Skip to content

Fix : Issue #34792 about process parallelism on python 3.14 - #2

Open
Prathamj07 wants to merge 1 commit into
mainfrom
scikit/34792
Open

Prathamj07 wants to merge 1 commit into
mainfrom
scikit/34792

Conversation

@Prathamj07

Copy link
Copy Markdown
Collaborator

Reference Issues/PRs

Fixes scikit-learn#34792.

What does this implement/fix? Explain your changes.

Pickling an estimator with a callback can race with the callback's listener thread.

When joblib uses prefer="processes", cloudpickle serializes the estimator and its callbacks to send the next task to a worker. At the same time, a previous worker can still be sending callback messages to the main process. The listener thread appends to the callback's list while pickle is iterating over it, which can raise:

RuntimeError: list changed size during iteration

This is then raised by joblib as a PicklingError.

The issue was easier to reproduce on Python 3.14 / i586 due to slower scheduling. The same problem also exists with ScoringMonitor.

This PR fixes the race by:

  • Adding a threading.Lock to ScoringMonitor and RecordingCallback.
  • Locking the callback history while appending and while creating the pickle state.
  • Adding __getstate__ to copy the history safely and exclude the lock from the pickle.
  • Adding __setstate__ to recreate the lock after unpickling.
  • Adding a regression test that pickles the callback while another thread is delivering messages.

Introduce yourself

I use scikit-learn for model training and evaluation. I picked this issue up after seeing the process-parallelism pickle race described in scikit-learn#34792.

AI usage disclosure

I used AI assistance for:

  • Code generation
  • Test generation
  • Research and understanding

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race when pickling RecordingCallback during process-based parallelism on Python 3.14

1 participant