Skip to content

Commit

Permalink
Ensure profiling messages can be serialized despite deep nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
fjetter committed Mar 8, 2023
1 parent 1035340 commit 7780ad7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion distributed/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def process(
merge
"""
if depth is None:
depth = sys.getrecursionlimit() - 50
# Cut off rather conservatively since the output of the profiling
# sometimes need to be recursed into as well, e.g. for serialization
# which can cause recursion errors later on since this can generate
# deeply nested dictionaries
depth = min(250, sys.getrecursionlimit() // 4)
if depth <= 0:
return None
if any(frame.f_code.co_filename.endswith(o) for o in omit):
Expand Down
19 changes: 19 additions & 0 deletions distributed/protocol/tests/test_protocol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from threading import Thread
from time import sleep

Expand Down Expand Up @@ -393,3 +394,21 @@ def _(header, frames):
header, frames = serialize(MyObj(), serializers=serializers)
o = deserialize(header, frames)
assert isinstance(o, MyObj)


def test_deeply_nested_structures():
# These kind of deeply nested structures are generated in our profiling code
def gen_deeply_nested(depth):
msg = {}
d = msg
while depth:
depth -= 1
d["children"] = d = {}
return msg

msg = gen_deeply_nested(sys.getrecursionlimit() - 100)
with pytest.raises(TypeError, match="Could not serialize object"):
serialize(msg, on_error="raise")

msg = gen_deeply_nested(sys.getrecursionlimit() // 4)
assert isinstance(serialize(msg), tuple)
2 changes: 1 addition & 1 deletion distributed/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def f(i):
else:
return f(i - 1)

f(sys.getrecursionlimit() - 40)
f(sys.getrecursionlimit() - 100)
process(frame, None, state)
merge(state, state, state)

Expand Down

0 comments on commit 7780ad7

Please sign in to comment.