Repository navigation
Conversation
…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]>
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 of 11 tasks
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]>
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Member
Author
|
Closing for now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
n_jobs=1. Setting the filters there needscatch_warnings, which isn't thread-safe.To know whether a task runs in the caller's process,
Parallelstores a module-levelobject()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 aboutsklearn.utils.parallel.delayed, and tasks dispatched by joblib'sParallel, whose caller filters are unknown, now keep their filters (they were reset to an empty list).The new tests fail on
main, excepttest_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_scoringemitted the warning in 6 of 8 runs onmain, 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:warnings.simplefilteroutside ofcatch_warningschanges the caller's filters (sklearn code always does it insidecatch_warnings);Parallelwas called (it matters for a generator output consumed under other filters);catch_warningsresets the registry between tasks anymore. sklearn's own warnings look unaffected: aConvergenceWarningincross_val_scoreis still shown for each fold, because othercatch_warningscalls infitreset the registry anyway.What remains is
catch_warningsitself: it isn't thread-safe with process-wide filters, and sklearn also calls it inside the tasks. The main one is incheck_array, which turnsComplexWarninginto an error: with 32 threads callingcheck_array, thiserrorfilter 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 thecatch_warningsfromcheck_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=0and-X thread_inherit_context=0).