Skip to content

Commit

Permalink
Fixed cancel scopes becoming their own parent on asyncio and curio
Browse files Browse the repository at this point in the history
Fixes nedbat#24.
Closes nedbat#25.
  • Loading branch information
agronholm committed Oct 27, 2018
1 parent bb276ae commit cb979b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion anyio/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ async def sleep(delay: float) -> None:
@async_generator
async def open_cancel_scope():
task = current_task()
parent_scope = get_cancel_scope(task)
scope = CancelScope()
scope.add_task(task)
set_cancel_scope(task, scope)
parent_scope = get_cancel_scope(task)
if parent_scope is not None:
parent_scope.children.add(scope)

Expand Down
2 changes: 1 addition & 1 deletion anyio/_backends/curio.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ async def sleep(seconds: int):
@async_generator
async def open_cancel_scope():
task = await curio.current_task()
parent_scope = get_cancel_scope(task)
scope = CancelScope()
scope.add_task(task)
set_cancel_scope(task, scope)
parent_scope = get_cancel_scope(task)
if parent_scope is not None:
parent_scope.children.add(scope)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ async def test_escaping_cancelled_exception():
await sleep(0)


@pytest.mark.anyio
async def test_cancel_scope_cleared():
async with move_on_after(0.1):
await sleep(1)

await sleep(0)


@pytest.mark.anyio
async def test_fail_after():
with pytest.raises(TimeoutError):
Expand Down

0 comments on commit cb979b6

Please sign in to comment.