Skip to content

Commit 94ce3e1

Browse files
committed
chore: drain topology util thread pools before shutting down monitor services
1 parent 24084ad commit 94ce3e1

11 files changed

Lines changed: 46 additions & 8 deletions

‎aws_advanced_python_wrapper/host_list_provider.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ def __init__(self, dialect: db_dialect.TopologyAwareDatabaseDialect, props: Prop
464464

465465
self.instance_template: HostInfo = instance_template
466466
self._max_timeout_sec = WrapperProperties.AUXILIARY_QUERY_TIMEOUT_SEC.get_int(props)
467-
self._thread_pool = services_container.get_thread_pool(self._executor_name)
467+
# Need to drain topology query pools before monitor services shut down.
468+
self._thread_pool = services_container.get_thread_pool(self._executor_name, drain_first=True)
469+
470+
@classmethod
471+
def release_resources(cls, wait: bool = True) -> bool:
472+
return services_container.release_thread_pool(cls._executor_name, wait=wait)
468473

469474
def _validate_host_pattern(self, host: str):
470475
if not self._rds_utils.is_dns_pattern_valid(host):
@@ -495,8 +500,9 @@ def query_for_topology(
495500
:return: a tuple of :py:class:`HostInfo` objects representing the database topology. If the query results did not include a writer instance,
496501
an empty tuple will be returned.
497502
"""
503+
thread_pool = services_container.get_thread_pool(self._executor_name, drain_first=True)
498504
query_for_topology_func_with_timeout = preserve_transaction_status_with_timeout(
499-
self._thread_pool, self._max_timeout_sec, driver_dialect, conn)(self._query_for_topology)
505+
thread_pool, self._max_timeout_sec, driver_dialect, conn)(self._query_for_topology)
500506
x = query_for_topology_func_with_timeout(conn)
501507
return x
502508

‎aws_advanced_python_wrapper/utils/services_container.py‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import threading
1818
from concurrent.futures import ThreadPoolExecutor
1919
from datetime import timedelta
20-
from typing import Dict, Optional
20+
from typing import Dict, Optional, Set
2121

2222
from aws_advanced_python_wrapper.allowed_and_blocked_hosts import \
2323
AllowedAndBlockedHosts
@@ -37,6 +37,10 @@ def __init__(self) -> None:
3737
self._storage_service: Optional[StorageService] = None
3838
self._monitor_service: Optional[MonitorService] = None
3939
self._thread_pools: Dict[str, ThreadPoolExecutor] = {}
40+
# Some service pools must be drained BEFORE the monitor service is released and connections are closed.
41+
# This prevents worker threads like the topology util threads from continuing to using connections
42+
# after they are closed, causing segfaults.
43+
self._drain_first_pools: Set[str] = set()
4044
self._lock = threading.Lock()
4145

4246
def _ensure_initialized(self) -> None:
@@ -63,19 +67,24 @@ def monitor_service(self) -> MonitorService:
6367
self._ensure_initialized()
6468
return self._monitor_service # type: ignore
6569

66-
def get_thread_pool(self, name: str, max_workers: Optional[int] = None) -> ThreadPoolExecutor:
70+
def get_thread_pool(self, name: str, max_workers: Optional[int] = None, drain_first: bool = False) -> ThreadPoolExecutor:
6771
pool = self._thread_pools.get(name)
6872
if pool is not None:
73+
if drain_first:
74+
self._drain_first_pools.add(name)
6975
return pool
7076
with self._lock:
7177
if name not in self._thread_pools:
7278
self._thread_pools[name] = ThreadPoolExecutor(
7379
max_workers=max_workers, thread_name_prefix=name)
80+
if drain_first:
81+
self._drain_first_pools.add(name)
7482
return self._thread_pools[name]
7583

7684
def release_thread_pool(self, name: str, wait: bool = True) -> bool:
7785
with self._lock:
7886
pool = self._thread_pools.pop(name, None)
87+
self._drain_first_pools.discard(name)
7988
if pool is not None:
8089
try:
8190
pool.shutdown(wait=wait)
@@ -85,6 +94,18 @@ def release_thread_pool(self, name: str, wait: bool = True) -> bool:
8594
return False
8695

8796
def release_resources(self) -> None:
97+
# Some thread pools need to be drained first before shutting down the monitor services.
98+
# This prevents segfaults when monitor services shut down and close all the active monitoring connections.
99+
with self._lock:
100+
drain_names = list(self._drain_first_pools)
101+
for name in drain_names:
102+
pool = self._thread_pools.get(name)
103+
if pool is not None:
104+
try:
105+
pool.shutdown(wait=True)
106+
except Exception as e:
107+
logger.debug("CoreServices.ErrorShuttingDownPool", name, e)
108+
88109
if self._monitor_service is not None:
89110
try:
90111
self._monitor_service.release_resources()
@@ -114,6 +135,7 @@ def release_resources(self) -> None:
114135
except Exception as e:
115136
logger.debug("CoreServices.ErrorShuttingDownPool", name, e)
116137
self._thread_pools.clear()
138+
self._drain_first_pools.clear()
117139

118140

119141
_instance = _ServicesContainer()
@@ -132,8 +154,8 @@ def get_monitor_service() -> MonitorService:
132154
return _instance.monitor_service
133155

134156

135-
def get_thread_pool(name: str, max_workers: Optional[int] = None) -> ThreadPoolExecutor:
136-
return _instance.get_thread_pool(name, max_workers)
157+
def get_thread_pool(name: str, max_workers: Optional[int] = None, drain_first: bool = False) -> ThreadPoolExecutor:
158+
return _instance.get_thread_pool(name, max_workers, drain_first)
137159

138160

139161
def release_thread_pool(name: str, wait: bool = True) -> bool:

‎tests/integration/container/test_aurora_failover.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def props(self):
7575
"connect_timeout": 10,
7676
"monitoring-connect_timeout": 5,
7777
"monitoring-socket_timeout": 5,
78+
"auxiliary_query_timeout_sec": 3,
7879
"autocommit": True,
7980
"cluster_id": "cluster1"
8081
})

‎tests/integration/container/test_autoscaling.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def rds_utils(self):
5151

5252
@pytest.fixture
5353
def props(self):
54-
p: Properties = Properties({"plugins": "read_write_splitting", "connect_timeout": 10, "autocommit": True})
54+
p: Properties = Properties({"plugins": "read_write_splitting", "connect_timeout": 10, "auxiliary_query_timeout_sec": 3, "autocommit": True})
5555

5656
if TestEnvironmentFeatures.TELEMETRY_TRACES_ENABLED in TestEnvironment.get_current().get_features() \
5757
or TestEnvironmentFeatures.TELEMETRY_METRICS_ENABLED in TestEnvironment.get_current().get_features():

‎tests/integration/container/test_aws_secrets_manager.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def test_failover_with_secrets_manager(
221221
"connect_timeout": 10,
222222
"monitoring-connect_timeout": 5,
223223
"monitoring-socket_timeout": 5,
224+
"auxiliary_query_timeout_sec": 3,
224225
"topology_refresh_ms": 10,
225226
})
226227

‎tests/integration/container/test_basic_connectivity.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def props(self):
5454
p: Properties = Properties({
5555
WrapperProperties.PLUGINS.name: "aurora_connection_tracker,failover",
5656
"connect_timeout": 3,
57+
"auxiliary_query_timeout_sec": 3,
5758
"autocommit": True,
5859
"cluster_id": "cluster1"})
5960

@@ -143,6 +144,7 @@ def test_wrapper_connection_reader_cluster_with_efm_enabled(self, test_driver: T
143144
"connect_timeout": 5,
144145
"monitoring-connect_timeout": 3,
145146
"monitoring-socket_timeout": 3,
147+
"auxiliary_query_timeout_sec": 3,
146148
"autocommit": True})
147149
target_driver_connect = DriverHelper.get_connect_func(test_driver)
148150
conn = AwsWrapperConnection.connect(target_driver_connect, **conn_utils.get_connect_params(conn_utils.reader_cluster_host), **props)

‎tests/integration/container/test_basic_functionality.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def props(self):
6161
p: Properties = Properties({
6262
"plugins": "aurora_connection_tracker,failover",
6363
"connect_timeout": 10,
64+
"auxiliary_query_timeout_sec": 3,
6465
"autocommit": True,
6566
"cluster_id": "cluster1"})
6667

‎tests/integration/container/test_custom_endpoint.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def rds_utils(self):
6363
@pytest.fixture(scope='class')
6464
def default_props(self):
6565
p: Properties = Properties(
66-
{"connect_timeout": 10_000, "autocommit": True, "cluster_id": "cluster1"})
66+
{"connect_timeout": 10_000, "autocommit": True, "cluster_id": "cluster1",
67+
"auxiliary_query_timeout_sec": 3})
6768

6869
features = TestEnvironment.get_current().get_features()
6970
if TestEnvironmentFeatures.TELEMETRY_TRACES_ENABLED in features \

‎tests/integration/container/test_host_monitoring_v2.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def props(self):
5959
"connect_timeout": 10,
6060
"monitoring-connect_timeout": 5,
6161
"monitoring-socket_timeout": 5,
62+
"auxiliary_query_timeout_sec": 3,
6263
"failure_detection_time_ms": 5_000,
6364
"failure_detection_interval_ms": 5_000,
6465
"failure_detection_count": 1,

‎tests/integration/container/test_iam_authentication.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def test_failover_with_iam(
150150
"connect_timeout": 10,
151151
"monitoring-connect_timeout": 5,
152152
"monitoring-socket_timeout": 5,
153+
"auxiliary_query_timeout_sec": 3,
153154
"topology_refresh_ms": 10,
154155
"autocommit": True
155156
})

0 commit comments

Comments
 (0)