Open elbakramer opened 2 years ago
Hi, thanks for the report. The issue lies in the fact that cloudpickle
does not override pickle
s logic of treating classes deriving from custom metaclasses, not as instances of a metaclass, but as traditional classes, for which __getstate__
is not part of any reducing spec (since there is no official spec on reducing dynamically created classes, only an unofficial cloudpickle implementation).
To fix your case, I suspect (my reasoning comes from pickle
's save logic, on which cloudpickle
relies) you need a custom reducer entry within copyreg.dispatch_table
for SingletonMeta
instances which can piggy-back on cloudpickle's reducer for dynamically created classes, _dynamic_class_reduce. Only problem is that this function is in theory private, so it might be removed without notice.
@elbakramer feel free to try the workaround I proposed. I'm likely to close this soon since unless I'm mistaken, an alternative seems possible.
In the example below, I implemented
__getstate__(cls)
that returns empty dict in order not to picklecls.INSTANCES
andcls.LOCK
members of metaclassSingletonMeta
.But it seems that
cloudpickle
still tries to pickle those members and fails to pickle sincethreading.RLock
is not picklable.