Skip to content

FIX: close BigQuery Storage client transport channel after use #295

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

Merged
merged 4 commits into from
Nov 25, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
FIX: close BigQuery Storage client transport channel after use
This fixes a file descriptor leak.
  • Loading branch information
tswast committed Nov 23, 2019
commit 34e38bc1cb4d22c5a0cf83074529fa03832570ab
34 changes: 18 additions & 16 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,7 @@ def __init__(
context.project = self.project_id

self.client = self.get_client()
self.bqstorage_client = _make_bqstorage_client(
use_bqstorage_api, self.credentials
)
self.use_bqstorage_api = use_bqstorage_api

# BQ Queries costs $5 per TB. First 1 TB per month is free
# see here for more: https://cloud.google.com/bigquery/pricing
Expand Down Expand Up @@ -541,29 +539,33 @@ def _download_results(
if max_results == 0:
return None

if max_results is None:
# Only use the BigQuery Storage API if the full result set is requested.
bqstorage_client = self.bqstorage_client
else:
try:
bqstorage_client = None
if max_results is None:
# Only use the BigQuery Storage API if the full result set is requested.
bqstorage_client = _make_bqstorage_client(self.use_bqstorage_api, self.credentials)

try:
query_job.result()
# Get the table schema, so that we can list rows.
destination = self.client.get_table(query_job.destination)
rows_iter = self.client.list_rows(
destination, max_results=max_results
)

schema_fields = [field.to_api_repr() for field in rows_iter.schema]
nullsafe_dtypes = _bqschema_to_nullsafe_dtypes(schema_fields)
df = rows_iter.to_dataframe(
dtypes=nullsafe_dtypes,
bqstorage_client=bqstorage_client,
progress_bar_type=progress_bar_type,
)
except self.http_error as ex:
self.process_http_error(ex)

schema_fields = [field.to_api_repr() for field in rows_iter.schema]
nullsafe_dtypes = _bqschema_to_nullsafe_dtypes(schema_fields)
df = rows_iter.to_dataframe(
dtypes=nullsafe_dtypes,
bqstorage_client=bqstorage_client,
progress_bar_type=progress_bar_type,
)
finally:
if bqstorage_client:
# Clean up open socket resources. See:
# https://github.com/pydata/pandas-gbq/issues/294
bqstorage_client.transport.channel.close()

if df.empty:
df = _cast_empty_df_dtypes(schema_fields, df)
Expand Down