tests(bigquery): implement robust wait loop for socket leak tests#17688
Conversation
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
/gemini review |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
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.
| 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) |
Intent
This PR addresses flakiness in the
google-cloud-bigquerysystem tests that verify system resource cleanup (specifically open sockets).The Problem
Some system tests in
google-cloud-bigquerywere 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.pyandtest_magics.pyto use a Robust Wait-Loop pattern.google-cloud-pubsub.gc.collect()totest_magics.pyto align with best practices for resource verification tests.Note
Also includes some linting updates. Sorry.