Skip to content

Avoid mutating sampler.pool as a side effect of pickling#572

Merged
dfm merged 1 commit into
dfm:mainfrom
steps-re:fix/getstate-pool-mutation
Jul 12, 2026
Merged

Avoid mutating sampler.pool as a side effect of pickling#572
dfm merged 1 commit into
dfm:mainfrom
steps-re:fix/getstate-pool-mutation

Conversation

@steps-re

Copy link
Copy Markdown
Contributor

EnsembleSampler.__getstate__ did d = self.__dict__ (a reference, not a copy) and then d["pool"] = None. Any pickle, copy.copy, or checkpoint of the sampler therefore permanently nulled the original object's pool, silently breaking parallel sampling for the rest of the run. The fix copies the dict first: d = self.__dict__.copy().

Reproduced with a real multiprocessing.Pool: the live sampler's pool became None after pickle.dumps. Added test_pickle_does_not_mutate_original_pool, which fails before and passes after. Full suite 129/129.

EnsembleSampler.__getstate__ assigned self.__dict__ to a local name
and then set pool to None on it, which mutated the live object in
place. Pickling a sampler (directly, via copy.copy, or via any
library that calls __getstate__) permanently discarded its pool and
broke subsequent parallel sampling on the original instance.

Signed-off-by: Mike German <mike@stepsventures.com>

@dfm dfm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks!

@dfm dfm merged commit 97179ef into dfm:main Jul 12, 2026
7 of 8 checks passed
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