Repository navigation
FIX Callback transport don't store connections forever - #34870
Conversation
| # Nothing more will be sent for this fit. The channel itself outlives the fit, | ||
| # since this callback keeps logging across fits, but the connection a worker | ||
| # copy holds is released here rather than whenever that copy is collected. | ||
| self._channel.disconnect() |
There was a problem hiding this comment.
That part is new and useful to stop the leak because callbacks are held a reference cycles and thus are not collected immediately. Adding the fact that loky reuses worker makes the callbacks and thus the channels and thus the connections alive for a very long time, which gives time for the number of file descriptors to grow too much.
So ideally the better fix would be to avoid that reference cycle (it comes from the context tree holding references to both its parent and children), which I'm planning to investigate as a follow-up work. In the meantime the disconnect fix is acceptable.
There was a problem hiding this comment.
The callback context no longer introduce reference cycles but I think we should keep this to release system resources deterministically as soon as no longer needed rather than rely on GC.
| def test_channel_state_is_not_walked_by_the_pickler(factory, monkeypatch): | ||
| """Check that pickling a callback never traverses state its channels write to. | ||
|
|
||
| An estimator carrying a callback can be pickled by a background thread, e.g. loky's | ||
| queue feeder dispatching a task to a worker, while the listener thread of that same | ||
| queue feeder dispatching a task to a worker, while a channel thread of that same | ||
| callback mutates the callback's state as messages come in. Pickling a container that | ||
| another thread mutates breaks the dump, which joblib reports as "Could not pickle | ||
| the task to send it to the workers". | ||
|
|
||
| The check runs from a hook, i.e. while the listeners are up, which is when such a | ||
| The check runs from a hook, i.e. while the channels are up, which is when such a | ||
| dispatch would happen. |
There was a problem hiding this comment.
I had to rewrite this test because it used to inspect the global _message_consumers dict which is no longer there. Instead I monkeypatch Channel.init to record the message_consumer. The intent of the test is still the same and it still fails if we remove the fix from #34821
| - Fixed a leak of file descriptors and background threads by | ||
| :class:`callback.ScoringMonitor` and :class:`callback.ProgressBar`. | ||
| By :user:`Jérémie du Boisberranger <jeremiedbb>`. :pr:`34856`, |
There was a problem hiding this comment.
this looks weird but the idea is to reference this PR and #34856 in a single changelog entry. Look at the generated changelog, it looks as expected.
|
The missing coverage is for a line that was already uncovered before the refactoring (see https://app.codecov.io/gh/scikit-learn/scikit-learn/pull/34870). I'd rather ignore it for this PR and open a follow up PR to write a test to cover this line (unrelated to this PR). |
There was a problem hiding this comment.
I like the refactoring, the Channel class looks much cleaner to me.
I have some nitpicks, some small suggestions, and one potential important issue that might be worth discussing but which is reaching the limit of my understanding of threading.
EDIT : I tried the full fix by including the changes from #34856 to this branch, and testing with the active thread counts on a meta-estimator with callbacks works (and fails on main), so I am now confident enough to approve the PR.
|
|
||
|
|
||
| def open_listener(message_consumer, *, owner=None): | ||
| def open_listener(message_consumer): |
There was a problem hiding this comment.
open_listener and close_listener now only appear inside Channel, they could become private helpers.
There was a problem hiding this comment.
They are: they are defined in a private module (_transport.py) and are not exposed in/by any public module.
ogrisel
left a comment
There was a problem hiding this comment.
Some feedback. The core of this PR looks good and I confirm it can fix the problem when combined with the listener / _accept fix from the other PR.
|
I merged #34856 to main and will resolve the conflicts. |
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]>
disconnect is kept so non-propagated callbacks can free the worker socket in teardown, before cyclic GC collects the copy. Close the Client explicitly and test that the handler thread exits while the copy is still alive. Leftover-thread checks already use the named transport threads. Co-authored-by: Olivier Grisel <[email protected]>
…ort-fd-2 Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
…ce cycle Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Serialize cross-process `Channel.send` and `Channel.disconnect` with a per-Channel lock (re-initialized on unpickling), drop broken connections on error, and close accepted server-side connections in `_handle`'s `finally` block. Add unit and nested loky+threading regression tests. Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
…te comment Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
ogrisel
left a comment
There was a problem hiding this comment.
I merged main, resolved the conflicts, addressed my previous review comments and iteratively improved the PR to increase test coverage, discovered a new bug (deadlocks and crashes) related to nested parallelism. This is now also fixed and the changelog entry has been updated to reflect the wider scope of this fix.
The PR LGTM. +1 for merge on my side.
|
Looks good. Not sure we really need the |
Fixes #34734
This PR fixes the second (and main) source of leaking threads and file descriptors. It ensures that we don't keep open connections forever, even when they're not useful anymore.
(I'd rather have #34856 merged first to have easier merge conflicts but the 2 PRs fix 2 independent problems and can be reviewed independently)
Currently, we store every connection in a process-wide dict (
_worker_connections) and never remove them. So they stay open until the worker is shut down (which can take a while since loky can reuse workers for subsequent tasks). Making it a lru cache mitigates the issue a bit, but not entirely since it still allows to grow the number of open connections far beyond what's actually needed.This PR proposes to refactor the transport module a bit to add a new object (Channel) that abstracts both end of the communication channel (listener in the main process and clients in the workers). This way the connection made to
sendmessages is tied to the channel and dies with it. The connection alone can also be closed to immediately free the resources instead of waiting for the callback to be collected (i.e. at teardown for scoring monitor).Maybe it could have been fixed with a bit less refactoring but I actually find it cleaner and simpler. Now the only public object from transport is
Channel(at least when we make transport public :) ). The callbacks create and store a channel and then call methods of the channel.