Skip to content

Commit 19a57d7

Browse files
partheagcf-owl-bot[bot]steffnay
authored
fix: handle AttributeError in bigquery_storage writer (#414)
* fix: resolve AttributeError in bigquery_storage writer * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * handle AttributeError to avoid race condition * actually catch AttributeError Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Steffany Brown <30247553+steffnay@users.noreply.github.com>
1 parent a7a447f commit 19a57d7

File tree

2 files changed

+54
-26
lines changed
  • packages/google-cloud-bigquery-storage/google/cloud

2 files changed

+54
-26
lines changed

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/writer.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,35 @@ def _open(
182182
# ValueError: Can not send() on an RPC that has never been open()ed.
183183
#
184184
# when they try to send a request.
185-
while not self._rpc.is_active and self._consumer.is_active:
186-
# Avoid 100% CPU while waiting for RPC to be ready.
187-
time.sleep(_WRITE_OPEN_INTERVAL)
188-
189-
# TODO: Check retry.deadline instead of (per-request) timeout.
190-
# Blocked by
191-
# https://github.com/googleapis/python-api-core/issues/262
192-
if timeout is None:
193-
continue
194-
current_time = time.monotonic()
195-
if current_time - start_time > timeout:
196-
break
185+
try:
186+
while not self._rpc.is_active and self._consumer.is_active:
187+
# Avoid 100% CPU while waiting for RPC to be ready.
188+
time.sleep(_WRITE_OPEN_INTERVAL)
189+
190+
# TODO: Check retry.deadline instead of (per-request) timeout.
191+
# Blocked by
192+
# https://github.com/googleapis/python-api-core/issues/262
193+
if timeout is None:
194+
continue
195+
current_time = time.monotonic()
196+
if current_time - start_time > timeout:
197+
break
198+
except AttributeError:
199+
# Handle the AttributeError which can occur if the stream is
200+
# unable to be opened. In that case, self._rpc or self._consumer
201+
# may be None.
202+
pass
203+
204+
try:
205+
is_consumer_active = self._consumer.is_active
206+
except AttributeError:
207+
# Handle the AttributeError which can occur if the stream is
208+
# unable to be opened. In that case, self._consumer
209+
# may be None.
210+
is_consumer_active = False
197211

198212
# Something went wrong when opening the RPC.
199-
if not self._consumer.is_active:
213+
if not is_consumer_active:
200214
# TODO: Share the exception from _rpc.open(). Blocked by
201215
# https://github.com/googleapis/python-api-core/issues/268
202216
request_exception = exceptions.Unknown(

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1beta2/writer.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,35 @@ def _open(
182182
# ValueError: Can not send() on an RPC that has never been open()ed.
183183
#
184184
# when they try to send a request.
185-
while not self._rpc.is_active and self._consumer.is_active:
186-
# Avoid 100% CPU while waiting for RPC to be ready.
187-
time.sleep(_WRITE_OPEN_INTERVAL)
188-
189-
# TODO: Check retry.deadline instead of (per-request) timeout.
190-
# Blocked by
191-
# https://github.com/googleapis/python-api-core/issues/262
192-
if timeout is None:
193-
continue
194-
current_time = time.monotonic()
195-
if current_time - start_time > timeout:
196-
break
185+
try:
186+
while not self._rpc.is_active and self._consumer.is_active:
187+
# Avoid 100% CPU while waiting for RPC to be ready.
188+
time.sleep(_WRITE_OPEN_INTERVAL)
189+
190+
# TODO: Check retry.deadline instead of (per-request) timeout.
191+
# Blocked by
192+
# https://github.com/googleapis/python-api-core/issues/262
193+
if timeout is None:
194+
continue
195+
current_time = time.monotonic()
196+
if current_time - start_time > timeout:
197+
break
198+
except AttributeError:
199+
# Handle the AttributeError which can occur if the stream is
200+
# unable to be opened. In that case, self._rpc or self._consumer
201+
# may be None.
202+
pass
203+
204+
try:
205+
is_consumer_active = self._consumer.is_active
206+
except AttributeError:
207+
# Handle the AttributeError which can occur if the stream is
208+
# unable to be opened. In that case, self._consumer
209+
# may be None.
210+
is_consumer_active = False
197211

198212
# Something went wrong when opening the RPC.
199-
if not self._consumer.is_active:
213+
if not is_consumer_active:
200214
# TODO: Share the exception from _rpc.open(). Blocked by
201215
# https://github.com/googleapis/python-api-core/issues/268
202216
request_exception = exceptions.Unknown(

0 commit comments

Comments
 (0)