-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(bigquery): close GAPIC storage transport and auth sessions to prevent socket leaks #17508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,6 @@ | |
|
|
||
| from . import helpers | ||
|
|
||
|
|
||
| JOB_TIMEOUT = 120 # 2 minutes | ||
| DATA_PATH = pathlib.Path(__file__).parent.parent / "data" | ||
|
|
||
|
|
@@ -234,23 +233,29 @@ def _create_bucket(self, bucket_name, location=None): | |
|
|
||
| def test_close_releases_open_sockets(self): | ||
| current_process = psutil.Process() | ||
| conn_count_start = len(current_process.net_connections()) | ||
| conn_start = current_process.net_connections() | ||
| conn_count_start = len(conn_start) | ||
|
|
||
| with helpers.patch_tracked_requests(): | ||
| client = Config.CLIENT | ||
| client.query( | ||
| """ | ||
| SELECT | ||
| source_year AS year, COUNT(is_male) AS birth_count | ||
| FROM `bigquery-public-data.samples.natality` | ||
| GROUP BY year | ||
| ORDER BY year DESC | ||
| LIMIT 15 | ||
| """ | ||
| ) | ||
|
|
||
| client = Config.CLIENT | ||
| client.query( | ||
| """ | ||
| SELECT | ||
| source_year AS year, COUNT(is_male) AS birth_count | ||
| FROM `bigquery-public-data.samples.natality` | ||
| GROUP BY year | ||
| ORDER BY year DESC | ||
| LIMIT 15 | ||
| """ | ||
| ) | ||
| client.close() | ||
|
|
||
| client.close() | ||
| import gc | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've recently experienced pytest still hanging onto resources even after explicit |
||
|
|
||
| conn_count_end = len(current_process.net_connections()) | ||
| gc.collect() | ||
| conn_end = current_process.net_connections() | ||
| conn_count_end = len(conn_end) | ||
| self.assertLessEqual(conn_count_end, conn_count_start) | ||
|
|
||
| def test_create_dataset(self): | ||
|
|
@@ -2174,25 +2179,31 @@ def test_dbapi_dry_run_query(self): | |
| def test_dbapi_connection_does_not_leak_sockets(self): | ||
| pytest.importorskip("google.cloud.bigquery_storage") | ||
| current_process = psutil.Process() | ||
| conn_count_start = len(current_process.net_connections()) | ||
|
|
||
| # Provide no explicit clients, so that the connection will create and own them. | ||
| connection = dbapi.connect() | ||
| cursor = connection.cursor() | ||
|
|
||
| cursor.execute( | ||
| conn_start = current_process.net_connections() | ||
| conn_count_start = len(conn_start) | ||
|
|
||
| with helpers.patch_tracked_requests(): | ||
| # Provide no explicit clients, so that the connection will create and own them. | ||
| connection = dbapi.connect() | ||
| cursor = connection.cursor() | ||
|
|
||
| cursor.execute( | ||
| """ | ||
| SELECT id, `by`, timestamp | ||
| FROM `bigquery-public-data.hacker_news.full` | ||
| ORDER BY `id` ASC | ||
| LIMIT 100000 | ||
| """ | ||
| SELECT id, `by`, timestamp | ||
| FROM `bigquery-public-data.hacker_news.full` | ||
| ORDER BY `id` ASC | ||
| LIMIT 100000 | ||
| """ | ||
| ) | ||
| rows = cursor.fetchall() | ||
| self.assertEqual(len(rows), 100000) | ||
| ) | ||
| rows = cursor.fetchall() | ||
| self.assertEqual(len(rows), 100000) | ||
|
|
||
| connection.close() | ||
| import gc | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for this function re: splitting the gc logic tests into a separate module. |
||
|
|
||
| connection.close() | ||
| conn_count_end = len(current_process.net_connections()) | ||
| gc.collect() | ||
| conn_end = current_process.net_connections() | ||
| conn_count_end = len(conn_end) | ||
| self.assertLessEqual(conn_count_end, conn_count_start) | ||
|
|
||
| def _load_table_for_dml(self, rows, dataset_id, table_id): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer we add a comment explaining why this is safe. In this case, it's to protect from the user somehow installing this despite us dropping support in #17187