Repository navigation
Revert "Make LS checkers time out after being idle, segment based on use" - #3425
Conversation
There was a problem hiding this comment.
Pull request overview
This PR reverts the recently-added “idle timeout / segmentation” logic around language-service checker pools, aiming to address the reported memory spike in recent dev builds by returning to simpler checker lifecycle behavior.
Changes:
- Removes checker lifetime plumbing (core context key + call sites) and session-level checker pool options.
- Reworks
internal/projectchecker pool implementation back toward a simpler model and removes the associated test suite. - Removes “discard checker pool” hooks during snapshot/project disposal and program updates.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/project/snapshot.go | Stops discarding checker pools during snapshot disposal. |
| internal/project/session.go | Removes CheckerPoolOptions from session options. |
| internal/project/projectcollectionbuilder.go | Stops discarding old pools and updates project pool assignment path. |
| internal/project/project.go | Changes project checker pool type/ownership and wires pool creation into program creation. |
| internal/project/checkerpool.go | Replaces the segmented/idle-timeout pool with a simpler cond-based pool. |
| internal/project/checkerpool_test.go | Deletes checker pool tests added by the reverted change. |
| internal/lsp/server.go | Removes diagnostic checker lifetime tagging. |
| internal/core/context.go | Removes checker lifetime from context helpers/types. |
| internal/compiler/program.go | Removes Program.GetCheckerPool() accessor. |
| internal/api/session.go | Removes API checker lifetime tagging when acquiring a checker. |
| @@ -369,7 +371,8 @@ func (p *Project) CreateProgram() CreateProgramResult { | |||
| UseSourceOfProjectReference: true, | |||
| TypingsLocation: typingsLocation, | |||
| CreateCheckerPool: func(program *compiler.Program) compiler.CheckerPool { | |||
| return newCheckerPool(p.host.sessionOptions.CheckerPoolOptions, program, p.log) | |||
| checkerPool = newCheckerPool(4, program, p.log) | |||
| return checkerPool | |||
| }, | |||
| }, | |||
| ) | |||
| @@ -382,8 +385,9 @@ func (p *Project) CreateProgram() CreateProgramResult { | |||
| newProgram.BindSourceFiles() | |||
|
|
|||
| return CreateProgramResult{ | |||
| Program: newProgram, | |||
| UpdateKind: updateKind, | |||
| Program: newProgram, | |||
| UpdateKind: updateKind, | |||
| CheckerPool: checkerPool, | |||
| } | |||
There was a problem hiding this comment.
CreateProgramResult.CheckerPool is only set via the CreateCheckerPool closure in the full NewProgram path. When CreateProgram uses Program.UpdateProgram (cloned/incremental path), the local checkerPool variable never gets populated, so this returns nil and later clears project.checkerPool, breaking global diagnostics accumulation/publishing (and any other uses of project.checkerPool). Ensure CheckerPool is set for UpdateProgram results too (e.g., restore a Program-level accessor for the created pool, or otherwise propagate the pool created inside Program.initCheckerPool).
| result := project.CreateProgram() | ||
| project.Program = result.Program | ||
| project.checkerPool = result.Program.GetCheckerPool().(*checkerPool) | ||
| project.checkerPool = result.CheckerPool |
There was a problem hiding this comment.
project.checkerPool is set from result.CheckerPool, but CreateProgram can currently return a nil CheckerPool when it goes through Program.UpdateProgram. That will clear the existing pool and stops global diagnostics tracking/publishing for configured projects. Consider keeping the existing pool when UpdateProgram is used, or retrieving the checker pool from the resulting Program so this assignment is never nil.
| project.checkerPool = result.CheckerPool | |
| if result.CheckerPool != nil { | |
| project.checkerPool = result.CheckerPool | |
| } |
| func newCheckerPool(maxCheckers int, program *compiler.Program, log func(msg string)) *CheckerPool { | ||
| pool := &CheckerPool{ | ||
| program: program, | ||
| opts: opts, | ||
| checkers: make([]*checker.Checker, opts.MaxCheckers), | ||
| heldBy: make([]string, opts.MaxCheckers), | ||
| fileAssociations: make(map[*ast.SourceFile]int), | ||
| maxCheckers: maxCheckers, | ||
| checkers: make([]*checker.Checker, maxCheckers), | ||
| inUse: make(map[*checker.Checker]bool), | ||
| requestAssociations: make(map[string]int), | ||
| lastReleased: make([]time.Time, opts.MaxCheckers), | ||
| diagSem: make(chan struct{}, 1), | ||
| querySem: make(chan struct{}, querySlots), | ||
| persistentSem: make(chan struct{}, 1), | ||
| log: log, | ||
| globalDiagCheckerCount: make([]int, opts.MaxCheckers), | ||
| } | ||
| return pool | ||
| } | ||
|
|
||
| // holdTag returns the value to store in heldBy for the given request ID. | ||
| func holdTag(requestID string) string { | ||
| if requestID == "" { | ||
| return checkerHeldAnonymous | ||
| } | ||
| return requestID | ||
| } | ||
|
|
||
| func (p *checkerPool) GetChecker(ctx context.Context, file *ast.SourceFile) (*checker.Checker, func()) { | ||
| lifetime := core.GetCheckerLifetime(ctx) | ||
| requestID := core.GetRequestID(ctx) | ||
|
|
||
| switch lifetime { | ||
| case core.CheckerLifetimeDiagnostics: | ||
| return p.getDiagnosticsChecker(ctx, requestID) | ||
| case core.CheckerLifetimeAPI: | ||
| return p.getPersistentChecker() | ||
| default: | ||
| return p.getQueryChecker(ctx, requestID, file) | ||
| } | ||
| } | ||
|
|
||
| // tryReacquireForRequest checks whether the given request already has an | ||
| // associated checker. If so, it either returns the checker directly (still held) | ||
| // or reacquires it by claiming a semaphore slot. The caller must provide the | ||
| // appropriate semaphore channel and indicate whether this is a diagnostics | ||
| // request (isDiag). If the associated checker is in the wrong category | ||
| // (e.g. a diagnostics index for a query request), the association is deleted | ||
| // and normal acquisition proceeds. | ||
| // | ||
| // Returns (checker, release, true) if the request was served (either still held | ||
| // or reclaimed). Returns (nil, nil, false) if the caller must proceed with | ||
| // normal acquisition — in this case, a semaphore slot has already been claimed. | ||
| // Must NOT be called with p.mu held. | ||
| func (p *checkerPool) tryReacquireForRequest(requestID string, sem chan<- struct{}, isDiag bool) (*checker.Checker, func(), bool) { | ||
| if requestID == "" { | ||
| sem <- struct{}{} | ||
| return nil, nil, false | ||
| } | ||
|
|
||
| p.mu.Lock() | ||
| index, ok := p.requestAssociations[requestID] | ||
| if !ok { | ||
| p.mu.Unlock() | ||
| sem <- struct{}{} | ||
| return nil, nil, false | ||
| } | ||
|
|
||
| // Validate that the associated index matches the expected category. | ||
| // Index 0 is for diagnostics; indices 1+ are for queries. | ||
| if (isDiag && index != 0) || (!isDiag && index == 0) { | ||
| delete(p.requestAssociations, requestID) | ||
| p.mu.Unlock() | ||
| sem <- struct{}{} | ||
| return nil, nil, false | ||
| globalDiagCheckerCount: make([]int, maxCheckers), | ||
| } |
There was a problem hiding this comment.
newCheckerPool doesn't validate maxCheckers. If it is 0 (or negative), checkers is empty, isFullLocked() returns true, and GetChecker will block forever in waitForAvailableChecker because no checker can ever be created. Clamp maxCheckers to at least 1 (and ideally a sensible default like 4) to avoid a hard-to-debug deadlock if the caller passes 0.
| func (p *CheckerPool) GetChecker(ctx context.Context, file *ast.SourceFile) (*checker.Checker, func()) { | ||
| p.mu.Lock() | ||
| defer p.mu.Unlock() | ||
|
|
||
| if p.checkers[diagIndex] == nil { | ||
| p.log("checkerpool: Creating diagnostics checker") | ||
| c, _ := checker.NewChecker(p.program) | ||
| p.checkers[diagIndex] = c | ||
| } | ||
|
|
||
| c := p.checkers[diagIndex] | ||
| p.heldBy[diagIndex] = holdTag(requestID) | ||
| p.log("checkerpool: Acquired diagnostics checker for request " + holdTag(requestID)) | ||
| requestID := core.GetRequestID(ctx) | ||
| if requestID != "" { | ||
| if _, alreadyRegistered := p.requestAssociations[requestID]; !alreadyRegistered { | ||
| p.requestAssociations[requestID] = diagIndex | ||
| p.registerRequestCleanup(ctx, requestID) | ||
| if checker, release := p.getRequestCheckerLocked(requestID); checker != nil { | ||
| return checker, release | ||
| } | ||
| } | ||
| return c, p.createRelease(requestID, diagIndex, c) | ||
| } | ||
|
|
||
| // getQueryChecker returns an ephemeral query checker from indices 1+. | ||
| // Uses request affinity, then file affinity, then finds/creates. | ||
| // Blocks on querySem if all query slots are in use. | ||
| func (p *checkerPool) getQueryChecker(ctx context.Context, requestID string, file *ast.SourceFile) (*checker.Checker, func()) { | ||
| if c, release, ok := p.tryReacquireForRequest(requestID, p.querySem, false); ok { | ||
| return c, release | ||
| } | ||
|
|
||
| // Token consumed — proceed with normal acquisition. | ||
| p.mu.Lock() | ||
| defer p.mu.Unlock() | ||
|
|
||
| // Try file affinity. | ||
| if file != nil { | ||
| if index, ok := p.fileAssociations[file]; ok && index > 0 { | ||
| if c := p.checkers[index]; c != nil && p.heldBy[index] == "" { | ||
| p.heldBy[index] = holdTag(requestID) | ||
| if requestID != "" { | ||
| if _, alreadyRegistered := p.requestAssociations[requestID]; !alreadyRegistered { | ||
| if p.fileAssociations == nil { | ||
| p.fileAssociations = make(map[*ast.SourceFile]int) | ||
| } | ||
|
|
||
| if index, ok := p.fileAssociations[file]; ok { | ||
| checker := p.checkers[index] | ||
| if checker != nil { | ||
| if inUse := p.inUse[checker]; !inUse { | ||
| p.inUse[checker] = true | ||
| if requestID != "" { | ||
| p.requestAssociations[requestID] = index | ||
| p.registerRequestCleanup(ctx, requestID) | ||
| } | ||
| return checker, p.createRelease(requestID, index, checker) | ||
| } | ||
| return c, p.createRelease(requestID, index, c) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Find any available query checker or create one. | ||
| c, index := p.findOrCreateQueryCheckerLocked() | ||
| p.heldBy[index] = holdTag(requestID) | ||
| p.log(fmt.Sprintf("checkerpool: Acquired query checker %d for request %s", index, holdTag(requestID))) | ||
| if requestID != "" { | ||
| if _, alreadyRegistered := p.requestAssociations[requestID]; !alreadyRegistered { | ||
| p.requestAssociations[requestID] = index | ||
| p.registerRequestCleanup(ctx, requestID) | ||
| } | ||
| } | ||
| checker, index := p.getCheckerLocked(requestID) | ||
| if file != nil { | ||
| if p.fileAssociations == nil { | ||
| p.fileAssociations = make(map[*ast.SourceFile]int) | ||
| } | ||
| p.fileAssociations[file] = index | ||
| } | ||
| return c, p.createRelease(requestID, index, c) | ||
| return checker, p.createRelease(requestID, index, checker) | ||
| } |
There was a problem hiding this comment.
For file == nil, this pool may return any available checker (depending on contention), which can break stable type/symbol identity across calls because checker.Type instances are tied to a specific *checker.Checker. The compiler's default CheckerPool always uses checker[0] for file == nil to avoid mixing types across checkers. Consider reserving a dedicated checker for nil-file acquisitions (or always waiting for index 0) so API sessions and other nil-file callers don't end up creating handles from multiple checkers over time.
Reverts #3363
I'm going to take a wild guess and say that this fixes #3423.
I don't think it'd be semantic tokens, as as one types, things get cleaned up due to snapshots GCing. But the checker pool stuff could have a bug that causes retention.
I'd rather a clean revert for now.