Fix save_binary TypeError on objects holding a Cluster#415
Merged
mesonepigreco merged 2 commits intoJun 10, 2026
Conversation
Cluster.compute_ensemble_batch stores a threading.Lock on the cluster and never clears it, so sscha.Utilities.save_binary failed with TypeError: cannot pickle '_thread.lock' object on any relax or minimizer object holding a cluster after a calculation. Add __getstate__/__setstate__ on Cluster that drop the lock on save and reset it to None on load, matching the state after __init__. compute_ensemble_batch recreates the lock when needed, so plain pickle round trips work and the restored object stays usable. Fixes SSCHAcode#114
mesonepigreco
approved these changes
Jun 10, 2026
mesonepigreco
left a comment
Collaborator
There was a problem hiding this comment.
The edits are extremely local; the test suite also passes in the GitHub workflow.
It is safe to approve.
Thanks a lot! :)
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.
Fixes #114.
sscha.Utilities.save_binaryraisedTypeError: cannot pickle '_thread.lock' objectwhen saving asscha.Relax.SSCHAobject after a relaxation. The lock comes fromCluster.compute_ensemble_batch, which setsself.lock = threading.Lock()and never clears it, so any object referencing the cluster after a calculation cannot be pickled.Instead of replacing pickle with dill, this adds
__getstate__/__setstate__tosscha.Cluster.Cluster. The lock is transient runtime state: it is dropped on save and reset toNoneon load (the same value__init__assigns), andcompute_ensemble_batchrecreates it on the next run. Plain pickle now round-trips, existing binaries stay loadable, and the restored object remains usable (see also #105).A regression test in
tests/test_save_binary/builds a minimal relax object fromExamples/ensemble_data_testwith a cluster in the post-calculation state, saves it withsave_binary, reloads it withload_binary, and checks the restored state.Verification
Before the fix:
After the fix: