Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elastic_transport/_node/_urllib3_chain_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
# specific language governing permissions and limitations
# under the License.

import _ssl # type: ignore
import hashlib
import sys
from binascii import hexlify, unhexlify
from hmac import compare_digest
from typing import Any, List, Optional

import _ssl # type: ignore
import urllib3
import urllib3.connection

Expand Down
3 changes: 2 additions & 1 deletion elastic_transport/_node_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,10 @@ def get(self) -> BaseNode:
return self._all_nodes[self._seed_nodes[0]]

# Filter nodes in 'alive_nodes' to ones not marked as removed.
# Iterate over a copy as other threads may mark nodes dead or alive meanwhile.
nodes = [
node
for node_config, node in self._alive_nodes.items()
for node_config, node in self._alive_nodes.copy().items()
if node_config not in self._removed_nodes
]

Expand Down
17 changes: 17 additions & 0 deletions tests/test_node_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,20 @@ def run(self) -> None:
[thread.join() for thread in threads]

assert sum(thread.nodes_gotten for thread in threads) >= 10000


def test_get_when_alive_nodes_change_during_iteration():
node_configs = [NodeConfig("http", "localhost", x) for x in range(3)]
pool = NodePool(node_configs, node_class=Urllib3HttpNode)
node = pool.all()[0]

class MarkDeadOnFirstLookup(set):
# Simulates another thread calling mark_dead() while get() iterates over the alive nodes.
def __contains__(self, item):
if node.config in pool._alive_nodes:
pool.mark_dead(node)
return super().__contains__(item)

pool._removed_nodes = MarkDeadOnFirstLookup()

assert pool.get().config in node_configs
Loading