Repository navigation
Serialize a Profile's bindings from a locked snapshot, so another thread's edits can't break it [patch] - #232
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
…ead's edits can't break it [patch] System.Text.Json wrote a profile's bindings by enumerating the live Chords dictionary with no lock, so serializing while another thread changed bindings threw "Collection was modified". The obsolete Chords property is now [JsonIgnore], and a private ChordsSnapshot property copies the bindings under the lock and is written as "Chords". The JSON shape is unchanged. Fixes #229 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW499sBmEvZWVdPTbTv
|
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 #229
What was wrong
Profiletakes a lock on every read and write of its bindings, except one. System.Text.Json wrote them through the public[Obsolete]Chordsproperty, which returns the live dictionary, and it enumerated that dictionary with no lock held. If another thread changed bindings duringJsonSerializer.Serialize(profile), the call threwInvalidOperationException: Collection was modified.Change
Chordsis now[JsonIgnore].[JsonInclude, JsonPropertyName("Chords")]ChordsSnapshotproperty copies the bindings under the lock. That copy is what gets written.[JsonConstructor]parameter is renamed fromchordstochordsSnapshot. Its type staysDictionary<string, Chord?>?, so reading keeps the existingJsonExceptionchecks.Id,Name,Description,Chords,ChordCountandBoundCommandsare still written.ChordCountandBoundCommandseach still take their own lock. Within one document they can still differ fromChordsby a concurrent edit. They are never read back, and the issue lists them as optional, so I left them out of this change.Tests
New
ProfileJsonConcurrencyTests:Serialize_WhileAnotherThreadChangesBindings_WritesASnapshotruns the issue's reproduction. A profile with 200 bindings is serialized 2000 times while another thread loopsSetChord/RemoveChord. Without the fix it fails on the first pass with "Collection was modified".Serialize_KeepsTheJsonShapepins the property names and how a binding is written. It passes both before and after the change.The existing
Profileround-trip and invalid-JSON tests still pass. Full suite on Linux / .NET 10: 383/383 passed.Generated by Claude Code