fix: let queued remote registry reads honour their context
Astra round 1 P2-1: remoteClient.call took a mutex before looking at its context, so a context-aware read queued behind another request on the same handle could wait past its deadline. The request slot is now a one-place semaphore selected against ctx.Done(); a nil context still waits, and only one request is ever on the pipe. TestFleetContextRemoteQueued parks one request, proves a second read returns DeadlineExceeded before the first is released, and that the first completes on the same connection afterwards.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+103
−8
internal/registry/remote.go+26 −8internal/registry/remote_test.go+77 −0
diff --git a/internal/registry/remote.go b/internal/registry/remote.go| index 483b724a7..6d32baac6 100644 |
| --- a/internal/registry/remote.go |
| +++ b/internal/registry/remote.go |
| @@ -8,7 +8,6 @@ import ( |
| "fmt" |
| "io" |
| "os" |
| - "sync" |
| "time" |
| ) |
| @@ -45,7 +44,9 @@ func setResponseError(out *remoteResponse, err error) { |
| } |
| type remoteClient struct { |
| - mu sync.Mutex |
| + // slot is the one request in flight on the pipe: a one-place semaphore |
| + // rather than a mutex, so a caller's context can end its wait for it. |
| + slot chan struct{} |
| enc *gob.Encoder |
| dec *gob.Decoder |
| input io.WriteCloser |
| @@ -57,7 +58,7 @@ type remoteClient struct { |
| // OpenRemote connects to an already privilege-dropped registry worker. |
| func OpenRemote(path string, input io.WriteCloser, output io.ReadCloser, wait func() error) (*Registry, error) { |
| - c := &remoteClient{} |
| + c := &remoteClient{slot: make(chan struct{}, 1)} |
| if err := c.attach(input, output, wait); err != nil { |
| return nil, err |
| } |
| @@ -68,7 +69,7 @@ func OpenRemote(path string, input io.WriteCloser, output io.ReadCloser, wait fu |
| // Failed or cancelled mutations are never retried; the next call reconnects. |
| // Canceled does not mean not applied: cancellation can race a committed mutation. |
| func OpenRemoteReconnect(path string, connect func() (io.WriteCloser, io.ReadCloser, func() error, error)) (*Registry, error) { |
| - c := &remoteClient{connect: connect} |
| + c := &remoteClient{slot: make(chan struct{}, 1), connect: connect} |
| if err := c.reconnect(context.Background()); err != nil { |
| return nil, err |
| } |
| @@ -129,15 +130,32 @@ func (c *remoteClient) stop() error { |
| return c.disconnect() |
| } |
| +// acquire takes the request slot; a nil ctx waits as long as it takes. |
| +func (c *remoteClient) acquire(ctx context.Context) error { |
| + if ctx == nil { |
| + c.slot <- struct{}{} |
| + return nil |
| + } |
| + select { |
| + case c.slot <- struct{}{}: |
| + return nil |
| + case <-ctx.Done(): |
| + return ctx.Err() |
| + } |
| +} |
| +func (c *remoteClient) release() { <-c.slot } |
| + |
| func (c *remoteClient) close() error { |
| - c.mu.Lock() |
| - defer c.mu.Unlock() |
| + c.acquire(nil) |
| + defer c.release() |
| return c.stop() |
| } |
| func (c *remoteClient) call(ctx context.Context, q remoteRequest, callback func(remoteResponse) error) (out remoteResponse, err error) { |
| - c.mu.Lock() |
| - defer c.mu.Unlock() |
| + if err = c.acquire(ctx); err != nil { |
| + return |
| + } |
| + defer c.release() |
| if c.closed { |
| return out, os.ErrClosed |
| } |
diff --git a/internal/registry/remote_test.go b/internal/registry/remote_test.go| index c2165d55e..9ca9e131f 100644 |
| --- a/internal/registry/remote_test.go |
| +++ b/internal/registry/remote_test.go |
| @@ -367,3 +367,80 @@ func TestFleetContextRemote(t *testing.T) { |
| t.Fatalf("legacy read after cancellation = %+v, %v", f, err) |
| } |
| } |
| + |
| +// A read queued behind another request on the same handle honours its own |
| +// context while it waits for the slot, and the parked request it waited |
| +// behind is neither disconnected nor disturbed. |
| +func TestFleetContextRemoteQueued(t *testing.T) { |
| + r, _ := remoteTestRegistry(t) |
| + if err := r.remote.disconnect(); err != nil { |
| + t.Fatal(err) |
| + } |
| + requestR, requestW := io.Pipe() |
| + responseR, responseW := io.Pipe() |
| + defer requestR.Close() |
| + defer responseW.Close() |
| + release := make(chan struct{}) |
| + entered := make(chan struct{}) |
| + connects := 0 |
| + r.remote.connect = func() (io.WriteCloser, io.ReadCloser, func() error, error) { |
| + connects++ |
| + go func() { |
| + enc, dec := gob.NewEncoder(responseW), gob.NewDecoder(requestR) |
| + if enc.Encode(remoteResponse{}) != nil { |
| + return |
| + } |
| + for first := true; ; first = false { |
| + var q remoteRequest |
| + if dec.Decode(&q) != nil { |
| + return |
| + } |
| + if first { |
| + close(entered) |
| + <-release |
| + } |
| + if enc.Encode(remoteResponse{Fleet: &Fleet{Name: q.Name}}) != nil { |
| + return |
| + } |
| + } |
| + }() |
| + return requestW, responseR, func() error { return nil }, nil |
| + } |
| + parked := make(chan error, 1) |
| + go func() { |
| + f, err := r.GetFleet("production") |
| + if err == nil && (f == nil || f.Name != "production") { |
| + err = errors.New("parked request lost its answer") |
| + } |
| + parked <- err |
| + }() |
| + select { |
| + case <-entered: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("first request never reached the worker") |
| + } |
| + ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond) |
| + defer cancel() |
| + queued := make(chan error, 1) |
| + go func() { |
| + _, err := r.GetFleetContext(ctx, "queued") |
| + queued <- err |
| + }() |
| + select { |
| + case err := <-queued: |
| + if !errors.Is(err, context.DeadlineExceeded) { |
| + t.Fatalf("queued read = %v", err) |
| + } |
| + case <-time.After(5 * time.Second): |
| + close(release) |
| + <-queued |
| + t.Fatal("queued read waited past its deadline behind another request") |
| + } |
| + close(release) |
| + if err := <-parked; err != nil { |
| + t.Fatalf("parked request: %v", err) |
| + } |
| + if f, err := r.GetFleet("after"); err != nil || f == nil || f.Name != "after" || connects != 1 { |
| + t.Fatalf("after release: %+v %v, connects=%d", f, err, connects) |
| + } |
| +} |