Skip to content

fix(bigquery): harden system test teardown and unit test auth isolation - #17956

Closed
shuoweil wants to merge 3 commits into
mainfrom
shuowei-gbq-fix-socket-and-ci
Closed

fix(bigquery): harden system test teardown and unit test auth isolation#17956
shuoweil wants to merge 3 commits into
mainfrom
shuowei-gbq-fix-socket-and-ci

Conversation

@shuoweil

@shuoweil shuoweil commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Following the merge of PR #17953 for the socket leak fix, this PR focuses on the remaining orthogonal test and fixture hardening fixes:

  1. System Test Teardown Resilience: Wrap TagKey and TagValue resource deletions in try...except NotFound: pass during TestBigQuery.tearDown() to prevent cascaded teardown failures when tag resources were already deleted.
  2. Unit Test ADC Isolation: Enable autouse=True on the use_local_magics_context fixture in test_magics.py to prevent credentials mutation across test runs in uncredentialed CI environments.
  3. InteractiveShell Fixture & Fallback: Use IPython.core.interactiveshell.InteractiveShell and add fallback unit test coverage for magics.Context.

@shuoweil shuoweil self-assigned this Jul 30, 2026
@shuoweil
shuoweil requested review from a team as code owners July 30, 2026 21:03
@shuoweil
shuoweil requested review from GarrettWu, sycai and tswast and removed request for a team July 30, 2026 21:03

@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 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.

shuoweil and others added 2 commits July 30, 2026 14:06
…nection.py

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@parthea

parthea commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Googlers see b/540939659

@shuoweil

Copy link
Copy Markdown
Contributor Author

Googlers see b/540939659

Thanks @parthea. I will trim down this PR and update the bug.

shuoweil pushed a commit that referenced this pull request Jul 31, 2026
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
@shuoweil shuoweil changed the title fix: harden test fixtures and prevent socket leaks fix(bigquery): harden system test teardown and unit test auth isolation Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants