Repository navigation
In progress
Labels
Description
Parent / source
- Parent: End-to-end red-to-green TLA+ fixes and upstream PR prep for flagship TypeScript 7 concurrency bugs #1
- Authoritative analysis:
verification/tla/ANALYSIS.md(Tier A Fix api.Session snapshot use-after-deref and post-close leak (tsc/internal/api/session.go) #3, Pattern 2) andverification/tla/FINDINGS.md(fix(project/dirty): unify canonical entry locking and lifecycle in SyncMap #9) on branchtla-concurrency-verification(3421f17). - Existing failing spec and reproducer:
verification/tla/specs/ApiSessionSnapshot/ApiSessionSnapshot.tla(NoUseAfterDerefin 4 states;NoLeakAfterClosein 6 states)verification/tla/reproducers/api_session_repro_test.go(both fail deterministically in 0.00s)
What to build
Make api.Session's snapshot registry safe under concurrent ipc.AsyncConn request handlers, handleRelease, createSnapshot, and Session.Close():
- Prevent use-after-deref in read/query handlers: Any handler obtaining
*snapshotDatafor a snapshot handle must keep the underlying*project.Snapshotalive until the handler finishes using it, even if a concurrenthandleRelease(handle)orSession.Close()removes the handle froms.snapshots. Once all in-flight handlers and client references have released the snapshot,sd.snapshot.Deref()must still run exactly once so no snapshot leaks. - Prevent post-close snapshot leaks: If
Session.Close()has already run (or runs concurrently with an in-flight snapshot creation),registerSnapshotmust not leave a retained*project.Snapshotorphaned ins.snapshots; it must release (Deref()) the snapshot and report/handle the closed session cleanly.
Decisions and constraints
- Follow all shared constraints in End-to-end red-to-green TLA+ fixes and upstream PR prep for flagship TypeScript 7 concurrency bugs #1 (clean fix branch from
mainate0fce98, no unsolicited PR againstmicrosoft/TypeScript, officialtla2tools.jaronly). getSnapshotDatais called across 480+ handlers intsc/internal/api/session.go; preserve clean ergonomics (e.g., returning a release callback or scoped helper) and avoid holdingsnapshotsMuacross slow checker/program work.- Preserve existing
handleReleaseerror behavior when releasing an unknown or already-released snapshot handle, and preserve layered-filesystem compaction behavior tested insession_requestfilesystem_test.go.
Acceptance criteria
- Green TLA+ model: Add a fixed configuration (and updated spec transitions on
tla-concurrency-verification) checked bytla2tools.jar/ TLC that verifiesNoUseAfterDeref,NoLeakAfterClose, no negative refcount / double-Deref, and quiescent refcount cleanup (0 errors across all reachable states), wired intoverification/tla/scripts/run-tlc.sh. - Go fix and regression tests: On a clean fix branch off
main(e0fce98), fixtsc/internal/api/session.goand add in-package regression tests covering:- Active snapshot query concurrent with
handleRelease(underlyingproject.Snapshotstays alive until query completes, then disposes cleanly without negative refcount). - In-flight
registerSnapshotracingSession.Close()(no entry left ins.snapshots, retained snapshot is deref'd without panic or leak).
- Active snapshot query concurrent with
- Verification:
go -C ./tsc test -race -timeout=120s ./internal/api/...passes (including existingsession_requestfilesystem_test.gocompaction tests and new regression tests), andnpx hereby check:formatandnpx hereby lintpass for touched files. - Upstream preparation: Include a concise ready-to-file upstream bug report and PR description (referencing the fix branch on
robertpenner/TypeScript) in an issue comment when done.
Dependencies
None. Independently executable.
Starting references
tsc/internal/api/session.go:703-743(getSnapshotData,retainSnapshotData,releaseSnapshot)tsc/internal/api/session.go:1865-1891(registerSnapshot),5789-5810(Close)tsc/internal/api/session_requestfilesystem_test.goverification/tla/specs/ApiSessionSnapshot/ApiSessionSnapshot.tlaverification/tla/reproducers/api_session_repro_test.go
Activity
robertpenner commentedon Nov 26, 2026
Just-in-Time Agent Brief (#3)
e0fce98(main),417e8a9(tla-concurrency-verification).tsc/internal/api/session.go:703-711:getSnapshotData(38 call sites insession.go, includingsetupChecker) readss.snapshots[handle]undersnapshotsMu.RLock()and unlocks without retaining the underlying*project.Snapshot. ConcurrenthandleReleaseorClosedropssd.snapshotto 0 and disposes it mid-query. Additionally,retainSnapshotDatabumped clientsd.refCount, which still raced withClose()and erroneous doublehandleRelease.tsc/internal/api/session.go:1865-1891, 5789-5810:Session.Close()clearss.snapshotswithout recordings.closed = true; a concurrentregisterSnapshot()can insert a retained*project.SnapshotafterClose()returns, leaking it permanently.sd.refCountstrictly for client-owned references (registerSnapshot/handleRelease).getSnapshotData(handle)retainsd.snapshotvias.snapshotHost.RetainSnapshot(sd.snapshot)while holdings.snapshotsMu.RLock()and return(sd, release, err)wherereleasecallssd.snapshot.Deref()once (sync.OnceFunc). ReplaceretainSnapshotDatawithgetSnapshotDataas well so snapshot derivation is also safe againstClose()and doublehandleRelease.setupChecker, release the snapshot on error paths and foldrelease()intosetup.done().closed boolguarded bys.snapshotsMu:Close()setss.closed = truebefore clearings.snapshots;registerSnapshot()checkss.closed, derefs the snapshot if closed, and returns an error so callers do not leak snapshots after close.ApiSessionSnapshot_Fixed.cfg, 0 errors onNoUseAfterDeref,NoLeakAfterClose,NoNegativeSnapRef,QuiescentNoLeak, deadlock-free), TDD regression tests undergo -C ./tsc test -race ./internal/api/..., andgofmt/go vet.robertpenner commentedon Nov 26, 2026
Slice Complete: Red-to-Green Verified (#3)
fix/api-snapshot-lifecycle(based onmaine0fce98)robertpenner/TypeScript(Closes #3)ApiSessionSnapshot_Fixed.cfgpassed with 0 errors (36 distinct states, 69 generated, deadlock checking enabled) onNoUseAfterDeref,NoLeakAfterClose,NoNegativeSnapRef, andQuiescentClean.go -C ./tsc test -race -count=1 ./internal/api/...passed (4/4 packages, includingTestGetSnapshotDataRetainsAcrossRelease,TestGetSnapshotDataRetainsAcrossSessionClose, andTestRegisterSnapshotAfterCloseRejectsAndDerefs);gofmtandgo vetclean.Draft Upstream Bug Report (for
microsoft/TypeScript)Title:
[api] Concurrent query handlers can use a disposed project.Snapshot after release or Close; registerSnapshot can leak snapshots after CloseDescription:
In
tsc/internal/api/session.go:getSnapshotData(handle)looks up*snapshotDataunders.snapshotsMu.RLock()and unlocks without retaining the underlying*project.Snapshot. Becauseipc.AsyncConnexecutes incoming requests in concurrent goroutines, a concurrenthandleReleaseorSession.Close()can dropsd.snapshot's refcount to zero (disposingcheckerPool, dereferencingparseCache, and releasingextendedConfigCache) while an active query handler is still usingsd.snapshot.Session.Close()callsclear(s.snapshots)without setting aclosedflag, so an in-flightcreateSnapshot/updateSnapshotcallingregisterSnapshot()afterward inserts a retained*project.Snapshotinto the emptied map and leaks it permanently.Pinning
sd.snapshotvias.snapshotHost.RetainSnapshot(sd.snapshot)unders.snapshotsMu.RLock()(returning an idempotentDeref()release callback) and guardingregisterSnapshotwith aclosedflag unders.snapshotsMufixes both races while keeping read handlers onRLock().(Clean fix branch ready at
robertpenner/TypeScript:fix/api-snapshot-lifecycleonce an upstream issue is milestoned.)test(verification): verify ApiSessionSnapshot green TLA+ model (#3)