Avoid runtime errors in zk node iterator

While iterating over zk nodes with node IDs from the from the node
cache, there can be runtime errors when the cache was updated during the
iteration process.
(``RuntimeError: dictionary changed size during iteration``)

Avoid this by iterating over a copy of the node IDs, rather than the
dict_keys directly.

Change-Id: Iecd88b4484cf48ea2127348bfb2905443eaaf49f
This commit is contained in:
Benjamin Schanzel 2022-01-28 13:32:10 +01:00
parent 447547273b
commit 1a73a7a33e
No known key found for this signature in database
1 changed files with 6 additions and 1 deletions

View File

@ -2271,7 +2271,12 @@ class ZooKeeper(object):
cache.
'''
node_ids = self._cached_nodes.keys() if cached_ids else self.getNodes()
if cached_ids:
# get a copy of the keys view to avoid runtime errors in the event
# the _cached_nodes dict gets updated while iterating
node_ids = list(self._cached_nodes.keys())
else:
node_ids = self.getNodes()
for node_id in node_ids:
node = self.getNode(node_id, cached=cached)