Skip to content

MNT Callback transport actually free the accept thread on closing a listener - #34856

Merged
ogrisel merged 9 commits into
scikit-learn:mainfrom
jeremiedbb:fix-callback-transport-fd
Nov 20, 2026
Merged

ogrisel merged 9 commits into
scikit-learn:mainfrom
jeremiedbb:fix-callback-transport-fd

Conversation

@jeremiedbb

Copy link
Copy Markdown
Member

Towards #34734

While investigating #34734, I found that there are 2 sources of leaking threads and file descriptors. This PR ensures that closing the listener actually frees the accept thread (which also happens to hold 1 file descriptor open).

Closing the listener doesn't unblock the accept thread, so previously it used to stay alive forever. Now, in close_listener, we make a connection without the authkey which wakes-up the accept thread and break its loop which ensures that the _accept function returns, freeing the thread.
I'll update the PR with some tests soon.

I'm planning to fix the second source of leaking file descriptors in a separate PR.

(I don't think this one requires a changelog entry since it's hardly visible from the user side)

@FrancoisPgm

Copy link
Copy Markdown
Member

The diagnostic and fix seem reasonable to me, although I am no threading expert. However I am a bit more skeptical about the tests. I tried running them on the main branch and they passed. I would be more confident with tests that pass here but fail on main.

@jeremiedbb

Copy link
Copy Markdown
Member Author

The second test is expected to pass on main. It's more a sanity check that should have been added earlier, when writing the _transport code.

The first test does fail on main though, on my linux machine. I asked an agent and it said that it's likely to pass on main on a macos because listener.close() usually does interrupt accept().

@github-actions github-actions Bot added the size/M label Nov 9, 2026

@ogrisel ogrisel 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.

LGTM besides the following:

Comment thread sklearn/callback/_transport.py Outdated
conn = listener.accept()
except (OSError, EOFError):
break # woken up by `close_listener`, or the listener failed
Thread(target=_handle, args=(conn,), daemon=True).start()

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.

Maybe we could pass name="sklearn_callback_transport_handle" to help future debugging and profiling? Note that thread names do not need to be unique, so it's ok to have several running threads named "sklearn_callback_transport_handle".

Similarly we could pass name="sklearn_callback_transport_accept" below.

@ogrisel ogrisel Nov 12, 2026 •

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.

We could also use that to change _assert_no_leftover_threads to only consider threads with names that start with "sklearn_callback_transport_" and make those tests less likely to fail (or wrongly pass) because of unrelated threads being spawn or terminated in the background (e.g. managed by joblib or pytest plugins for instance).

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.

@FrancoisPgm

Copy link
Copy Markdown
Member

The first test does fail on main though, on my linux machine. I asked an agent and it said that it's likely to pass on main on a macos because listener.close() usually does interrupt accept().

I can confirm that the first test fails on the linux machine I have, while it passes on macos.

@FrancoisPgm FrancoisPgm 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.

LGTM thanks. I also like @ogrisel's suggestion to add names to the transport threads.

@ogrisel ogrisel moved this from PR waiting for reviews to PR in progress in Labs Nov 16, 2026
cursoragent and others added 2 commits November 18, 2026 17:30
Give the accept and handler threads a shared name prefix so debugging
and the leftover-thread tests can ignore unrelated threads.

Co-authored-by: Olivier Grisel <[email protected]>
AuthenticationError is not an OSError. A peer that answers the challenge
with a bad digest must not tear down the listener. close_listener still
wakes the accept thread with EOFError.

Co-authored-by: Olivier Grisel <[email protected]>
@github-actions github-actions Bot added size/L and removed size/M labels Nov 20, 2026
@ogrisel

ogrisel commented Nov 20, 2026

Copy link
Copy Markdown
Member

I asked cursor to address the remaining review comments and fix another small problem to explicitly deal with the AuthenticationError exception.

If CI still green I will merge.

@ogrisel
ogrisel enabled auto-merge (squash) November 20, 2026 09:47
cursoragent and others added 3 commits November 20, 2026 09:58
Other callback tests can leave named accept threads in the process.
Count only the threads this test added, so the leak checks still ignore
unrelated threads without depending on test order.

Co-authored-by: Olivier Grisel <[email protected]>
Other callback tests can leave transport threads alive, and one of them
can exit between a count snapshot and the assertion, so comparing counts
can cancel the thread a test just started. Snapshot the thread objects
instead and only consider threads that were not in the snapshot.

Co-authored-by: Olivier Grisel <[email protected]>
@ogrisel
ogrisel merged commit 5e9184e into scikit-learn:main Nov 20, 2026
37 checks passed
@github-project-automation github-project-automation Bot moved this from PR in progress to Done in Labs Nov 20, 2026
ogrisel added a commit to jeremiedbb/scikit-learn that referenced this pull request Nov 21, 2026
Bring in the accept-thread shutdown from scikit-learn#34856. close_listener wakes the
accept thread with an unauthenticated connection, a rejected handshake
keeps the listener open, and the accept and handler threads are named.

Co-authored-by: Olivier Grisel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants