Skip to content

Commit

Permalink
fix(id_generation): unique ID fallback for unpickleable invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
nkemnitz committed Jan 5, 2024
1 parent 9507b7c commit 7af7735
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
9 changes: 9 additions & 0 deletions tests/unit/mazepa/test_id_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,12 @@ def test_persistence_across_sessions() -> None:
result = pool.map(_gen_id_calls, range(2))

assert result[0] == result[1]


def test_unpickleable_fn(mocker) -> None:
# See https://github.com/uqfoundation/dill/issues/147 and possibly
# https://github.com/uqfoundation/dill/issues/56

unpickleable_fn = mocker.MagicMock()
# gen_id will return a random UUID in case of pickle errors
assert gen_id(unpickleable_fn, [], {}) != gen_id(unpickleable_fn, [], {})
21 changes: 13 additions & 8 deletions zetta_utils/mazepa/id_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ def generate_invocation_id(
prefix: Optional[str] = None,
):
x = xxhash.xxh128()
x.update(
dill.dumps(
(fn, args, kwargs),
protocol=dill.DEFAULT_PROTOCOL,
byref=False,
recurse=True,
fmode=dill.FILE_FMODE,
try:
x.update(
dill.dumps(
(fn, args, kwargs),
protocol=dill.DEFAULT_PROTOCOL,
byref=False,
recurse=True,
fmode=dill.FILE_FMODE,
)
)
)
except dill.PicklingError as e:
logger.warning(f"Failed to pickle {fn} with args {args} and kwargs {kwargs}: {e}")
x.update(str(uuid.uuid4()))

if prefix is not None:
return f"{prefix}-{x.hexdigest()}"
else:
Expand Down

0 comments on commit 7af7735

Please sign in to comment.