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][14.0] auth_saml: Add FOR NO KEY UPDATE NOWAIT clause to SAML provider query #716

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

Ricardoalso
Copy link
Contributor

@Ricardoalso Ricardoalso commented Oct 17, 2024

auth_saml: Add FOR NO KEY UPDATE NOWAIT clause to SAML provider query

  • Enhance concurrency handling when fetching SAML provider data
  • Prevent potential deadlocks by using NOWAIT
  • Ensure data consistency during high-traffic scenarios

Previous attempt using the locking mechanisms

self.env.cr.execute(
    "SELECT id FROM auth_saml_provider WHERE id in %s FOR UPDATE",
    (tuple(provider_ids),),
 )

led to increased latency and deadlocks under high load.

We experienced race conditions where two simultaneous login
attempts triggered parallel updates of the idp_metadata, leading to unreleased locks.

The FOR NO KEY UPDATE NOWAIT clause addresses this issue by:

  1. A weaker form of row-level locking compared to FOR UPDATE. It allows concurrent INSERT operations on child tables that reference the locked row, but prevents UPDATE and DELETE operations on the locked row
  2. Failing fast if the lock cannot be acquired (NOWAIT)
  3. Ensuring that only one transaction can update the idp_metadata at a time

Offical Potsgresql documentation

FOR NO KEY UPDATE
Behaves similarly to FOR UPDATE, except that the lock acquired is weaker: this lock will not block SELECT FOR KEY SHARE commands that attempt to acquire a lock on the same rows. This lock mode is also acquired by any UPDATE that does not acquire a FOR UPDATE lock.

@Ricardoalso Ricardoalso force-pushed the auth_saml_provider_locks branch from fec6c83 to 81014f5 Compare October 17, 2024 12:41
Copy link
Member

@gurneyalex gurneyalex left a comment

Choose a reason for hiding this comment

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

Can you update the commit message:

  • mention the other attempts with the locks, and the issues they caused
  • explain the race condition we checked together this morning with 2 login attempts that will cause 2 updates of the idp_metadata in parallel
  • explain how the fix is preventing this

@Ricardoalso Ricardoalso marked this pull request as draft October 17, 2024 14:53
@Ricardoalso Ricardoalso force-pushed the auth_saml_provider_locks branch 2 times, most recently from 607b03a to f00a171 Compare October 17, 2024 15:32
@Ricardoalso Ricardoalso marked this pull request as ready for review October 17, 2024 15:33
- Enhance concurrency handling when fetching SAML provider data
- Prevent potential deadlocks by using NOWAIT
- Ensure data consistency during high-traffic scenarios

Previous attempt using the locking mechanisms

self.env.cr.execute(
    "SELECT id FROM auth_saml_provider WHERE id in %s FOR UPDATE",
    (tuple(provider_ids),),
 )

led to increased latency and deadlocks under high load.

We experienced race conditions where two simultaneous login
attempts triggered parallel updates of the idp_metadata, leading to unreleased locks.

The FOR NO KEY UPDATE NOWAIT clause addresses this issue by:
1. This lock will not block SELECT FOR KEY SHARE commands
2. Failing fast if the lock cannot be acquired (NOWAIT)
3. Ensuring that only one transaction can update the idp_metadata at a time
@Ricardoalso Ricardoalso force-pushed the auth_saml_provider_locks branch from f00a171 to d99ab0a Compare December 4, 2024 16:48
@Ricardoalso Ricardoalso changed the title [FIX][14.0] auth_saml: Add FOR UPDATE NOWAIT clause to SAML provider query [FIX][14.0] auth_saml: Add FOR NO KEY UPDATE NOWAIT clause to SAML provider query Dec 4, 2024
@gurneyalex
Copy link
Member

/ocabot merge patch

@OCA-git-bot
Copy link
Contributor

On my way to merge this fine PR!
Prepared branch 14.0-ocabot-merge-pr-716-by-gurneyalex-bump-patch, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit e6dcb9e into OCA:14.0 Dec 5, 2024
6 of 7 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at 8435445. Thanks a lot for contributing to OCA. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants