Skip to content

fix(project/dirty): unify canonical entry locking and lifecycle in SyncMap - #9

Draft
robertpenner wants to merge 2 commits into
mainfrom
fix/dirty-syncmap-lifecycle
Draft

robertpenner wants to merge 2 commits into
mainfrom
fix/dirty-syncmap-lifecycle

Conversation

@robertpenner

Copy link
Copy Markdown
Owner

Closes #5

Summary

Fixes 5 verified concurrency and state-machine defects in dirty.SyncMap (tsc/internal/project/dirty/syncmap.go) by unifying canonical-entry promotion and locking under a single invariant:

  1. Lost delete in SyncMapEntry.Delete(): On a raced clean handle that lost m.dirty.LoadOrStore, line 151 previously set e.delete = true instead of entry.delete = true and omitted e.proxyFor = entry, silently dropping deletions.
  2. Nil-pointer panic on SyncMap.Delete(key) + concurrent SyncMapEntry.Change(): m.Delete inserted a tombstone with value: zero (nil), dirty: false, delete: true, causing concurrent clean handles to panic in entry.value.Clone() and causing FinalizeWith's OnDelete hook to receive nil.
  3. TOCTOU in ChangeIf / DeleteIf: Predicates were evaluated against stale clean handles before proxying to the winning dirty entry without re-checking under entry.mu.
  4. Mutating deleted entries: changeLocked cloned and ran apply even when entry.delete was already true.
  5. Data race and lost delete inside SyncMapEntry.Locked(): After a first v.Change() proxied a clean handle and unlocked entry.mu, subsequent v.Change() or v.Delete() calls in the same Locked(fn) skipped e.proxyFor, causing a -race data race and lost deletion.

Fix

  • Introduce lockCanonicalLocked() so every mutating path resolves the canonical entry in m.dirty, binds e.proxyFor, and locks canonical.mu in strict loser.mu -> canonical.mu order (depth-1 DAG, deadlock-free).
  • Refuse mutation on deleted entries (if c.delete { return }) and re-check ChangeIf/DeleteIf predicates under c.mu after proxying (while preserving zero-allocation clean fast paths when untouched and cond is false).
  • Store value: baseVal alongside original: baseVal in SyncMap.Delete(key) so FinalizeWith OnDelete receives the base value.
  • Have lockedEntry retain the canonical lock for the remainder of SyncMapEntry.Locked(fn) once promoted or bound, keeping multi-operation Locked callbacks mutually excluded on canonical.mu.

Verification

  • TLA+ model (DirtySyncMapFixed.tla / .cfg on tla-concurrency-verification): 0 errors across 4,164 distinct states (2 workers) and 261,232 distinct states (3 workers) with deadlock checking enabled, verifying NoNilClonePanic, NoDataRace, NoMutateBase, NoMutateDeleted, NoChangeIfTOCTOU, and NoLostDelete.
  • All 6 original reproducer tests (dirty_syncmap_repro_test.go) now pass under -race.
  • go -C ./tsc test -race -count=1 ./internal/project/... ./internal/ls/autoimport/...: all 6 packages pass (dirty, project, ata, background, logging, autoimport), including TestSyncMapEntryLifecycleSafety.
  • gofmt and go vet ./internal/project/dirty/...: clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redesign dirty.SyncMap entry lifecycle to eliminate data race, nil panic, lost deletes, and TOCTOU (tsc/internal/project/dirty/syncmap.go)

1 participant