Skip to content

ScoringMonitor is mutated by its listener thread while joblib pickles the estimator #34816

Closed

Description

@betatim

examples/callbacks/plot_scoring_monitor.py fails on Python 3.14.7 with scikit-learn 1.9.0:

_pickle.PicklingError: Could not pickle the task to send it to the workers.

Most times the example fails on 3.14.7 (like four out of five times), but doesn't on 3.14.6 (zero out of five). The full traceback is in the details below.

I put AI to work to figure this out, it started as trying to understand a failure in the cuml CI. The "cause" for the failure in CI is that we started using Python 3.14.7. AI dug around for a while and found python/cpython#150024 and the following reasoning about what is happening.

Two threads touch the same list:

  • ScoringMonitor.__init__ registers its own append as a message consumer, open_listener(self._log.append, owner=self) (sklearn/callback/_scoring_monitor.py:124), so _log grows as scores arrive.
  • The estimator carrying that monitor in _skl_callbacks is pickled to the workers by loky's queue-feeder thread (loky/backend/queues.py:159).

The PR and the reasoning is consistent and feels like it would explain it all. But I am not deep enough in the topic to fully validate it. But it does look reasonable and I think it is a real bug in scikit-learn (even if the explanation isn't what AI suggests).

Details

joblib.externals.loky.process_executor._RemoteTraceback:
"""
Traceback (most recent call last):
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/externals/loky/backend/queues.py", line 159, in _feed
    obj_ = dumps(obj, reducers=reducers)
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/externals/loky/backend/reduction.py", line 214, in dumps
    dump(obj, buf, reducers=reducers, protocol=protocol)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/externals/loky/backend/reduction.py", line 207, in dump
    _LokyPickler(file, reducers=reducers, protocol=protocol).dump(obj)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/externals/cloudpickle/cloudpickle.py", line 1313, in dump
    return super().dump(obj)
           ~~~~~~~~~~~~^^^^^
RuntimeError: list changed size during iteration
when serializing dict item '_log'
when serializing sklearn.callback._scoring_monitor.ScoringMonitor state
when serializing sklearn.callback._scoring_monitor.ScoringMonitor object
when serializing list item 0
when serializing dict item '_skl_callbacks'
when serializing sklearn.linear_model._logistic.LogisticRegression state
when serializing sklearn.linear_model._logistic.LogisticRegression object
when serializing tuple item 1
when serializing list item 1
when serializing dict item 'steps'
when serializing sklearn.pipeline.Pipeline state
when serializing sklearn.pipeline.Pipeline object
when serializing tuple item 0
when serializing tuple item 1
when serializing list item 0
when serializing tuple item 0
when serializing joblib.parallel.BatchedCalls reconstructor arguments
when serializing joblib.parallel.BatchedCalls object
when serializing dict item 'fn'
when serializing joblib.externals.loky.process_executor._CallItem state
when serializing joblib.externals.loky.process_executor._CallItem object
"""

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

Traceback (most recent call last):
  File "/home/thead/git/cuml/python/cuml/cuml_accel_tests/upstream/scikit-learn/sklearn-upstream/examples/callbacks/plot_scoring_monitor.py", line 79, in <module>
    grid_search.set_callbacks(ProgressBar()).fit(X, y)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/sklearn/base.py", line 1403, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/sklearn/model_selection/_search.py", line 1131, in fit
    self._run_search(evaluate_candidates, callback_ctx=root_callback_ctx)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/sklearn/model_selection/_search.py", line 1718, in _run_search
    evaluate_candidates(ParameterGrid(self.param_grid), callback_ctx=search_ctx)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/sklearn/model_selection/_search.py", line 1080, in evaluate_candidates
    out = parallel(
        delayed(_fit_and_score)(
    ...<15 lines>...
        ) in zip(candidate_split_pairs, evaluation_contexts)
    )
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/sklearn/utils/parallel.py", line 91, in __call__
    return super().__call__(iterable_with_config_and_warning_filters)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/parallel.py", line 2072, in __call__
    return output if self.return_generator else list(output)
                                                ~~~~^^^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/parallel.py", line 1682, in _get_outputs
    yield from self._retrieve()
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/parallel.py", line 1784, in _retrieve
    self._raise_error_fast()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/parallel.py", line 1859, in _raise_error_fast
    error_job.get_result(self.timeout)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/parallel.py", line 758, in get_result
    return self._return_or_raise()
           ~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/thead/miniforge3/envs/cuml-aug24/lib/python3.14/site-packages/joblib/parallel.py", line 773, in _return_or_raise
    raise self._result
_pickle.PicklingError: Could not pickle the task to send it to the workers.

Output from show_versions():

System:
    python: 3.14.7 | packaged by conda-forge | (main, Aug 19 2026, 15:32:49) [GCC 15.3.0]
executable: /home/thead/miniforge3/envs/cuml-aug24/bin/python
   machine: Linux-7.0.0-29-generic-x86_64-with-glibc2.39

Python dependencies:
      sklearn: 1.9.0
          pip: 26.2.1
   setuptools: 84.0.0
        numpy: 2.4.6
        scipy: 1.17.1
       Cython: None
       pandas: 3.0.5
   matplotlib: 3.11.1
       joblib: 1.5.3
threadpoolctl: 3.6.0
     narwhals: 2.25.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 64
         prefix: libopenblas
       filepath: /home/thead/miniforge3/envs/cuml-aug24/lib/libopenblasp-r0.3.34.so
        version: 0.3.34
threading_layer: pthreads
   architecture: Cooperlake

       user_api: openmp
   internal_api: openmp
    num_threads: 64
         prefix: libgomp
       filepath: /home/thead/miniforge3/envs/cuml-aug24/lib/libgomp.so.1.0.0
        version: None

xref NVIDIA/cuml#8510

Activity

  1. changed the title [-]ScoringMonitor` is mutated by its listener thread while joblib pickles the estimator[/-] [+]`ScoringMonitor` is mutated by its listener thread while joblib pickles the estimator[/+] on Aug 25, 2026
  2. AnneBeyer commented on Aug 26, 2026

    @AnneBeyer
    Member

    This is related to the CI failure we currently have on main: #34806 also updated Python to 3.14.7.

  3. lesteve commented on Aug 26, 2026

    @lesteve
    Member

    So this is something that happens in Python 3.14.7 only, thanks for confirming this!

    cc @jeremiedbb @FrancoisPgm

  4. added
    Needs Investigation
    on Aug 26, 2026
  5. removed
    Needs Investigation
    on Aug 27, 2026
  6. lesteve commented on Aug 27, 2026

    @lesteve
    Member

    As an aside I wish the joblib pickling error was a bit more explicit about what can not be pickled and why. Not sure how easy this is though ...

    Edit: actually it is shown when you run the example directly ... maybe sphinx-gallery removes the __cause__ of the exception in the traceback, or maybe something else 🤷 ?

    python examples/callbacks/plot_scoring_monitor.py 
    

    The pickle error does show that this is about pickling _log attribute.

    joblib.externals.loky.process_executor._RemoteTraceback: 
    """
    Traceback (most recent call last):
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/externals/loky/backend/queues.py", line 159, in _feed
        obj_ = dumps(obj, reducers=reducers)
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/externals/loky/backend/reduction.py", line 214, in dumps
        dump(obj, buf, reducers=reducers, protocol=protocol)
        ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/externals/loky/backend/reduction.py", line 207, in dump
        _LokyPickler(file, reducers=reducers, protocol=protocol).dump(obj)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/externals/cloudpickle/cloudpickle.py", line 1303, in dump
        return super().dump(obj)
               ~~~~~~~~~~~~^^^^^
    RuntimeError: list changed size during iteration
    when serializing dict item '_log'
    when serializing sklearn.callback._scoring_monitor.ScoringMonitor state
    when serializing sklearn.callback._scoring_monitor.ScoringMonitor object
    when serializing list item 0
    when serializing dict item '_skl_callbacks'
    when serializing sklearn.linear_model._logistic.LogisticRegression state
    when serializing sklearn.linear_model._logistic.LogisticRegression object
    when serializing tuple item 1
    when serializing list item 1
    when serializing dict item 'steps'
    when serializing sklearn.pipeline.Pipeline state
    when serializing sklearn.pipeline.Pipeline object
    when serializing tuple item 0
    when serializing tuple item 1
    when serializing list item 0
    when serializing tuple item 0
    when serializing joblib.parallel.BatchedCalls reconstructor arguments
    when serializing joblib.parallel.BatchedCalls object
    when serializing dict item 'fn'
    when serializing joblib.externals.loky.process_executor._CallItem state
    when serializing joblib.externals.loky.process_executor._CallItem object
    """
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/home/lesteve/dev/scikit-learn/examples/callbacks/plot_scoring_monitor.py", line 79, in <module>
        grid_search.set_callbacks(ProgressBar()).fit(X, y)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
      File "/home/lesteve/dev/scikit-learn/sklearn/base.py", line 1403, in wrapper
        return fit_method(estimator, *args, **kwargs)
      File "/home/lesteve/dev/scikit-learn/sklearn/model_selection/_search.py", line 1134, in fit
        self._run_search(evaluate_candidates, callback_ctx=root_callback_ctx)
        ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/lesteve/dev/scikit-learn/sklearn/model_selection/_search.py", line 1721, in _run_search
        evaluate_candidates(ParameterGrid(self.param_grid), callback_ctx=search_ctx)
        ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/lesteve/dev/scikit-learn/sklearn/model_selection/_search.py", line 1083, in evaluate_candidates
        out = parallel(
            delayed(_fit_and_score)(
        ...<15 lines>...
            ) in zip(candidate_split_pairs, evaluation_contexts)
        )
      File "/home/lesteve/dev/scikit-learn/sklearn/utils/parallel.py", line 91, in __call__
        return super().__call__(iterable_with_config_and_warning_filters)
               ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/parallel.py", line 2072, in __call__
        return output if self.return_generator else list(output)
                                                    ~~~~^^^^^^^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/parallel.py", line 1682, in _get_outputs
        yield from self._retrieve()
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/parallel.py", line 1784, in _retrieve
        self._raise_error_fast()
        ~~~~~~~~~~~~~~~~~~~~~~^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/parallel.py", line 1859, in _raise_error_fast
        error_job.get_result(self.timeout)
        ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/parallel.py", line 758, in get_result
        return self._return_or_raise()
               ~~~~~~~~~~~~~~~~~~~~~^^
      File "/home/lesteve/micromamba/envs/scikit-learn-dev/lib/python3.14t/site-packages/joblib/parallel.py", line 773, in _return_or_raise
        raise self._result
    _pickle.PicklingError: Could not pickle the task to send it to the workers.
    
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