Skip to content

FIX Don't reset the warning filters in each task of thread-based Parallel - #35090

Closed
cakedev0 wants to merge 9 commits into
scikit-learn:mainfrom
cakedev0:fix/parallel-warning-filters-threads
Closed

cakedev0 wants to merge 9 commits into
scikit-learn:mainfrom
cakedev0:fix/parallel-warning-filters-threads

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Stalled for now, see #35089 (comment)

Reference Issues/PRs

Fixes #35089. Also removes the xfail added for #32631 in #33320.

What does this implement/fix? Explain your changes.

Tasks that already run with the caller's warning filters no longer reset and refill them:

  • Without context-aware warnings (GIL builds, free-threaded 3.13), the filters are process-wide, so that's every task running in the caller's process: worker threads, and n_jobs=1. Setting the filters there needs catch_warnings, which isn't thread-safe.
  • With context-aware warnings, it's the tasks whose context has the caller's filter list: the caller's thread, or a thread that inherited its context (the default on free-threaded 3.14).

To know whether a task runs in the caller's process, Parallel stores a module-level object() token on each task. A task unpickled in another process (loky, multiprocessing, dask, ...) gets a new object, so it sets the filters as before. I use this rather than the pid because a pid doesn't identify a process across hosts or containers.

Two small related fixes: an empty list of caller filters (e.g. after warnings.resetwarnings()) no longer raises the warning about sklearn.utils.parallel.delayed, and tasks dispatched by joblib's Parallel, whose caller filters are unknown, now keep their filters (they were reset to an empty list).

The new tests fail on main, except test_pickled_task_sets_warning_filters, which guards the pickled path. With the reproducer of #35089, the filters are now unchanged and no warning is raised. On free-threaded 3.13 with pytest-run-parallel, test_no_user_warning_with_scoring emitted the warning in 6 of 8 runs on main, and in 0 of 5 runs with the first version of this PR (same code path for these tasks).

AI usage disclosure: yes, a lot

Any other comments?

Tasks running in the caller's process no longer run inside catch_warnings, so they behave like any other code in that process, which changes a few things:

  • a task calling warnings.simplefilter outside of catch_warnings changes the caller's filters (sklearn code always does it inside catch_warnings);
  • without context-aware warnings, tasks use the current filters, not the ones captured when Parallel was called (it matters for a generator output consumed under other filters);
  • a warning raised with the same message in each task is shown once instead of once per task, since no catch_warnings resets the registry between tasks anymore. sklearn's own warnings look unaffected: a ConvergenceWarning in cross_val_score is still shown for each fold, because other catch_warnings calls in fit reset the registry anyway.

What remains is catch_warnings itself: it isn't thread-safe with process-wide filters, and sklearn also calls it inside the tasks. The main one is in check_array, which turns ComplexWarning into an error: with 32 threads calling check_array, this error filter stays in the global filters for good (3 runs out of 3 on GIL 3.13 and on 3.13t, with plain threads, so with or without this PR). I'll open a separate issue and PR to remove the catch_warnings from check_array.

Tested on GIL 3.13 and 3.14, and on free-threaded 3.13t and 3.14t (3.14t also with -X context_aware_warnings=0 and -X thread_inherit_context=0).

cakedev0 and others added 3 commits October 6, 2026 14:48
…llel

catch_warnings and resetwarnings swap the process-wide warning filters,
which isn't thread-safe: with many threads, tasks could leave the
filters stale or reset, losing the user's filters and raising spurious
warnings. Threads of the caller's process (or inheriting its context,
with context-aware warnings) already have the caller's filters, so they
are now kept as is. This also removes a contended lock on free-threaded
Python.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
….13t

The intermittent failure (scikit-learn#32631) came from the warning filters race
fixed in the previous commit: tasks resetting the process-wide filters
could empty the filter list captured by Parallel, so later tasks warned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0 and others added 2 commits October 6, 2026 16:49
A pid doesn't identify a process across hosts or containers. Also
check the context-aware warnings flag once, share the call path of
both cases, and test the n_jobs=1 and pickled paths.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0 and others added 4 commits October 6, 2026 17:41
Use None for the config and filters that sklearn's Parallel didn't set,
so that an empty list of filters no longer triggers the warning about
joblib's Parallel, and tasks dispatched by joblib's Parallel keep their
filters on every Python build. Also document in the changelog that
in-process tasks use the filters active when they run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A token set at dispatch survives a shallow copy and is replaced by a
new object when the task is unpickled, so copying a task, or pickling
it before dispatch, no longer sends it down the reset path. Also reuse
_get_warning_filters and _CONTEXT_AWARE_WARNINGS in the tests, and note
in the changelog that filter changes made by in-process tasks persist.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cakedev0

Copy link
Copy Markdown
Member Author

Closing for now.

@cakedev0 cakedev0 closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: thread-based Parallel can corrupt the process-wide warning filters

1 participant