Propagate RPC cancellation to daemon requests
Stop long-running daemon operations from running under context.Background by\nthreading a request-scoped context from handleConn into dispatch. The daemon\nnow cancels in-flight handlers when the client socket goes away, and the RPC\nclient closes its Unix connection when the caller context is canceled so that\ninterrupts actually reach the daemon boundary.\n\nAdd regression coverage for both sides of the path: canceled dispatch calls,\nclient disconnects during handleConn, watcher EOF cancellation, and context\ncancellation without an RPC deadline.\n\nValidated with GOCACHE=/tmp/banger-gocache go test ./... and\nGOCACHE=/tmp/banger-gocache make build.
This commit is contained in:
parent
ebb68c3126
commit
ccba07ec68
4 changed files with 220 additions and 16 deletions
|
|
@ -1,13 +1,19 @@
|
|||
package daemon
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"banger/internal/api"
|
||||
"banger/internal/model"
|
||||
"banger/internal/rpc"
|
||||
"banger/internal/store"
|
||||
)
|
||||
|
||||
|
|
@ -330,3 +336,107 @@ func TestSetDNSUsesMapDNSDefaultsWhenDataFileUnset(t *testing.T) {
|
|||
}
|
||||
runner.assertExhausted()
|
||||
}
|
||||
|
||||
func TestDispatchUsesPassedContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := openDefaultImageStore(t, t.TempDir())
|
||||
d := &Daemon{store: db}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
resp := d.dispatch(ctx, rpc.Request{
|
||||
Version: rpc.Version,
|
||||
Method: "vm.list",
|
||||
Params: mustJSON(t, api.Empty{}),
|
||||
})
|
||||
|
||||
if resp.OK {
|
||||
t.Fatal("dispatch() succeeded with canceled context")
|
||||
}
|
||||
if resp.Error == nil || !strings.Contains(resp.Error.Message, context.Canceled.Error()) {
|
||||
t.Fatalf("dispatch() error = %+v, want context canceled", resp.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleConnCancelsRequestWhenClientDisconnects(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server, client := net.Pipe()
|
||||
defer client.Close()
|
||||
|
||||
requestCanceled := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
d := &Daemon{
|
||||
closing: make(chan struct{}),
|
||||
requestHandler: func(ctx context.Context, req rpc.Request) rpc.Response {
|
||||
if req.Method != "block" {
|
||||
t.Errorf("request method = %q, want block", req.Method)
|
||||
}
|
||||
<-ctx.Done()
|
||||
close(requestCanceled)
|
||||
return rpc.NewError("operation_failed", ctx.Err().Error())
|
||||
},
|
||||
}
|
||||
|
||||
go func() {
|
||||
d.handleConn(server)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
if err := json.NewEncoder(client).Encode(rpc.Request{Version: rpc.Version, Method: "block"}); err != nil {
|
||||
t.Fatalf("encode request: %v", err)
|
||||
}
|
||||
if err := client.Close(); err != nil {
|
||||
t.Fatalf("close client: %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-requestCanceled:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("request context was not canceled after client disconnect")
|
||||
}
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("handleConn did not return after client disconnect")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatchRequestDisconnectCancelsContextOnEOF(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server, client := net.Pipe()
|
||||
defer server.Close()
|
||||
|
||||
reader := bufio.NewReader(server)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
d := &Daemon{closing: make(chan struct{})}
|
||||
stop := d.watchRequestDisconnect(server, reader, "block", cancel)
|
||||
defer stop()
|
||||
|
||||
if err := client.Close(); err != nil {
|
||||
t.Fatalf("close client: %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !strings.Contains(ctx.Err().Error(), context.Canceled.Error()) {
|
||||
t.Fatalf("ctx.Err() = %v, want canceled", ctx.Err())
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("watchRequestDisconnect did not cancel context")
|
||||
}
|
||||
}
|
||||
|
||||
func mustJSON(t *testing.T, v any) []byte {
|
||||
t.Helper()
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal(%T): %v", v, err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue