Skip to content

Allow callers to choose the row lock mode in refresh_token_if_expired! - #2094

Open
andersonkrs wants to merge 1 commit into
Shopify:mainfrom
andersonkrs:patch/refresh-token-lock-mode
Open

andersonkrs wants to merge 1 commit into
Shopify:mainfrom
andersonkrs:patch/refresh-token-lock-mode

Conversation

@andersonkrs

Copy link
Copy Markdown

What this PR does

ShopSessionStorage#refresh_token_if_expired! currently hardcodes with_lock do ... end, which on PostgreSQL issues SELECT ... FOR UPDATE on the shop row and holds that lock for the whole duration of the HTTP call to Shopify's token endpoint.

FOR UPDATE is the strongest row lock. It conflicts with FOR KEY SHARE, which is the lock PostgreSQL takes implicitly on the referenced row for every INSERT/UPDATE into a table with a foreign key pointing at shops. 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 with shop_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 UPDATE gives exactly that: it still conflicts with other FOR NO KEY UPDATE / FOR UPDATE locks, so the double-checked should_refresh? inside the lock keeps working, but it does not conflict with FOR KEY SHARE, so foreign-key inserts proceed.

This PR adds a lock: keyword to refresh_token_if_expired! and forwards it to ActiveRecord's with_lock(lock). The default stays true (i.e. FOR UPDATE), so existing behavior is unchanged. Apps that want the weaker lock override the method:

class Shop < ActiveRecord::Base
  include ShopifyApp::ShopSessionStorage

  def refresh_token_if_expired!(lock: "FOR NO KEY UPDATE")
    super
  end
end

I went with an opt-in rather than changing the default because the lock clause is database-specific (FOR NO KEY UPDATE is PostgreSQL-only; MySQL would want FOR UPDATE or FOR SHARE), and the gem doesn't know which adapter the host app runs on. Happy to discuss defaulting to FOR NO KEY UPDATE when the adapter is PostgreSQL if maintainers prefer that.

Also adds a short section to docs/shopify_app/sessions.md describing the trade-off and the override, and a CHANGELOG entry.

Reviewer's guide to testing

  1. 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.
  2. In a PostgreSQL-backed app on this branch, override refresh_token_if_expired! as shown above, expire a shop's token, and call shop.refresh_token_if_expired! with ActiveSupport::Notifications subscribed to sql.active_record. The lock statement should read SELECT "shops".* FROM "shops" WHERE "shops"."id" = ... LIMIT 1 FOR NO KEY UPDATE.
  3. To reproduce the original contention: in one psql session run BEGIN; SELECT * FROM shops WHERE id = X FOR UPDATE; and leave it open. In another session try INSERT into any table with a foreign key to shops for that shop_id. The insert blocks. Repeat with FOR NO KEY UPDATE in the first session and the insert goes through.

Things to focus on

  1. lib/shopify_app/session/shop_session_storage.rb — the only runtime change is with_lock do becoming with_lock(lock) do, with lock: true as the default so with_lock(true) is byte-for-byte what ActiveRecord did before.
  2. test/support/session_store_strategy_test_helpers.rb — MockShopInstance#with_lock now mirrors ActiveRecord's with_lock(lock = true) signature and records the mode so the tests can assert on it.
  3. Whether the opt-in override is the right API, versus a configuration option on ShopifyApp.configuration, versus an adapter-aware default.

Checklist

Before submitting the PR, please consider if any of the following are needed:

  • Update CHANGELOG.md if the changes would impact users
  • Update README.md, if appropriate.
  • Update any relevant pages in /docs, if necessary
  • For security fixes, the Disclosure Policy must be followed.

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
andersonkrs force-pushed the patch/refresh-token-lock-mode branch from 4274384 to ac3f522 Compare October 29, 2026 14:18
@github-actions github-actions Bot added the devtools-gardener Post the issue or PR to Slack for the gardener label Oct 29, 2026
@andersonkrs
andersonkrs marked this pull request as ready for review October 29, 2026 14:21
def refresh_token_if_expired!(lock: "FOR NO KEY UPDATE")
super
end
end

@andersonkrs andersonkrs Oct 29, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

@andersonkrs

Copy link
Copy Markdown
Author

I have signed the CLA

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

devtools-gardener Post the issue or PR to Slack for the gardener

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant