Skip to content

Sometimes notify_closing can be called on dead events - #3503

Merged
A5rocks merged 2 commits into
python-trio:mainfrom
A5rocks:notify-closing-race
Sep 17, 2026
Merged

A5rocks merged 2 commits into
python-trio:mainfrom
A5rocks:notify-closing-race

Conversation

@A5rocks

@A5rocks A5rocks commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3502 since this is the repro they were describing, I think.

@A5rocks
A5rocks force-pushed the notify-closing-race branch from 8e2fc98 to d887165 Compare September 5, 2026 00:38
@codecov

codecov Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00000%. Comparing base (d5eb534) to head (5387ebb).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@               Coverage Diff               @@
##                 main        #3503   +/-   ##
===============================================
  Coverage   100.00000%   100.00000%           
===============================================
  Files             128          128           
  Lines           19452        19490   +38     
  Branches         1321         1325    +4     
===============================================
+ Hits            19452        19490   +38     
Files with missing lines Coverage Δ
src/trio/_core/_io_kqueue.py 100.00000% <100.00000%> (ø)
src/trio/_core/_tests/test_guest_mode.py 100.00000% <100.00000%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@A5rocks
A5rocks force-pushed the notify-closing-race branch from d887165 to b97da89 Compare September 5, 2026 05:55
@A5rocks

A5rocks commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

I'll close this in favor of the more general solution @arthur-tacca was proposing.

@A5rocks A5rocks closed this Sep 17, 2026
@A5rocks

A5rocks commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Gah, I forgot what this PR did. NVM!

@A5rocks A5rocks reopened this Sep 17, 2026

@CoolCat467 CoolCat467 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand this is probably fine.

@A5rocks
A5rocks merged commit 50b9825 into python-trio:main Sep 17, 2026
139 checks passed
@A5rocks
A5rocks deleted the notify-closing-race branch September 17, 2026 02:12
@arthur-tacca

Copy link
Copy Markdown
Member

Sorry I just noticed that this only checks for ENOENT

                except OSError as e:
                    if e.errno == errno.ENOENT:  # pragma: no branch
                        # the event isn't in kqueue
                        continue

But for similar code in _wait_common, Trio currently checks for exc.errno in (errno.EBADF, errno.ENOENT). It says "FreeBSD reports this using EBADF. macOS uses ENOENT". Not sure if this applies to this situation too.

@A5rocks

A5rocks commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Ah possible. I just took that errno from the relevant man page on macOS...

@arthur-tacca

Copy link
Copy Markdown
Member

Ok sorry, the error code thing is not a big deal in this case.

The bug that this pull request fixes is that a host callback (in guest mode) closes a socket after get_events in the Trio worker thread has already finished and one of the results is for the socket (so it's already removed from the kqueue).

Assuming the socket close is done with the normal ordering of notify_closing and then actual close, you'd get errno.ENOENT on both platforms. It doesn't make sense to return errno.EBADF because the file isn't actually closed yet.

The trio docs says that's the recommended way for a close implementation to work, but does say it's also valid to do the actual close first and then notify_closing. If you do that then you really would get errno.EBADF on BSD. But that's an odd thing for the application to do, and it anyway doesn't cause a Trio internal error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants