Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict memoization sorting before normalizing keys is unsafe #3157

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions parsl/dataflow/memoization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pickle
from parsl.dataflow.taskrecord import TaskRecord

from typing import Dict, Any, List, Optional, TYPE_CHECKING
from typing import Dict, Any, List, Optional, TYPE_CHECKING # avoid circular imports

if TYPE_CHECKING:
from parsl import DataFlowKernel # import loop at runtime - needed for typechecking - TODO turn into "if typing:"
Expand All @@ -14,7 +14,9 @@

import types

logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__) # logger named name for logging purposes

# memoization function with a single dispatch decorator


@singledispatch
Expand Down Expand Up @@ -49,6 +51,8 @@ def id_for_memo(obj: object, output_ref: bool = False) -> bytes:
logger.error("id_for_memo attempted on unknown type {}".format(type(obj)))
raise ValueError("unknown type for memoization: {}".format(type(obj)))

# type specific implementations - handle how each type should be serialized for memoization


@id_for_memo.register(str)
@id_for_memo.register(int)
Expand Down Expand Up @@ -94,10 +98,13 @@ def id_for_memo_dict(denormalized_dict: dict, output_ref: bool = False) -> bytes
if type(denormalized_dict) is not dict:
raise ValueError("id_for_memo_dict cannot work on subclasses of dict")

keys = sorted(denormalized_dict)
# keys = sorted(denormalized_dict) Line that sirosen commented on
# Proposed solution was to normalize the keys and then sort them
keymap = {id_for_memo(k): k for k in denormalized_dict}
normed_keys = sorted(keymap.values())
Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, I think this is right thing to be doing - but I don't think your test case that you added really tests that something was broken before, and is now fixed by this change.

I will try to add some comments on the test case if I can figure out a stronger test case.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you , I've also realised that and was trying to think though it. Where I fell short was I corrected the implementation first, then created the test case later. I should have done vice versa.


normalized_list = []
for k in keys:
for k in normed_keys:
normalized_list.append(id_for_memo(k))
normalized_list.append(id_for_memo(denormalized_dict[k], output_ref=output_ref))
return pickle.dumps(normalized_list)
Expand Down
29 changes: 29 additions & 0 deletions parsl/tests/test_python_apps/test_memoize_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
import enum

# Define an enum - collection of related consonants


class Foo(enum.Enum):
x = enum.auto()
y = enum.auto()


# Test function demonstrating the issue with unstable sorting when keys
# are hashable but not comparable.


def test_unstable_sorting():
# Functions
def foo():
return 1

def bar():
return 2

# Dictionary with problematic keys
d = {foo: 1, bar: 2}

# Sort the dictionary, it should raise a TypeError
with pytest.raises(TypeError):
sorted(d)
Loading