fix(builder): make model declaration check-and-create atomic - #8005
Open
aqeelat wants to merge 1 commit into
Open
fix(builder): make model declaration check-and-create atomic#8005aqeelat wants to merge 1 commit into
aqeelat wants to merge 1 commit into
Conversation
aqeelat
marked this pull request as draft
July 29, 2026 19:06
aqeelat
force-pushed
the
fix/idempotency-race-condition
branch
from
July 29, 2026 19:11
80a5910 to
aa8c343
Compare
AddModelDeclarationIfDoesntExist checked for an existing class and created a new one in separate non-atomic steps. When two parallel threads both saw 'doesn't exist', both created class stubs — one with properties, one empty. The empty stub could be returned as a type definition, causing missing or different model files between runs (idempotency test failures). Fix: lock on the classLifecycles entry (keyed by namespace + class name) for the entire check-and-create. The lock is reentrant (recursive parent-schema and self-reference calls on the same thread don't deadlock) and per-class-name (different classes don't contend).
aqeelat
force-pushed
the
fix/idempotency-race-condition
branch
from
July 29, 2026 20:57
aa8c343 to
2cc8059
Compare
aqeelat
marked this pull request as ready for review
July 29, 2026 21:15
Contributor
Author
|
@baywet can you please run the tests? |
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.
Problem
Intermittent idempotency test failures with the Twitter API spec: two consecutive generations from the same spec sometimes produced different output. CI logs showed model files missing in one run (
Models/Analytics_metrics.cs) and content differences in others (Models/Analytics.cs,Models/User.cs, etc.).Root cause
AddModelClasschecked for an existing class implicitly viaAddModelDeclarationIfDoesntExist'sGetExistingDeclarationcall, then created and added a class stub viacurrentNamespace.AddClass()— in separate, non-atomic steps. When two parallel threads processing different URL tree nodes both referenced the same component schema, both could pass the check and both create class stubs. The existingclassLifecyclesmechanism prevented duplicate property building but not duplicate class creation.Fix
Added a narrow
lock (lifecycle)insideAddModelClass, covering ONLY theGetExistingDeclarationre-check andAddClasscall — not property building. This makes check-and-add atomic per class name.Why not lock the entire method? Property building (
CreatePropertiesForModelClass) recursively creates dependent models viaAddModelDeclarationIfDoesntExist. If the lock were held during property building, mutually-referencing models (A → B → A) processed by different threads would deadlock: Thread 1 holds A's lock waiting for B's, Thread 2 holds B's waiting for A's.The narrow lock avoids this because:
classLifecyclesmechanismVerification
ModelA.b → ModelB,ModelB.a → ModelA)main(2/10 failures without fix)