Skip to content

Commit 04c7535

Browse files
committed
fix(test): replace anyio.sleep(0.01) with anyio.lowlevel.checkpoint()
Use checkpoint() for deterministic context switching instead of a fixed-duration sleep in the tenant isolation test.
1 parent 02725c8 commit 04c7535

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

tests/server/test_multi_tenancy_session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ async def test_tenant_context_isolation_between_concurrent_requests():
237237
2. Each simulated request:
238238
- Creates an AccessToken with its tenant_id
239239
- Sets it in the auth_context_var (the contextvar used for auth state)
240-
- Yields control via anyio.sleep() to allow the other task to run
240+
- Yields control via checkpoint() to allow the other task to run
241241
- Reads back the tenant_id via get_tenant_id()
242242
- Stores the result for verification
243243
244-
3. The anyio.sleep(0.01) is intentional - it forces a context switch,
245-
creating an opportunity for tenant context to "leak" if the isolation
246-
is broken. Without proper contextvar isolation, task2 might see
244+
3. The anyio.lowlevel.checkpoint() forces a context switch, creating
245+
an opportunity for tenant context to "leak" if the isolation is
246+
broken. Without proper contextvar isolation, task2 might see
247247
task1's tenant_id (or vice versa) after the context switch.
248248
249249
4. We use anyio.create_task_group() to run both tasks truly concurrently,
@@ -281,7 +281,7 @@ async def simulate_request(tenant_id: str, request_key: str) -> None:
281281
try:
282282
# Yield control to allow other tasks to run. This is the critical
283283
# point where context leakage could occur if isolation is broken.
284-
await anyio.sleep(0.01)
284+
await anyio.lowlevel.checkpoint()
285285

286286
# Read back the tenant_id - should still be our tenant, not the other
287287
results[request_key] = tenant_id_var.get()

0 commit comments

Comments
 (0)