Skip to content

Commit 2a2dc83

Browse files
authored
test(bigquery): add debug info to socket leak test (#17802)
## Problem The system test `test_dbapi_connection_does_not_leak_sockets` has been failing intermittently with minor socket count discrepancies (e.g., 7 instead of 6). Previous attempts to fix this have not fully eliminated the flakiness, likely due to non-deterministic garbage collection or Operating System cleanup delays. Without knowing the exact state of these "leaked" sockets (e.g., `TIME_WAIT` vs `CLOSE_WAIT`), it is difficult to determine if this is a real resource leak in the application or just OS noise. ## Solution This PR wraps the socket count assertion in a `try...except AssertionError` block. If the assertion fails, it captures the status, local address, and remote address of all active network connections for the current process and appends this information to the failure message. ## Notes to Reviewers - This change does **not** attempt to fix the underlying flakiness or leak. It is purely diagnostic. - The detailed socket state will help us distinguish between expected OS delays (`TIME_WAIT`) and real application leaks (`CLOSE_WAIT` or `ESTABLISHED`) in future CI/CD runs. - This approach avoids running expensive system tests repeatedly to try and capture an intermittent failure manually.
1 parent b2d3fb9 commit 2a2dc83

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,26 @@ def test_dbapi_connection_does_not_leak_sockets(self):
22132213
break
22142214
time.sleep(0.1)
22152215

2216-
self.assertLessEqual(conn_count_end, conn_count_start)
2216+
try:
2217+
self.assertLessEqual(conn_count_end, conn_count_start)
2218+
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.
2223+
conn_debug = [
2224+
f"Status: {c.status}, Laddr: {c.laddr}, Raddr: {c.raddr}"
2225+
for c in current_process.net_connections()
2226+
]
2227+
debug_msg = "\n".join(conn_debug)
2228+
2229+
raise AssertionError(
2230+
f"{e}\n\n"
2231+
f"--- Socket Leak Debug Info ---\n"
2232+
f"Start Count: {conn_count_start}\n"
2233+
f"End Count: {conn_count_end}\n"
2234+
f"Current Connections:\n{debug_msg}"
2235+
)
22172236

22182237
def _load_table_for_dml(self, rows, dataset_id, table_id):
22192238
from google.cloud._testing import _NamedTemporaryFile

0 commit comments

Comments
 (0)