Skip to content

Commit 0ed35a7

Browse files
committed
add test for ki protection leaking accross local functions
1 parent 3ea6617 commit 0ed35a7

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/trio/_core/_tests/test_ki.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import inspect
55
import signal
66
import threading
7-
from typing import TYPE_CHECKING, AsyncIterator, Callable, Iterator
7+
from typing import TYPE_CHECKING, AsyncIterator, Callable, Iterator, TypeVar
88

99
import outcome
1010
import pytest
@@ -515,3 +515,26 @@ async def inner() -> None:
515515
_core.run(inner)
516516
finally:
517517
threading._active[thread.ident] = original # type: ignore[attr-defined]
518+
519+
520+
_T = TypeVar("_T")
521+
522+
523+
def _identity(v: _T) -> _T:
524+
return v
525+
526+
527+
async def test_ki_does_not_leak_accross_different_calls_to_inner_functions() -> None:
528+
assert not _core.currently_ki_protected()
529+
530+
def factory(enabled: bool) -> Callable[[], bool]:
531+
@_identity(_core.enable_ki_protection if enabled else _identity)
532+
def decorated() -> bool:
533+
return _core.currently_ki_protected()
534+
535+
return decorated
536+
537+
decorated_enabled = factory(True)
538+
decorated_disabled = factory(False)
539+
assert decorated_enabled()
540+
assert not decorated_disabled()

0 commit comments

Comments
 (0)