Skip to content
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

Fix: report non-503 http status errors when creating engagements #1479

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Changes from all commits
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
3 changes: 3 additions & 0 deletions pyquil/api/_engagement_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def get_engagement(
:return: Fetched or cached engagement.
:raises QPUUnavailableError: raised when the QPU is unavailable due, and provides a suggested
number of seconds to wait until retrying.
:raises QCSHTTPStatusError: raised when creating an engagement fails for a reason that is not
due to QPU unavailability.
"""
key = EngagementCacheKey(quantum_processor_id, endpoint_id)

Expand All @@ -102,6 +104,7 @@ def get_engagement(
except QCSHTTPStatusError as e:
if response.status_code == 503:
raise QPUUnavailableError(retry_after=response.headers.get("Retry-After")) from e
raise e
return self._cached_engagements[key]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that safe dictionary access via .get is already ensured for the lock session here:

if not self._engagement_valid(self._cached_engagements.get(key)):

The addition of raise e will ensure that any error in the try block will still throw, so the get_engagement function only continues if the cache is populated.

Copy link
Contributor Author

@jselig-rigetti jselig-rigetti Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, if we want to make sure that this access is guarded, we could do something similar to the following:

Suggested change
return self._cached_engagements[key]
engagement = self._cached_engagements.get(key)
if not self._engagement_valid(engagement):
self._cached_engagements.pop(key, None)
# TODO: create more detailed error in this case
raise Exception(f"expected valid engagement in cache, but got: {engagement}")
return engagement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of raise e will ensure that any error in the try block will still throw

Ah, yeah, this is fine without the extra check - with your addition in this PR, that path is safe again.

Note, for code ⛳ purposes, raise is equivalent to raise e.


@staticmethod
Expand Down