fix(bigquery): harden system test teardown and unit test auth isolation - #17956
fix(bigquery): harden system test teardown and unit test auth isolation#17956shuoweil wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves resource cleanup and socket leak prevention in the BigQuery DB-API connection implementation, alongside updating test assertions and IPython magic tests. Specifically, it ensures cursors are closed prior to closing the client connections, handles NotFound exceptions during tag deletions in system tests, and updates the socket leak test to track specific connection addresses and explicitly close cursors. Additionally, unit tests for IPython magics are updated to use IPython.core.interactiveshell and modern parenthesized context managers. Review feedback suggests wrapping the cursor closing loop in a try...except block to prevent an exception in one cursor from blocking the closure of other cursors and the underlying clients.
…nection.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
Googlers see b/540939659 |
Thanks @parthea. I will trim down this PR and update the bug. |
This Pull Request addresses flakiness in the socket leak system test and improves resource cleanup in the Database API (DB-API) connection. ## Problem 1. The test `test_dbapi_connection_does_not_leak_sockets` was flaky in Continuous Integration (CI) environments because it relied on the `psutil` library to count Operating System sockets. Socket pooling, delayed garbage collection, and background noise (other processes in multiprocess systems creating sockets in parallelized systems) make counting Operating System sockets non-deterministic. 2. Closing a database connection could leave underlying resources hanging if cursors were not closed in the correct order or if clients were not fully initialized. ## Solution 1. **Deterministic Testing:** Rewrote the test to use **Whitebox Object Tracking**. Instead of counting Operating System sockets, the test intercepts the creation and closure of `requests.Session` and `grpc.Channel` objects using instance-level monkey patching. This ensures that every session or channel opened by the connection is explicitly accounted for and closed, independent of Operating System quirks. 2. **Improved Cleanup:** Updated `Connection.close()` in `connection.py` to close all created cursors **before** closing the clients themselves. This ensures that cursors release their references to query data and transports first. Added defensive checks to prevent errors if clients are `None`. > [!note] > - This test no longer uses `psutil` or artificial sleeps (that were used to try and wait long enough for garbage collection to kick in), making it faster and fully deterministic. > - Uses Python `sets` to track objects thus avoiding false positives due to double closures. > - Instance-level patching is used to isolate tracking to only the objects created within the test block. Closes #17956
Following the merge of PR #17953 for the socket leak fix, this PR focuses on the remaining orthogonal test and fixture hardening fixes:
try...except NotFound: passduringTestBigQuery.tearDown()to prevent cascaded teardown failures when tag resources were already deleted.autouse=Trueon theuse_local_magics_contextfixture intest_magics.pyto prevent credentials mutation across test runs in uncredentialed CI environments.IPython.core.interactiveshell.InteractiveShelland add fallback unit test coverage formagics.Context.