Skip to content

BUG: Callback's _worker_connections cache grows too big #34734

Closed

Description

@FrancoisPgm

Describe the bug and give evidence about its user-facing impact

Using a ScoringMonitor in a lot of sub-estimators in sub-processes eventually raises an EOFError when trying to create the connection for the callback to communicate across processes.

After investigating with the help of an LLM, the suspect appears to be the _worker_connections. It stores new connections for each fit, but there is no mechanism to remove them from the cache. When persistent workers are used, like when using joblib's loky backend, the cache grows until the worker process hits its fd limit and the next time a connection is opened the handshake sequence breaks mid-flight, causing the EOFError.

The LLM's suggested fix is to have a max size for _worker_connections, using a least recently used eviction policy.

ping @jeremiedbb

Steps/Code to Reproduce

from sklearn.callback import ScoringMonitor
from sklearn.callback.tests._utils import (
    MaxIterEstimator,
    MetaEstimator,
)

for _ in range(120):
    callback = ScoringMonitor(scoring="r2")
    est = MaxIterEstimator(max_iter=2).set_callbacks(callback)
    meta_est = MetaEstimator(est, n_jobs=2, prefer="processes")

    meta_est.fit()

Expected Results

No error.

Actual Results

joblib.externals.loky.process_executor._RemoteTraceback:
"""
Traceback (most recent call last):
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/externals/loky/process_executor.py", line 453, in _process_worker
    call_item = call_queue.get(block=True, timeout=timeout)
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/multiprocessing/queues.py", line 120, in get
    return _ForkingPickler.loads(res)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/Users/fpaugam/probabl/scikit-learn/sklearn/callback/_scoring_monitor.py", line 176, in __setstate__
    if not can_reuse_listener(self._listener_handle):
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fpaugam/probabl/scikit-learn/sklearn/callback/_transport.py", line 153, in can_reuse_listener
    Client(listener_handle.address, authkey=listener_handle.authkey).close()
    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/multiprocessing/connection.py", line 525, in Client
    answer_challenge(c, authkey)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/multiprocessing/connection.py", line 953, in answer_challenge
    message = connection.recv_bytes(256)         # reject large message
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/multiprocessing/connection.py", line 216, in recv_bytes
    buf = self._recv_bytes(maxlength)
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/multiprocessing/connection.py", line 430, in _recv_bytes
    buf = self._recv(4)
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/multiprocessing/connection.py", line 399, in _recv
    raise EOFError
EOFError
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/fpaugam/probabl/scikit-learn/test.py", line 12, in <module>
    meta_est.fit()
    ~~~~~~~~~~~~^^
  File "/Users/fpaugam/probabl/scikit-learn/sklearn/base.py", line 1403, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File "/Users/fpaugam/probabl/scikit-learn/sklearn/callback/tests/_utils.py", line 379, in fit
    Parallel(n_jobs=self.n_jobs, prefer=self.prefer)(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        delayed(_fit_subestimator)(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<7 lines>...
        for i in range(self.n_outer)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/fpaugam/probabl/scikit-learn/sklearn/utils/parallel.py", line 91, in __call__
    return super().__call__(iterable_with_config_and_warning_filters)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/parallel.py", line 2072, in __call__
    return output if self.return_generator else list(output)
                                                ~~~~^^^^^^^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/parallel.py", line 1682, in _get_outputs
    yield from self._retrieve()
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/parallel.py", line 1784, in _retrieve
    self._raise_error_fast()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/parallel.py", line 1859, in _raise_error_fast
    error_job.get_result(self.timeout)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/parallel.py", line 758, in get_result
    return self._return_or_raise()
           ~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/fpaugam/miniforge3/envs/sklearn/lib/python3.13/site-packages/joblib/parallel.py", line 773, in _return_or_raise
    raise self._result
joblib.externals.loky.process_executor.BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable.

Versions

System:
    python: 3.13.11 | packaged by conda-forge | (main, Dec  6 2025, 11:28:54) [Clang 19.1.7 ]
executable: /Users/fpaugam/miniforge3/envs/sklearn/bin/python3
   machine: macOS-26.5.2-arm64-arm-64bit-Mach-O

Python dependencies:
      sklearn: 1.10.dev0
          pip: 25.3
   setuptools: 80.9.0
        numpy: 2.4.0
        scipy: 1.16.3
       Cython: 3.2.4
       pandas: 3.0.1
   matplotlib: 3.10.8
       joblib: 1.5.3
threadpoolctl: 3.6.0
     narwhals: 2.15.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
    num_threads: 10
         prefix: libomp
       filepath: /opt/homebrew/Cellar/libomp/22.1.7/lib/libomp.dylib
        version: None

Interest in fixing the bug

I'm interested in solving it.

Activity

  1. added
    Needs Triage
    on Aug 12, 2026
  2. added
    Needs Decision
    and removed
    Needs Triage
    on Aug 18, 2026
  3. github-actions commented on Aug 18, 2026

    @github-actions

    Thanks for the work you've done so far. The goal of this comment is to set expectations.

    Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.

    Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.

  4. moved this to Todo in Labson Aug 24, 2026
  5. moved this from Todo to In progress in Labson Aug 24, 2026
  6. moved this from In progress to Todo in Labson Aug 24, 2026
  7. shubhampotdar4122003-debug commented on Sep 2, 2026

    @shubhampotdar4122003-debug

    I'm interested in solving it.

  8. moved this from Todo to In progress in Labson Sep 2, 2026
  9. removed
    Needs Decision
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions