Skip to content

Commit fa01f45

Browse files
committed
test(bigquery): track socket addresses in test_dbapi_connection_does_not_leak_sockets
1 parent 4d3c801 commit fa01f45

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

packages/google-cloud-bigquery/tests/system/test_client.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,7 @@ def test_dbapi_connection_does_not_leak_sockets(self):
21842184
pytest.importorskip("google.cloud.bigquery_storage")
21852185
current_process = psutil.Process()
21862186
conn_start = current_process.net_connections()
2187+
conn_start_addrs = {c.laddr for c in conn_start if c.laddr}
21872188
conn_count_start = len(conn_start)
21882189

21892190
with helpers.patch_tracked_requests():
@@ -2209,17 +2210,18 @@ def test_dbapi_connection_does_not_leak_sockets(self):
22092210
for _ in range(60): # Wait up to 6 seconds for background socket cleanup
22102211
conn_end = current_process.net_connections()
22112212
conn_count_end = len(conn_end)
2212-
if conn_count_end <= conn_count_start:
2213+
new_conns_remaining = [
2214+
c for c in conn_end if c.laddr and c.laddr not in conn_start_addrs
2215+
]
2216+
if conn_count_end <= conn_count_start or len(new_conns_remaining) == 0:
22132217
break
22142218
time.sleep(0.1)
22152219

22162220
try:
2217-
self.assertLessEqual(conn_count_end, conn_count_start)
2221+
self.assertTrue(
2222+
conn_count_end <= conn_count_start or len(new_conns_remaining) == 0
2223+
)
22182224
except AssertionError as e:
2219-
# Due to flakiness in this test (likely caused by OS cleanup delays or
2220-
# non-deterministic garbage collection of sockets), we want to capture
2221-
# the detailed state of connections in future failing runs to help
2222-
# decrease false positives and identify the root cause.
22232225
conn_debug = [
22242226
f"Status: {c.status}, Laddr: {c.laddr}, Raddr: {c.raddr}"
22252227
for c in current_process.net_connections()
@@ -2231,6 +2233,7 @@ def test_dbapi_connection_does_not_leak_sockets(self):
22312233
f"--- Socket Leak Debug Info ---\n"
22322234
f"Start Count: {conn_count_start}\n"
22332235
f"End Count: {conn_count_end}\n"
2236+
f"New Sockets Remaining: {len(new_conns_remaining)}\n"
22342237
f"Current Connections:\n{debug_msg}"
22352238
)
22362239

0 commit comments

Comments
 (0)