From 6124eb59cfa76d7bb3d60301409696791aca5bba Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Fri, 24 May 2024 11:54:03 +0200 Subject: [PATCH] go/runtime/host: Ignore stale abort requests --- .changelog/5702.bugfix.md | 1 + go/runtime/host/sandbox/sandbox.go | 31 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .changelog/5702.bugfix.md diff --git a/.changelog/5702.bugfix.md b/.changelog/5702.bugfix.md new file mode 100644 index 00000000000..e3b35c57ae4 --- /dev/null +++ b/.changelog/5702.bugfix.md @@ -0,0 +1 @@ +go/runtime/host: Ignore stale abort requests diff --git a/go/runtime/host/sandbox/sandbox.go b/go/runtime/host/sandbox/sandbox.go index 41be43d7870..1f6cf380c76 100644 --- a/go/runtime/host/sandbox/sandbox.go +++ b/go/runtime/host/sandbox/sandbox.go @@ -237,12 +237,21 @@ func (r *sandboxedRuntime) Start() { // Implements host.Runtime. func (r *sandboxedRuntime) Abort(ctx context.Context, force bool) error { + // Ignore abort requests when connection is not available. + r.RLock() + if r.conn == nil { + r.RUnlock() + return nil + } + r.RUnlock() + // Send internal request to the manager goroutine. ch := make(chan error, 1) select { case r.ctrlCh <- &abortRequest{ch: ch, force: force}: - case <-ctx.Done(): - return ctx.Err() + default: + // If the command channel is full, do not queue more abort requests. + return fmt.Errorf("command channel is full") } // Wait for response from the manager goroutine. @@ -564,6 +573,24 @@ func (r *sandboxedRuntime) manager() { continue } + + // Ensure the command queue is empty to avoid processing any stale requests after the + // runtime restarts. + drainLoop: + for { + select { + case grq := <-r.ctrlCh: + switch rq := grq.(type) { + case *abortRequest: + rq.ch <- fmt.Errorf("runtime restarted") + close(rq.ch) + default: + // Ignore unknown requests. + } + default: + break drainLoop + } + } } // Wait for either the runtime or the runtime manager to terminate.