Skip to content

Commit

Permalink
Merge pull request #5702 from oasisprotocol/kostko/fix/rt-abort-incor…
Browse files Browse the repository at this point in the history
…rect

go/runtime/host: Ignore stale abort requests
  • Loading branch information
kostko authored May 24, 2024
2 parents 6a6ea9e + 6124eb5 commit 7f4cb9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changelog/5702.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/runtime/host: Ignore stale abort requests
31 changes: 29 additions & 2 deletions go/runtime/host/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7f4cb9a

Please sign in to comment.