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 get_one_or_create IntegrityError handler (PP-1838) #2134

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

jonathangreen
Copy link
Member

Description

Fix an issue with the Exception handler for an IntegrityError in the get_one_or_create function.

If one of the parameters for this function is a sqlalchemy class, then the logging function can cause a PendingRollbackError, since the rollback is happening after the log message.

Update the function to use db.begin_nested() as a context manager, and write a test for this case.

I added type hints to a couple related functions while I was debugging this before i narrowed in on exactly the cause. These hints are included in this PR. They aren't really related to this fix, but they seem harmless to come in with this one. I can break them out to separate PR if desired.

Motivation and Context

We are seeing a number of PendingRollbackError happening via the patron_activity.sync_patron_activity celery task. 282 in the last week. Trying to clean up these errors that are causing tasks to fail, so we can add alerts for failed celery tasks.

How Has This Been Tested?

  • Added test for this case
  • Running tests in CI

Checklist

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

@jonathangreen jonathangreen added the bug Something isn't working label Oct 23, 2024
@jonathangreen jonathangreen requested a review from a team October 23, 2024 16:55
@@ -913,7 +913,7 @@ def get_patron_credential(
:param is_fulfillment: Boolean indicating whether we need a fulfillment credential.
"""

def refresh(credential):
def refresh(credential: Credential) -> Credential:
Copy link
Member Author

Choose a reason for hiding this comment

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

Unrelated type hint changes.

allow_persistent_token: bool = False,
allow_empty_token: bool = False,
collection: Collection | None = None,
force_refresh: bool = False,
Copy link
Member Author

Choose a reason for hiding this comment

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

Unrelated type hint changes.

if key in kwargs:
del kwargs[key]
obj = create(db, model, create_method, create_method_kwargs, **kwargs)
return obj
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the actual fix

Copy link

codecov bot commented Oct 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.83%. Comparing base (371492c) to head (0431138).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2134   +/-   ##
=======================================
  Coverage   90.82%   90.83%           
=======================================
  Files         352      352           
  Lines       40854    40854           
  Branches     8852     8853    +1     
=======================================
+ Hits        37104    37108    +4     
+ Misses       2442     2438    -4     
  Partials     1308     1308           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@tdilauro tdilauro left a comment

Choose a reason for hiding this comment

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

🚀

@jonathangreen jonathangreen merged commit a1d5c61 into main Oct 23, 2024
32 checks passed
@jonathangreen jonathangreen deleted the bugfix/get-one-or-create-integrity-error branch October 23, 2024 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants