Repository navigation
MNT Callback transport actually free the accept thread on closing a listener - #34856
Conversation
|
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. |
|
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 |
| conn = listener.accept() | ||
| except (OSError, EOFError): | ||
| break # woken up by `close_listener`, or the listener failed | ||
| Thread(target=_handle, args=(conn,), daemon=True).start() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I can confirm that the first test fails on the linux machine I have, while it passes on macos. |
FrancoisPgm
left a comment
There was a problem hiding this comment.
LGTM thanks. I also like @ogrisel's suggestion to add names to the transport threads.
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]>
|
I asked cursor to address the remaining review comments and fix another small problem to explicitly deal with the If CI still green I will merge. |
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]>
…edbb/scikit-learn into fix-callback-transport-fd 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]>
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]>
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)