Skip to content

Free-threaded failure in gaussian_process/tests/test_gpc.py::test_multi_class_n_jobs[kernel3] with dictionary changed size during iteration #33152

Closed as not planned

Description

@scikit-learn-bot

CI failed on Unit tests (Jan 29, 2026)

  • test_multi_class_n_jobs[kernel3]

Activity

  1. added
    Needs Triage
    on Jan 29, 2026
  2. StefanieSenger commented on Jan 29, 2026

    @StefanieSenger
    Member

    Jan 29: RuntimeError in Linux x86-64 pylatest_free_threaded:

        @property
        def hyperparameters(self):
            """Returns a list of all hyperparameter specifications."""
            r = [
                getattr(self, attr)
    >           for attr in dir(self)
                            ^^^^^^^^^
                if attr.startswith("hyperparameter_")
            ]
    E       RuntimeError: dictionary changed size during iteration
    
    self       = 1.25**2
    
    ../sklearn/gaussian_process/kernels.py:280: RuntimeError
    
    PARALLEL FAILED gaussian_process/tests/test_gpc.py::test_multi_class_n_jobs[kernel3] - RuntimeError: dictionary changed size during iteration
    Full stack-trace

    ______________ ERROR at call of test_multi_class_n_jobs[kernel3] _______________
    
    kernel = 1**2 * RBF(length_scale=1)
    
        @pytest.mark.parametrize("kernel", kernels)
        def test_multi_class_n_jobs(kernel):
            # Test that multi-class GPC produces identical results with n_jobs>1.
            gpc = GaussianProcessClassifier(kernel=kernel)
    >       gpc.fit(X, y_mc)
    
    gpc        = GaussianProcessClassifier(kernel=1**2 * RBF(length_scale=1))
    kernel     = 1**2 * RBF(length_scale=1)
    
    ../sklearn/gaussian_process/tests/test_gpc.py:188: 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    ../sklearn/base.py:1347: in wrapper
        return fit_method(estimator, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            args       = (array([[ 0.        ],
           [ 0.34482759],
           [ 0.68965517],
           [ 1.03448276],
           [ 1.37931034],
           ....        ]]), array([1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2,
           2, 2, 2, 2, 2, 1, 1, 0]))
            estimator  = GaussianProcessClassifier(kernel=1**2 * RBF(length_scale=1))
            fit_method = <function GaussianProcessClassifier.fit at 0x540616e8640>
            global_skip_validation = False
            kwargs     = {}
            partial_fit_and_fitted = False
            prefer_skip_nested_validation = True
    ../sklearn/gaussian_process/_gpc.py:773: in fit
        self.base_estimator_.fit(X, y)
            X          = array([[ 0.        ],
           [ 0.34482759],
           [ 0.68965517],
           [ 1.03448276],
           [ 1.37931034],
           [...586207],
           [ 8.62068966],
           [ 8.96551724],
           [ 9.31034483],
           [ 9.65517241],
           [10.        ]])
            self       = GaussianProcessClassifier(kernel=1**2 * RBF(length_scale=1))
            y          = array([1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2,
           2, 2, 2, 2, 2, 1, 1, 0])
    ../sklearn/base.py:1347: in wrapper
        return fit_method(estimator, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            args       = (array([[ 0.        ],
           [ 0.34482759],
           [ 0.68965517],
           [ 1.03448276],
           [ 1.37931034],
           ....        ]]), array([1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2,
           2, 2, 2, 2, 2, 1, 1, 0]))
            estimator  = OneVsRestClassifier(estimator=_BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1)))
            fit_method = <function OneVsRestClassifier.fit at 0x5405f6d3e40>
            global_skip_validation = True
            kwargs     = {}
            partial_fit_and_fitted = False
            prefer_skip_nested_validation = False
    ../sklearn/multiclass.py:376: in fit
        self.estimators_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose)(
            X          = array([[ 0.        ],
           [ 0.34482759],
           [ 0.68965517],
           [ 1.03448276],
           [ 1.37931034],
           [...586207],
           [ 8.62068966],
           [ 8.96551724],
           [ 9.31034483],
           [ 9.65517241],
           [10.        ]])
            Y          = <Compressed Sparse Column sparse matrix of dtype 'int64'
    	with 30 stored elements and shape (30, 3)>
            columns    = <generator object OneVsRestClassifier.fit.<locals>.<genexpr> at 0x541342b3e20>
            fit_params = {}
            routed_params = <sklearn.utils._metadata_requests.process_routing.<locals>.EmptyRequest object at 0x54134300190>
            self       = OneVsRestClassifier(estimator=_BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1)))
            y          = array([1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2,
           2, 2, 2, 2, 2, 1, 1, 0])
    ../sklearn/utils/parallel.py:91: in __call__
        return super().__call__(iterable_with_config_and_warning_filters)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            __class__  = <class 'sklearn.utils.parallel.Parallel'>
            config     = {'array_api_dispatch': False, 'assume_finite': False, 'display': 'diagram', 'enable_cython_pairwise_dist': True, ...}
            filters_func = <function _get_filters at 0x54058c07d40>
            iterable   = <generator object OneVsRestClassifier.fit.<locals>.<genexpr> at 0x541343f0280>
            iterable_with_config_and_warning_filters = <generator object Parallel.__call__.<locals>.<genexpr> at 0x541343f03c0>
            self       = Parallel(n_jobs=1)
            warning_filters = [('error', None, <class 'pytest.PytestRemovedIn9Warning'>, None, 0), ('always', None, <class 'PendingDeprecationWarnin...arning'>, None, 0), ('ignore', re.compile('numpy.dtype size changed', re.IGNORECASE), <class 'Warning'>, None, 0), ...]
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/joblib/parallel.py:1986: in __call__
        return output if self.return_generator else list(output)
                                                    ^^^^^^^^^^^^
            iterable   = <generator object Parallel.__call__.<locals>.<genexpr> at 0x541343f03c0>
            n_jobs     = 1
            output     = <generator object Parallel._get_sequential_output at 0x541343f0640>
            self       = Parallel(n_jobs=1)
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/joblib/parallel.py:1914: in _get_sequential_output
        res = func(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^
            args       = (_BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1)), array([[ 0.        ],
           [ 0.34482759....        ]]), array([1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
           0, 0, 0, 0, 0, 1, 1, 0]))
            batch_size = 1
            func       = <sklearn.utils.parallel._FuncWrapper object at 0x541344a9f70>
            iterable   = <generator object Parallel.__call__.<locals>.<genexpr> at 0x541343f03c0>
            kwargs     = {'classes': ['not 1', np.int64(1)], 'fit_params': {}}
            res        = _BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1))
            self       = Parallel(n_jobs=1)
    ../sklearn/utils/parallel.py:184: in __call__
        return self.function(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            args       = (_BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1)), array([[ 0.        ],
           [ 0.34482759....        ]]), array([1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
           0, 0, 0, 0, 0, 1, 1, 0]))
            config     = {'array_api_dispatch': False, 'assume_finite': False, 'display': 'diagram', 'enable_cython_pairwise_dist': True, ...}
            filter_args = ('ignore', None, <class 'ResourceWarning'>, None, 0)
            kwargs     = {'classes': ['not 1', np.int64(1)], 'fit_params': {}}
            self       = <sklearn.utils.parallel._FuncWrapper object at 0x541344a9f70>
            special_key = 'module'
            this_value = '__main__'
            this_warning_filter_dict = {'action': 'ignore', 'category': <class 'ResourceWarning'>, 'lineno': 0}
            warning_filter_keys = ['action', 'message', 'category', 'module', 'lineno']
            warning_filters = [('error', None, <class 'pytest.PytestRemovedIn9Warning'>, None, 0), ('always', None, <class 'PendingDeprecationWarnin...arning'>, None, 0), ('ignore', re.compile('numpy.dtype size changed', re.IGNORECASE), <class 'Warning'>, None, 0), ...]
    ../sklearn/multiclass.py:96: in _fit_binary
        estimator.fit(X, y, **fit_params)
            X          = array([[ 0.        ],
           [ 0.34482759],
           [ 0.68965517],
           [ 1.03448276],
           [ 1.37931034],
           [...586207],
           [ 8.62068966],
           [ 8.96551724],
           [ 9.31034483],
           [ 9.65517241],
           [10.        ]])
            classes    = ['not 1', np.int64(1)]
            estimator  = _BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1))
            fit_params = {}
            unique_y   = array([0, 1])
            y          = array([1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
           0, 0, 0, 0, 0, 1, 1, 0])
    ../sklearn/gaussian_process/_gpc.py:228: in fit
        self._constrained_optimization(
            X          = array([[ 0.        ],
           [ 0.34482759],
           [ 0.68965517],
           [ 1.03448276],
           [ 1.37931034],
           [...586207],
           [ 8.62068966],
           [ 8.96551724],
           [ 9.31034483],
           [ 9.65517241],
           [10.        ]])
            label_encoder = LabelEncoder()
            obj_func   = <function _BinaryGaussianProcessClassifierLaplace.fit.<locals>.obj_func at 0x54134530a80>
            self       = _BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1))
            y          = array([1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
           0, 0, 0, 0, 0, 1, 1, 0])
    ../sklearn/gaussian_process/_gpc.py:503: in _constrained_optimization
        opt_res = scipy.optimize.minimize(
            bounds     = array([[-4.60517019,  4.60517019],
           [-6.90775528,  6.90775528]])
            initial_theta = array([0., 0.])
            obj_func   = <function _BinaryGaussianProcessClassifierLaplace.fit.<locals>.obj_func at 0x54134530a80>
            self       = _BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1))
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/optimize/_minimize.py:784: in minimize
        res = _minimize_lbfgsb(fun, x0, args, jac, bounds,
            args       = ()
            bounds     = [(-4.605170185988091, 4.605170185988092), (-6.907755278982137, 6.907755278982137)]
            callback   = None
            constraints = []
            fd_needed  = False
            fun        = <scipy.optimize._optimize.MemoizeJac object at 0x541342c0b70>
            hess       = None
            hessp      = None
            i_fixed    = array([False, False])
            jac        = <bound method MemoizeJac.derivative of <scipy.optimize._optimize.MemoizeJac object at 0x541342c0b70>>
            meth       = 'l-bfgs-b'
            method     = 'L-BFGS-B'
            options    = {}
            remove_vars = np.False_
            tol        = None
            x0         = array([0., 0.])
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/optimize/_lbfgsb_py.py:469: in _minimize_lbfgsb
        f, g = func_and_grad(x)
               ^^^^^^^^^^^^^^^^
            L          = 1
            U          = 1
            args       = ()
            bounds     = array([[-4.60517019, -6.90775528],
           [ 4.60517019,  6.90775528]])
            bounds_map = {(-inf, 1): 3, (-inf, inf): 0, (1, 1): 2, (1, inf): 1}
            callback   = None
            disp       = <object object at 0x54058570460>
            dsave      = array([ 2.47466860e+00,  1.91172904e+01,  2.22044605e-09,  3.90615641e-01,
            0.00000000e+00,  0.00000000e+00,  0...1,  0.00000000e+00,
            0.00000000e+00,  0.00000000e+00,  5.00000000e+00,  2.14364231e+01,
            4.28728461e+01])
            eps        = 1e-08
            f          = np.float64(19.117290445310896)
            factr      = np.float64(10000000.0)
            finite_diff_rel_step = None
            ftol       = 2.220446049250313e-09
            fun        = <scipy.optimize._optimize.MemoizeJac object at 0x541342c0b70>
            func_and_grad = <bound method ScalarFunction.fun_and_grad of <scipy.optimize._differentiable_functions.ScalarFunction object at 0x541343e5010>>
            g          = array([-0.74886615, -1.2216671 ])
            gtol       = 1e-05
            i          = 1
            intermediate_result =  fun: 19.117290445310896
       x: [ 5.774e-01 -5.943e-01]
            iprint     = <object object at 0x54058570460>
            isave      = array([  20,  100,  400,    0,   20,   40,  140,  240,  340,  740, 1140,
           1142, 1144, 1146, 1148, 1150,    0,   ...0,    1,    1,    0,    1,
              3,    0,    1,    0,    2,    0,    2,    0,    0,    0,    1],
          dtype=int32)
            iwa        = array([0, 1, 0, 0, 0, 1], dtype=int32)
            jac        = <bound method MemoizeJac.derivative of <scipy.optimize._optimize.MemoizeJac object at 0x541342c0b70>>
            ln_task    = array([3, 0], dtype=int32)
            low_bnd    = array([-4.60517019, -6.90775528])
            lsave      = array([0, 1, 1, 1], dtype=int32)
            m          = 10
            maxcor     = 10
            maxfun     = 15000
            maxiter    = 15000
            maxls      = 20
            n          = 2
            n_iterations = 1
            nbd        = array([2, 2], dtype=int32)
            pgtol      = 1e-05
            sf         = <scipy.optimize._differentiable_functions.ScalarFunction object at 0x541343e5010>
            task       = array([  3, 302], dtype=int32)
            unknown_options = {}
            upper_bnd  = array([4.60517019, 6.90775528])
            wa         = array([ 0.44387205, -0.96139523,  0.        , ...,  0.        ,
            0.        ,  0.        ], shape=(1230,))
            workers    = None
            x          = array([ 0.57737733, -0.59430271])
            x0         = array([0., 0.])
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/optimize/_differentiable_functions.py:412: in fun_and_grad
        self._update_fun()
            self       = <scipy.optimize._differentiable_functions.ScalarFunction object at 0x541343e5010>
            x          = array([ 0.57737733, -0.59430271])
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/optimize/_differentiable_functions.py:362: in _update_fun
        fx = self._wrapped_fun(self.x)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
            self       = <scipy.optimize._differentiable_functions.ScalarFunction object at 0x541343e5010>
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/_lib/_util.py:603: in __call__
        fx = self.f(np.copy(x), *self.args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            self       = <scipy._lib._util._ScalarFunctionWrapper object at 0x541340d0fd0>
            x          = array([ 0.57737733, -0.59430271])
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/optimize/_optimize.py:80: in __call__
        self._compute_if_needed(x, *args)
            args       = ()
            self       = <scipy.optimize._optimize.MemoizeJac object at 0x541342c0b70>
            x          = array([ 0.57737733, -0.59430271])
    ../../../../miniconda3/envs/testvenv/lib/python3.14t/site-packages/scipy/optimize/_optimize.py:74: in _compute_if_needed
        fg = self.fun(x, *args)
             ^^^^^^^^^^^^^^^^^^
            args       = ()
            self       = <scipy.optimize._optimize.MemoizeJac object at 0x541342c0b70>
            x          = array([ 0.57737733, -0.59430271])
    ../sklearn/gaussian_process/_gpc.py:219: in obj_func
        lml, grad = self.log_marginal_likelihood(
            eval_gradient = True
            self       = _BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1))
            theta      = array([ 0.57737733, -0.59430271])
    ../sklearn/gaussian_process/_gpc.py:372: in log_marginal_likelihood
        kernel.theta = theta
        ^^^^^^^^^^^^
            clone_kernel = False
            eval_gradient = True
            kernel     = 1.25**2 * RBF(length_scale=0.382)
            self       = _BinaryGaussianProcessClassifierLaplace(kernel=1**2 * RBF(length_scale=1))
            theta      = array([ 0.57737733, -0.59430271])
    ../sklearn/gaussian_process/kernels.py:759: in theta
        self.k1.theta = theta[:k1_dims]
        ^^^^^^^^^^^^^
            k1_dims    = 1
            self       = 1.25**2 * RBF(length_scale=0.382)
            theta      = array([ 0.57737733, -0.59430271])
    ../sklearn/gaussian_process/kernels.py:320: in theta
        for hyperparameter in self.hyperparameters:
                              ^^^^^^^^^^^^^^^^^^^^
            i          = 0
            params     = {'constant_value': np.float64(1.5587310264868124), 'constant_value_bounds': (0.01, 100.0)}
            self       = 1.25**2
            theta      = array([0.57737733])
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    
    self = 1.25**2
    
        @property
        def hyperparameters(self):
            """Returns a list of all hyperparameter specifications."""
            r = [
                getattr(self, attr)
    >           for attr in dir(self)
                            ^^^^^^^^^
                if attr.startswith("hyperparameter_")
            ]
    E       RuntimeError: dictionary changed size during iteration
    
    self       = 1.25**2
    
    ../sklearn/gaussian_process/kernels.py:280: RuntimeError

  3. scikit-learn-bot commented on Jan 30, 2026

    @scikit-learn-bot
    ContributorAuthor

    CI is no longer failing! ✅

    Successful run on Feb 05, 2026

  4. added
    free-threading
    and removed
    Needs Triage
    on Jan 30, 2026
  5. ogrisel commented on Jan 30, 2026

    @ogrisel
    Member

    Note: this is a specific race condition discovered on the free-threading build that leverages pytest-run-parallel to detect concurrency problems. I labeled this issue as free threading, but it might be possible to trigger it on a regular Python build with pytest-run-parallel (I haven't tried).

  6. lesteve commented on Feb 2, 2026

    @lesteve
    Member

    Side-comment the name of the issue is not very descriptive it should mention the name of the job e.g. Linux x86-64 pylatest_free_threaded in this case. I think otherwise all failures will be grouped in the same tracking issue, cc @FrancoisPgm.

  7. lesteve commented on Feb 2, 2026

    @lesteve
    Member

    FYI I haven't managed to reproduce locally the issue from #33152 (comment). The following command doesn't fail for me:

    pytest sklearn/gaussian_process/tests/test_gpc.py --parallel-threads 4 --iterations 10 -v
    

    The last free-threaded "dictionary changed size during iteration" issue I could remember #32264.

  8. ogrisel commented on Feb 3, 2026

    @ogrisel
    Member

    Note that it should be easy to fix with a .copy() (I think):

        ...
        for attr in dir(self).copy()
        ...

    but it's unfortunate that we cannot easily reproduce to check that this candidate fix actually works.

  9. betatim commented on Feb 5, 2026

    @betatim
    Member

    Does someone know why the dictionary would change? It looks like it should contain the attributes of a class, surely that is a static thing? It makes me think of #33067 - isn't the list of hyperparamters something that is known? Could we compute it once when the class is instantiated? Maybe this would help avoid the problem that is causing this.

  10. lesteve commented on Feb 5, 2026

    @lesteve
    Member

    Does someone know why the dictionary would change?

    Not sure. What happens with pytest-run-parallel is that the same test function is run in multiple separate threads. With parametrized tests, the kernel instances are shared between the two threads see Quansight-Labs/pytest-run-parallel#14 for more details.

    So maybe fitting in one thread modifies the kernel instance while the other thread is iterating over dir(self) (self is a kernel instance). Another potential "fix" would be to do kernel = clone(kernel) in the test maybe?

  11. changed the title [-]⚠️ CI failed on Unit tests (last failure: Jan 29, 2026) ⚠️[/-] [+]Free-threaded failure with dictionary changed size during iteration in gaussian_process/tests/test_gpc.py::test_multi_class_n_jobs[kernel3][/+] on Feb 6, 2026
  12. changed the title [-]Free-threaded failure with dictionary changed size during iteration in gaussian_process/tests/test_gpc.py::test_multi_class_n_jobs[kernel3][/-] [+]Free-threaded failure in `gaussian_process/tests/test_gpc.py::test_multi_class_n_jobs[kernel3]` with `dictionary changed size during iteration`[/+] on Feb 6, 2026
  13. lesteve commented on Feb 6, 2026

    @lesteve
    Member

    Let's see how often this happens in the new automated and updated free-threaded issue #33216.

    I rename the issue title to make it more explicit that this is about this specific failure.

  14. ogrisel commented on Feb 6, 2026

    @ogrisel
    Member

    So maybe fitting in one thread modifies the kernel instance while the other thread is iterating over dir(self) (self is a kernel instance). Another potential "fix" would be to do kernel = clone(kernel) in the test maybe?

    It's true that all parametrized tests with mutable input value such as kernels or estimators should always clone/copy those values to avoid cross-tests side effects.

  15. lesteve commented on Oct 14, 2026

    @lesteve
    Member

    We haven't seen this in the CI again that I could easily find at least. I wasn't able to reproduce locally. Let's close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugfree-threading

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions