Skip to content

FIX Preserve partial callback state across workers - #676

Open
antonsoo wants to merge 2 commits into
joblib:masterfrom
antonsoo:fix/partial-instance-state
Open

antonsoo wants to merge 2 commits into
joblib:masterfrom
antonsoo:fix/partial-instance-state

Conversation

@antonsoo

@antonsoo antonsoo commented Oct 18, 2026 •

Copy link
Copy Markdown

Loky's partial reducer reconstructs the callable and arguments but loses its instance attributes. For example, sending a SciPy event callback created with functools.partial to a worker drops .terminal, so integration continues past the requested stop.

Remove the custom partial reducer and let the standard pickler preserve its dictionary, aliases and recursive state.

Validation:

  • All 36 regressions fail before and pass after, covering both picklers and protocols 0-5. The serialization/wrapper family passes on Python 3.11 and 3.14.
  • Actual two-worker SciPy simulations correct 24 stopping cases across 72 configurations; 48 complete controls stay exact, and all repaired worker results match serial results.
  • Python 3.12 native suite passes, with Docker integration and high-memory checks excluded on this WSL host.
  • Changed-file hooks pass. Loky pickle artifacts are internal, short-lived messages; no long-term artifact compatibility is promised.

Comment thread loky/backend/reduction.py Outdated
# Make partial func pickable
def _reduce_partial(p):
return _rebuild_partial, (p.func, p.args, p.keywords or {})
return p.__reduce__()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that functools.partial instances are always picklable by default in all the Python version we support? If that's the case, we could just remove _reduce_partial / _rebuild_partial and register(functools.partial, _reduce_partial), no?

Keep the old reconstruction helper available for existing pickle artifacts.

I don't think we need this. Those custom pickling stuff are only meant to be privately used by loky internally for short lived commuication between a driver process and its worker process. Loky users should never keep long lived pickle artifacts on their hosts.

@antonsoo antonsoo Oct 27, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Current master requires Python 3.11 or newer, whose standard partial reduction preserves this state. I removed the reducer, registration, reconstruction helper, and unused functools import. The serialization/wrapper checks pass on Python 3.11 and 3.14, and all 72 two-worker SciPy simulation results still match their serial references. I also removed the artifact-compatibility claim from the description.

Correction to the earlier version note: Python 3.10 was tested as extra compatibility, but is no longer supported by current master.

Use the standard partial reduction so callbacks retain their custom attributes, aliases and recursive keyword arguments when dispatched to workers. Keep the old reconstruction helper available for previously produced pickle artifacts. Cover both configured picklers and every supported pickle protocol.
@antonsoo
antonsoo force-pushed the fix/partial-instance-state branch from f91bda9 to 1bdfc3a Compare October 27, 2026 19:36

This branch has not been deployed

No deployments
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.

2 participants