Fix thread-unsafe Generator publication race in HttpRouter#compile - #1
Open
hazel-tan wants to merge 1 commit into
Open
Fix thread-unsafe Generator publication race in HttpRouter#compile#1hazel-tan wants to merge 1 commit into
hazel-tan wants to merge 1 commit into
Conversation
Generator#initialize set @route.generator = self before finishing computation of @path_generators. Since HttpRouter#compile's guard (return if @compiled) is not synchronized, concurrent threads racing into compile() for the first time can read route.generator.max_param_count while another thread's Generator is still mid-construction, hitting `undefined method 'map' for nil` on the not-yet-assigned @path_generators. Move the publish to the end of initialize so no thread can ever observe a partially-built Generator. Reproduced the original failure and confirmed the fix by widening the race window with an injected sleep (0/100 hits after the fix vs. reliable reproduction before it).
Author
Rollout plan for TC
|
Author
|
Hey @ngsikai, requested your review as you're on-call and also since you might have more context on this gem! Let me know if you want a separate sync up on this 🙏 |
Collaborator
|
Hey Hazel will set up a session with you on this for Monday |
ngsikai
approved these changes
Aug 31, 2026
ngsikai
left a comment
Collaborator
There was a problem hiding this comment.
Synced up offline with @hazel-tan, will pull this custom branch on TC staging for fermentation and eventually with TC production.
Once it is stable, we will roll out on master and inform other teams
Author
|
Fermenting in TC staging here: https://github.com/Kaligo/transfer_connect/pull/5230 |
Author
|
Fix has been deployed to TC prod - will let it ferment to verify that there is no regression |
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.
Summary
Surfaced in https://appsignal.com/kaligo/sites/5a7a817101925b30127a73d7/exceptions/incidents/612/traces/60ccb4b3070014d393591b73078771ad
Stacktrace:
HttpRouter::Generator#initializepublishes itself to the sharedRouteobject (@route.generator = self) before it finishes computing@path_generators.Combined with
HttpRouter#compile's unsynchronizedreturn if @compiledguard, concurrent threads racing intocompilefor the first time can readroute.generator.max_param_counton aGeneratorthat's still mid-construction — hittingundefined method 'map' for nilon the not-yet-assigned@path_generators.This surfaces in multi-threaded Rack servers (e.g. Puma with multiple threads) as an intermittent
NoMethodError: undefined method 'map' for nil, typically right after a fresh worker boots and the first burst of concurrent requests all hit the router before it's compiled.Some examples encountered:
Fix
Move
@route.generator = selfto the end ofinitialize, after@path_generatorsis fully computed, so no other thread can ever observe a partially-builtGenerator.Verification
Reproduced the original failure by widening the race window with an injected
sleepat the point our theory says the unsafe ordering exists, then ran the same test against the fix.Pre-fix (with the sleep inserted at the old, buggy position — publish before
@path_generatorsis computed):Post-fix (sleep inserted at the new position, same test, 100 iterations):