diff --git a/neo4j/io/__init__.py b/neo4j/io/__init__.py index 7e0c0c009..7b8c63ee1 100644 --- a/neo4j/io/__init__.py +++ b/neo4j/io/__init__.py @@ -1334,10 +1334,11 @@ def deactivate(self, address): log.debug("[#0000] C: Deactivating address %r", address) # We use `discard` instead of `remove` here since the former # will not fail if the address has already been removed. - for database in self.routing_tables.keys(): - self.routing_tables[database].routers.discard(address) - self.routing_tables[database].readers.discard(address) - self.routing_tables[database].writers.discard(address) + with self.refresh_lock: + for database in self.routing_tables.keys(): + self.routing_tables[database].routers.discard(address) + self.routing_tables[database].readers.discard(address) + self.routing_tables[database].writers.discard(address) log.debug("[#0000] C: table=%r", self.routing_tables) super(Neo4jPool, self).deactivate(address) @@ -1345,8 +1346,9 @@ def on_write_failure(self, address): """ Remove a writer address from the routing table, if present. """ log.debug("[#0000] C: Removing writer %r", address) - for database in self.routing_tables.keys(): - self.routing_tables[database].writers.discard(address) + with self.refresh_lock: + for database in self.routing_tables.keys(): + self.routing_tables[database].writers.discard(address) log.debug("[#0000] C: table=%r", self.routing_tables)