Repository navigation
fix: make document model builds safe for concurrent use - #721
Open
daveshanley wants to merge 2 commits into
Open
daveshanley wants to merge 2 commits into
daveshanley wants to merge 2 commits into
Conversation
BuildV3Model and BuildV2Model checked and set the cached model with no synchronization. Concurrent calls on one document raced, and each caller could build and receive its own model. The document's mutable state (spec info, configuration, rolodex and both models) now lives in atomic pointers, so getters and cached builds read without locking. A mutex serializes the builds themselves: concurrent callers wait for the first build, then share its model. Release takes the same mutex. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #721 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 315 315
Lines 39919 39938 +19
=========================================
+ Hits 39919 39938 +19
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Release clears the SpecInfo in place, and the SpecIndex shares it, so a reader still holding it races with Release. Detaching it instead would keep the YAML tree and spec bytes alive through the index. Release stays safe against concurrent builds; every other use must finish first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
BuildV3ModelandBuildV2Modelchecked and set the cached model with no synchronization. Concurrent calls on the same document raced, and each caller could build and receive its own model, while the document kept whichever was stored last.atomic.Pointerfields. Getters and cached builds are a single atomic load, with no locking.Releasetakes the same mutex, so it waits for a build in progress. It is still end-of-life: it clears theSpecInfoin place (theSpecIndexshares it), so it must come after every other use of the document and of anySpecInfotaken from it. The interface doc now says so. DetachingSpecInfowithout clearing it was rejected because the index would keep the YAML tree and spec bytes alive.RenderandSerializeread through the atomic fields.Callers such as libopenapi-validator no longer need their own locking around model builds.
Testing
document_concurrency_test.goruns concurrent builds alongside the getters,Render,SerializeandSetConfiguration, and concurrent builds alongsideRelease. Againstmainit reports 40 data races and 30 failures where concurrent callers received different models. With this change it passes.go test -race ./...passes, anddocument.gohas 100% statement coverage.Benchmarks
8 interleaved runs against
main,benchstat:mainBuildV3ModelGetSpecInfo,GetRolodexBuildV3Model, 16 goroutinesBuildV3ModelThe +0.11ns on the cached build is one atomic acquire load in place of a plain load, the same cost
sync.Oncepays. No benchmark allocates more. A plain mutex was measured first and rejected: it doubled getter cost and pushed the 16-goroutine case to 89ns per call.🤖 Generated with Claude Code