diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-11 08:34:41 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-11 08:34:41 +0300 |
| commit | e95f3fdf0a66ba05ba2c8fb7e755e107f9cf7991 (patch) | |
| tree | 412c7e21ec9c317beb99ed7fe0d4d93a7dacbe50 /internal/lsp/server.go | |
| parent | 73dadb573f92dca310036e8793932e94277abd62 (diff) | |
Thread context.Context through blocking I/O entry points
Accept ctx as the first parameter on the blocking I/O entry points and
propagate it to downstream blocking calls so the work is cancellable from
the process entry point:
- appconfig.Load / LoadWithOptions: honor ctx before the blocking file
reads, returning defaults on a cancelled context.
- LSP: lsp.Server.Run(ctx) ties the serve loop to the caller context via a
new watchParentContext bridge (cancels the server context, aborting
in-flight LLM work). Threaded through hexailsp.Run/RunWithConfig/
RunWithFactory and runtimeconfig.Store.Reload.
- MCP: mcp.Server.Run(ctx) stops accepting requests once ctx is cancelled;
threaded through hexaimcp.Run/RunWithFactory/RunBackfill.
- editor: RunEditor/OpenTempAndEdit/OpenFile take ctx and use
exec.CommandContext so a cancelled context kills the editor subprocess;
threaded through hexaicli, hexaiaction and askcli call sites.
Top-level callers (cmd/hexai-lsp-server, cmd/hexai-mcp-server) now build a
signal-cancelled context (SIGINT/SIGTERM) so shutdown tears the run down
cleanly. Updated comments to explain the cancellation flow and added
cancellation tests for the LSP/MCP loops, editor, and config load.
All tests pass with -race; cross-package coverage 86.2%.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/lsp/server.go')
| -rw-r--r-- | internal/lsp/server.go | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/internal/lsp/server.go b/internal/lsp/server.go index e42179d..966ec90 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -345,6 +345,27 @@ func (s *Server) cancelRequests() { } } +// watchParentContext propagates cancellation from the caller-provided ctx into +// the server's own context. When ctx is cancelled we call cancelRequests so any +// in-flight LLM/network work tied to s.serverCtx is aborted; the main loop then +// returns once the current blocking read unblocks. It returns a stop function +// that tears down the watcher goroutine when Run exits normally (EOF), so the +// goroutine never leaks. A nil ctx (or context.Background) yields a no-op. +func (s *Server) watchParentContext(ctx context.Context) func() { + if ctx == nil || ctx.Done() == nil { + return func() {} + } + done := make(chan struct{}) + go func() { + select { + case <-ctx.Done(): + s.cancelRequests() + case <-done: + } + }() + return func() { close(done) } +} + func (s *Server) emitLLMStartStatus(provider, model string) { if s.statusSink != nil { if err := s.statusSink.SetLLMStart(provider, model); err != nil { @@ -363,7 +384,14 @@ func (s *Server) emitGlobalStatus(gs GlobalStatus) { // Run starts the server's main loop, reading and dispatching LSP messages until EOF or exit. // On shutdown it cancels the server context and waits for in-flight goroutines. -func (s *Server) Run() error { +// +// The supplied ctx ties the serve loop to the process lifecycle: when the +// caller's context is cancelled (e.g. SIGINT/SIGTERM at main), we cancel the +// internal server context so in-flight LLM/network requests are aborted and the +// blocking stdin read is unblocked, letting Run return promptly. +func (s *Server) Run(ctx context.Context) error { + stopWatch := s.watchParentContext(ctx) + defer stopWatch() defer func() { s.cancelRequests() s.inflight.Wait() |
