Clear the re-entrancy guard instead of restoring its previous value - #1047
Open
serioushaircut wants to merge 1 commit into
Open
Clear the re-entrancy guard instead of restoring its previous value#1047serioushaircut wants to merge 1 commit into
serioushaircut wants to merge 1 commit into
Conversation
acquire_semian_resource is the only caller of mark_resource_as_acquired and it returns early when the guard is already set, so in supported use the value captured as `previous` can only ever be falsey. Capturing and restoring it is what turns a violation of that contract into a permanent one: if two callers interleave, one captures true, the other restores false, and the one holding true restores it after both have left. From then on every call on that instance returns early from acquire_semian_resource, so the circuit breaker is disabled for the lifetime of the process, silently and with the circuit still reporting closed. Assign false on the way out instead. In supported use this is the value `previous` always held, so behaviour is unchanged; a shared instance now degrades transiently rather than permanently. Two tests, since the guard had none: the interleaving above, and nesting still short-circuiting so a nested call neither re-enters the circuit breaker nor takes a second bulkhead ticket. Refs Shopify#1045 Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Orchestrated-by: ae <noreply@shopify.com> Assisted-By: devx/39c7b308-c333-4ffe-8cd7-f642a14a4b7c
Edouard-chin
approved these changes
Sep 2, 2026
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.
Fixes the one genuine race in #1045. The documentation half is #1046.
mark_resource_as_acquiredcaptures@resource_acquiredand restores it on the way out.acquire_semian_resourceis its only caller and returns early when the guard is already set, so in supported use — an adapter instance standing for one resource, used by one session at a time — the captured value can only ever be falsey!Capturing it is what makes a violation of that contract permanent.
Problem recap - with the instance shared between threads:
previous = false, guard =true.previous = true, guard =true.false.true, and nothing ever clears it. ❗From then on every call through that instance returns early from
acquire_semian_resource::successor:circuit_opennotificationswhile the circuit still reports
closed.The window between 1 and 2 is not theoretical.
ProtectedResource#acquirenotifies:successbefore yielding, so any subscriber that writes to a socket — a StatsD backend, say — releases the GVL inside it. With one such subscriber the strand reproduced in 75 of 75 rounds of 8 threads × 200 calls; with no subscriber, not once in ~10M calls.Assigning
falsein theensureis the valuepreviousalways holds in supported use, so nothing changes there, and a shared instance degrades transiently instead of permanently.Tests. The guard had none. This adds the interleaving above — deterministic, driven
with queues rather than sleeps — and one asserting that nesting still short-circuits, so
a nested call neither re-enters the circuit breaker nor takes a second bulkhead ticket.
Alternative, if you would rather the racy case were handled fully: a depth counter
under a per-instance mutex, incremented and decremented rather than saved and restored,
keeps nesting protection intact even when the instance is shared. It costs one uncontended
mutex per acquire and needs double-checked lazy init for the mutex. Happy to send that
version instead.
Verified locally against 0.28.2 on CRuby 3.4.10: the new test fails before the change and
passes after, and the reproduction from #1045 goes from a permanently dead breaker to one
that keeps opening.
Co-authored-by: Claude Opus 5 noreply@anthropic.com
Orchestrated-by: ae noreply@shopify.com