Skip to content

Commit

Permalink
Fix circular referencing within the labone.nodetree
Browse files Browse the repository at this point in the history
This commit adapts the node tree manager to no longer hold
refernces to the created nodes but instead use  weak refernces.

The nodtree manager holds the functionality of creating new nodes
and also the reference to the underlying session. Every node therefore
needs to have a refernce to the manager. To avoid creating n objects for
for the same node the nodes are cached within the nodtree manager. This
caching needs to use weak references otherwise the nodetree will keep
itself alive forever ...
  • Loading branch information
tobiasah committed Feb 7, 2024
1 parent e86f11d commit f710b60
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/labone/nodetree/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import asyncio
import typing as t
import warnings
import weakref
from abc import ABC, abstractmethod
from functools import cached_property

Expand Down Expand Up @@ -79,10 +80,9 @@ def __init__(
self._hide_kernel_prefix = hide_kernel_prefix

self._remembered_nodes: dict[tuple[NormalizedPathSegment, ...], Node] = {}
self._cache_path_segments_to_node: (dict)[
tuple[int, tuple[NormalizedPathSegment, ...]],
Node,
] = {}
self._cache_path_segments_to_node: weakref.WeakValueDictionary[int, Node] = (
weakref.WeakValueDictionary()
)
self._cache_find_substructure: (dict)[
tuple[int, tuple[NormalizedPathSegment, ...]],
NestedDict[list[list[NormalizedPathSegment]] | dict],
Expand Down Expand Up @@ -279,7 +279,7 @@ def path_segments_to_node(
LabOneInvalidPathError: In no subtree_paths are given and the path
is invalid.
"""
unique_value = (hash(self), path_segments)
unique_value = hash(path_segments)
if unique_value in self._cache_path_segments_to_node:
return self._cache_path_segments_to_node[unique_value]

Expand Down

0 comments on commit f710b60

Please sign in to comment.