Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3502.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure `trio.lowlevel.notify_closing` works even on events that have been deleted, e.g. because they were registered as one shot events.
8 changes: 7 additions & 1 deletion src/trio/_core/_io_kqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ def notify_closing(self, fd: int | _HasFileNo) -> None:

if type(receiver) is _core.Task:
event = select.kevent(fd, filter_, select.KQ_EV_DELETE)
self._kqueue.control([event], 0)
try:
self._kqueue.control([event], 0)
except OSError as e:
if e.errno == errno.ENOENT: # pragma: no branch
# the event isn't in kqueue
continue
raise # pragma: no cover
exc = _core.ClosedResourceError("another task closed this fd")
_core.reschedule(receiver, outcome.Error(exc))
del self._registered[key]
Expand Down
21 changes: 21 additions & 0 deletions src/trio/_core/_tests/test_guest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,24 @@ async def trio_main() -> None:
aiotrio_run(trio_main, host_uses_signal_set_wakeup_fd=True)

assert record == {("asyncio", "asyncio", True), ("trio", "trio", True)}


def test_notify_closing_after_events() -> None:
# inspired by wrong repro in https://github.com/python-trio/trio/pull/3502
# either the program should silently pass or wait_writable should fail.
pair = socket.socketpair()
for sock in pair:
sock.setblocking(False)

async def trio_main(in_host: InHost) -> None:
in_host(uh_oh)
with contextlib.suppress(trio.ClosedResourceError):
await trio.lowlevel.wait_writable(pair[0]) # blocks

def uh_oh() -> None:
# this will run after trio gets events but before they are processed
trio.lowlevel.notify_closing(pair[0])

trivial_guest_run(trio_main)
for sock in pair:
sock.close()