diff --git a/trio/_core/_run.py b/trio/_core/_run.py index a10d6cb57f..465aebe6f5 100644 --- a/trio/_core/_run.py +++ b/trio/_core/_run.py @@ -2076,6 +2076,7 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False): # tie-breaker and the non-deterministic ordering of # task._notify_queues.) batch = list(runner.runq) + runner.runq.clear() if _ALLOW_DETERMINISTIC_SCHEDULING: # We're running under Hypothesis, and pytest-trio has patched # this in to make the scheduler deterministic and avoid flaky @@ -2083,8 +2084,12 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False): # operation, since we'll shuffle the list and _r is only # seeded for tests. batch.sort(key=lambda t: t._counter) - runner.runq.clear() - _r.shuffle(batch) + _r.shuffle(batch) + else: + # 50% chance of reversing the batch, this way each task + # can appear before/after any other task. + if _r.random() < 0.5: + batch.reverse() while batch: task = batch.pop() GLOBAL_RUN_CONTEXT.task = task diff --git a/trio/tests/test_scheduler_determinism.py b/trio/tests/test_scheduler_determinism.py index 67b2447f0a..e2d3167e45 100644 --- a/trio/tests/test_scheduler_determinism.py +++ b/trio/tests/test_scheduler_determinism.py @@ -6,7 +6,7 @@ async def scheduler_trace(): trace = [] async def tracer(name): - for i in range(10): + for i in range(50): trace.append((name, i)) await trio.sleep(0)