Skip to content

Commit

Permalink
add test for ki protection leaking accross local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Oct 14, 2024
1 parent 3ea6617 commit 0ed35a7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/trio/_core/_tests/test_ki.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import inspect
import signal
import threading
from typing import TYPE_CHECKING, AsyncIterator, Callable, Iterator
from typing import TYPE_CHECKING, AsyncIterator, Callable, Iterator, TypeVar

import outcome
import pytest
Expand Down Expand Up @@ -515,3 +515,26 @@ async def inner() -> None:
_core.run(inner)
finally:
threading._active[thread.ident] = original # type: ignore[attr-defined]


_T = TypeVar("_T")


def _identity(v: _T) -> _T:
return v


async def test_ki_does_not_leak_accross_different_calls_to_inner_functions() -> None:
assert not _core.currently_ki_protected()

def factory(enabled: bool) -> Callable[[], bool]:
@_identity(_core.enable_ki_protection if enabled else _identity)
def decorated() -> bool:
return _core.currently_ki_protected()

return decorated

decorated_enabled = factory(True)
decorated_disabled = factory(False)
assert decorated_enabled()
assert not decorated_disabled()

0 comments on commit 0ed35a7

Please sign in to comment.