Repository navigation
Allow callers to choose the row lock mode in refresh_token_if_expired! - #2094
Open
andersonkrs wants to merge 1 commit into
Open
andersonkrs wants to merge 1 commit into
andersonkrs wants to merge 1 commit into
Conversation
The token refresh holds a FOR UPDATE lock on the shop row across the HTTP call to Shopify. FOR UPDATE conflicts with the FOR KEY SHARE lock taken by inserts into tables with a foreign key to the shop, so when Shopify's token endpoint is slow every webhook write for that shop queues behind the refresh. Forward a lock argument to with_lock so an app can pass a weaker mode such as "FOR NO KEY UPDATE", which still serialises concurrent refreshes but lets FK-referencing inserts proceed.
andersonkrs
force-pushed
the
patch/refresh-token-lock-mode
branch
from
October 29, 2026 14:18
4274384 to
ac3f522
Compare
andersonkrs
marked this pull request as ready for review
October 29, 2026 14:21
andersonkrs
commented
Oct 29, 2026
| def refresh_token_if_expired!(lock: "FOR NO KEY UPDATE") | ||
| super | ||
| end | ||
| end |
Author
There was a problem hiding this comment.
Happy to discuss if yall think this should have it's proper DSL config like
class Shop < ActiveRecord::Base
include ShopifyApp::ShopSessionStorage
self.refresh_token_lock = "FOR NO KEY UPDATE"
end
Author
|
I have signed the CLA |
This was referenced Nov 21, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
ShopSessionStorage#refresh_token_if_expired!currently hardcodeswith_lock do ... end, which on PostgreSQL issuesSELECT ... FOR UPDATEon the shop row and holds that lock for the whole duration of the HTTP call to Shopify's token endpoint.FOR UPDATEis the strongest row lock. It conflicts withFOR KEY SHARE, which is the lock PostgreSQL takes implicitly on the referenced row for everyINSERT/UPDATEinto a table with a foreign key pointing atshops. In practice that means: while one process is refreshing the token, every other write for that shop that touches a child table (orders, webhooks, line items, anything withshop_id) blocks until the refresh finishes.Under normal conditions the refresh takes a few hundred milliseconds and nobody notices. When Shopify's token endpoint is slow or returning 5xx, the lock holder sits on the row for the full HTTP read timeout, and for a high-volume shop every concurrent webhook job for that shop piles up behind it. We hit this in production: a degraded token endpoint turned into statement/lock timeouts across the webhook pipeline for the affected shops, and the SLA on webhook processing was gone for the duration.
The refresh only needs to serialize other refreshes (and other direct writes to the shop row). It does not need to block inserts into tables that merely reference the shop.
FOR NO KEY UPDATEgives exactly that: it still conflicts with otherFOR NO KEY UPDATE/FOR UPDATElocks, so the double-checkedshould_refresh?inside the lock keeps working, but it does not conflict withFOR KEY SHARE, so foreign-key inserts proceed.This PR adds a
lock:keyword torefresh_token_if_expired!and forwards it to ActiveRecord'swith_lock(lock). The default staystrue(i.e.FOR UPDATE), so existing behavior is unchanged. Apps that want the weaker lock override the method:I went with an opt-in rather than changing the default because the lock clause is database-specific (
FOR NO KEY UPDATEis PostgreSQL-only; MySQL would wantFOR UPDATEorFOR SHARE), and the gem doesn't know which adapter the host app runs on. Happy to discuss defaulting toFOR NO KEY UPDATEwhen the adapter is PostgreSQL if maintainers prefer that.Also adds a short section to
docs/shopify_app/sessions.mddescribing the trade-off and the override, and a CHANGELOG entry.Reviewer's guide to testing
bundle exec ruby -Itest test/shopify_app/session/shop_session_storage_test.rb— two new tests cover the forwarded lock mode and the unchanged default.refresh_token_if_expired!as shown above, expire a shop's token, and callshop.refresh_token_if_expired!withActiveSupport::Notificationssubscribed tosql.active_record. The lock statement should readSELECT "shops".* FROM "shops" WHERE "shops"."id" = ... LIMIT 1 FOR NO KEY UPDATE.psqlsession runBEGIN; SELECT * FROM shops WHERE id = X FOR UPDATE;and leave it open. In another session tryINSERTinto any table with a foreign key toshopsfor thatshop_id. The insert blocks. Repeat withFOR NO KEY UPDATEin the first session and the insert goes through.Things to focus on
lib/shopify_app/session/shop_session_storage.rb— the only runtime change iswith_lock dobecomingwith_lock(lock) do, withlock: trueas the default sowith_lock(true)is byte-for-byte what ActiveRecord did before.test/support/session_store_strategy_test_helpers.rb—MockShopInstance#with_locknow mirrors ActiveRecord'swith_lock(lock = true)signature and records the mode so the tests can assert on it.ShopifyApp.configuration, versus an adapter-aware default.Checklist
Before submitting the PR, please consider if any of the following are needed:
CHANGELOG.mdif the changes would impact usersREADME.md, if appropriate./docs, if necessary