From 9f6efcbd28662700680a4bb7546b27863db8b87c Mon Sep 17 00:00:00 2001 From: crusaderky Date: Thu, 2 Jul 2026 17:07:09 +0100 Subject: [PATCH] Fix flaky test_collections_metadata: don't dedupe span metadata by id() Span.add_metadata deduplicated incoming metadata via a class-level set of id(metadata) values that was never pruned. Since CPython recycles the id of garbage-collected objects, the metadata of a new graph submission could collide with the id of a previous, dead metadata dict and be silently dropped, leaving Span.metadata as None. The set also grew unboundedly over the lifetime of the scheduler. The only deduplication actually needed is within a single observe_tasks call, where all tasks of one graph submission share the same metadata object. Do that with a local set instead. Co-Authored-By: Claude Fable 5 --- distributed/spans.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/distributed/spans.py b/distributed/spans.py index a020fa8566..1b94c65092 100644 --- a/distributed/spans.py +++ b/distributed/spans.py @@ -134,7 +134,6 @@ class Span: __weakref__: Any __slots__ = tuple(__annotations__) - _metadata_seen: set[int] = set() def __init__( self, @@ -171,11 +170,7 @@ def parent(self) -> Span | None: return None def add_metadata(self, metadata: SpanMetadata) -> None: - """Add metadata to the span, e.g. code snippets""" - id_ = id(metadata) - if id_ in self._metadata_seen: - return - self._metadata_seen.add(id_) + """Add metadata to the span, e.g. computed collections""" if self._metadata is None: self._metadata = copy.deepcopy(metadata) else: @@ -522,6 +517,9 @@ def observe_tasks( """ out = {} default_span = None + # All tasks of the same graph submission share the same metadata object. + # Add it only once per span. + metadata_added: set[Span] = set() for ts in tss: if ts.annotations is None: @@ -546,7 +544,8 @@ def observe_tasks( if code: span._code[code] = None - if span_metadata: + if span_metadata and span not in metadata_added: + metadata_added.add(span) span.add_metadata(span_metadata) # The span may be completely different from the one referenced by the