Skip to content

tests(bigquery): implement robust wait loop for socket leak tests#17688

Merged
parthea merged 2 commits into
mainfrom
fix/bigquery-socket-leak-robust
Jul 13, 2026
Merged

tests(bigquery): implement robust wait loop for socket leak tests#17688
parthea merged 2 commits into
mainfrom
fix/bigquery-socket-leak-robust

Conversation

@chalmerlowe

Copy link
Copy Markdown
Contributor

Intent

This PR addresses flakiness in the google-cloud-bigquery system tests that verify system resource cleanup (specifically open sockets).

The Problem

Some system tests in google-cloud-bigquery were performing instantaneous assertions on the number of open sockets immediately after client closure or garbage collection. These assertions are fragile because underlying transport layers (like gRPC) and OS socket reclamation can occur asynchronously in background threads. This leads to intermittent failures (flakiness) especially under varied environmental loads or parallel execution.

Solution

Refactored the fragile instantaneous checks in test_client.py and test_magics.py to use a Robust Wait-Loop pattern.

  • The tests now wait up to 3 seconds, checking periodically, to allow asynchronous cleanup to complete before asserting.
  • This pattern is already proven effective in other packages like google-cloud-pubsub.
  • Added explicit gc.collect() to test_magics.py to align with best practices for resource verification tests.

Note

Also includes some linting updates. Sorry.

@chalmerlowe chalmerlowe self-assigned this Jul 9, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@chalmerlowe chalmerlowe marked this pull request as ready for review July 13, 2026 13:49
@chalmerlowe chalmerlowe requested review from a team as code owners July 13, 2026 13:49
@chalmerlowe chalmerlowe requested review from GarrettWu and removed request for a team July 13, 2026 13:49
@chalmerlowe chalmerlowe added the automerge Merge the pull request once unit tests and other checks pass. label Jul 13, 2026
@parthea parthea changed the title fix(bigquery): implement robust wait loop for socket leak tests tests(bigquery): implement robust wait loop for socket leak tests Jul 13, 2026
@parthea

parthea commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request reorganizes imports and introduces retry loops in several socket leak tests to wait up to 3 seconds for connections to close before making assertions. The feedback suggests moving the gc.collect() call inside these retry loops to ensure any objects that become eligible for garbage collection during the sleep intervals are collected and their sockets are properly released.

Comment on lines +255 to +260
for _ in range(30): # Wait up to 3 seconds
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure that any socket-holding objects that become eligible for garbage collection during the sleep intervals are actually collected and their sockets released, gc.collect() should be called inside the wait loop rather than only once before it.

Suggested change
for _ in range(30): # Wait up to 3 seconds
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)
for _ in range(30): # Wait up to 3 seconds
gc.collect()
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)

Comment on lines +2209 to +2214
for _ in range(30): # Wait up to 3 seconds
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure that any socket-holding objects that become eligible for garbage collection during the sleep intervals are actually collected and their sockets released, gc.collect() should be called inside the wait loop rather than only once before it.

Suggested change
for _ in range(30): # Wait up to 3 seconds
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)
for _ in range(30): # Wait up to 3 seconds
gc.collect()
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)

Comment on lines +79 to +84
for _ in range(30): # Wait up to 3 seconds
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure that any socket-holding objects that become eligible for garbage collection during the sleep intervals are actually collected and their sockets released, gc.collect() should be called inside the wait loop rather than only once before it.

Suggested change
for _ in range(30): # Wait up to 3 seconds
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)
for _ in range(30): # Wait up to 3 seconds
gc.collect()
conn_end = current_process.net_connections()
conn_count_end = len(conn_end)
if conn_count_end <= conn_count_start:
break
time.sleep(0.1)

@parthea parthea merged commit 8feb1b8 into main Jul 13, 2026
30 checks passed
@parthea parthea deleted the fix/bigquery-socket-leak-robust branch July 13, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Merge the pull request once unit tests and other checks pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants