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_test.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_test.go')
| -rw-r--r-- | internal/lsp/server_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/lsp/server_test.go b/internal/lsp/server_test.go index 1f33a46..40e8f76 100644 --- a/internal/lsp/server_test.go +++ b/internal/lsp/server_test.go @@ -1,8 +1,12 @@ package lsp import ( + "bytes" "context" + "io" + "log" "testing" + "time" "codeberg.org/snonux/hexai/internal/appconfig" "codeberg.org/snonux/hexai/internal/llm" @@ -86,6 +90,60 @@ func TestServerApplyOptions(t *testing.T) { } } +// TestRunReturnsOnEOF verifies the serve loop exits cleanly when the input +// stream reaches EOF, regardless of the parent context being active. +func TestRunReturnsOnEOF(t *testing.T) { + srv := NewServer(bytes.NewReader(nil), &bytes.Buffer{}, log.New(io.Discard, "", 0), ServerOptions{}) + if err := srv.Run(context.Background()); err != nil { + t.Fatalf("Run on EOF returned error: %v", err) + } +} + +// TestRunCancelsServerContextOnParentCancel verifies that cancelling the +// caller-provided context propagates into the server's own context (so +// in-flight LLM/network work is aborted) via watchParentContext. +func TestRunCancelsServerContextOnParentCancel(t *testing.T) { + // A pipe whose write end is never written blocks the serve loop's read, + // so Run only returns once the parent context cancellation unblocks it by + // the input being closed. + pr, pw := io.Pipe() + srv := NewServer(pr, &bytes.Buffer{}, log.New(io.Discard, "", 0), ServerOptions{}) + + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { done <- srv.Run(ctx) }() + + // Cancel the parent: watchParentContext should cancel the server context. + cancel() + // Allow the watcher goroutine to observe cancellation. + deadline := time.After(2 * time.Second) + for srv.serverCtx.Err() == nil { + select { + case <-deadline: + t.Fatal("server context was not cancelled after parent cancel") + default: + } + } + // Unblock the read so Run can return. + _ = pw.Close() + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("Run did not return after input closed") + } +} + +// TestWatchParentContextNilIsNoOp verifies a nil/background context yields a +// no-op stop function and never cancels the server. +func TestWatchParentContextNilIsNoOp(t *testing.T) { + srv := NewServer(bytes.NewReader(nil), &bytes.Buffer{}, log.New(io.Discard, "", 0), ServerOptions{}) + stop := srv.watchParentContext(nil) + stop() + if srv.serverCtx.Err() != nil { + t.Fatalf("server context should remain active for nil parent ctx") + } +} + func TestServerStoreAndTakePendingCompletion(t *testing.T) { s := newTestServer() items := []CompletionItem{{Label: "foo"}} |
