Queued VM provision blocks shutdown and further SIGINT is swallowed
Report
Summary
Node shutdown is supposed to cancel in-flight work and exit, with a second SIGINT restoring default terminate. cmd/xe/main.go:1559-1563 reads sigCh once (buffer 1, Notify never Stopped); defer n.Stop at :1419 runs cancel then wg.Wait then store.Close (node.go:1301-1308). Gossip auto-accept spawn at node.go:721-726 does n.wg.Add(1) around autoAcceptLease then Provision; LimaManager.Provision at vm/lima_manager.go:94-99 holds m.mu across create/start/SSH wait, and runLimactlWithTimeout at :296 uses time.After at :319, not n.ctx (call sites at :118-131), so cancel() does not unblock it. An operator hitting Ctrl-C during auto-accept on a provider node takes this path. GitHub #44 is sync OOM and desk XE-0041 / xe#60 is SIGTERM during sync hanging shutdown; this is lima time.After plus Stop wg.Wait plus Notify buffer-1 on the provider path.
Affected components
cmd/xe/main.go:1559-1563 signal.Notify buffer-1 single read; cmd/xe/main.go:1419 defer Stop; node/node.go:721-726 autoAcceptLease wg.Add around Provision; node/node.go:1301-1308 Node.Stop (cancel, wg.Wait, store.Close); vm/lima_manager.go:94-99 Provision m.mu; vm/lima_manager.go:118-131 Provision call sites; vm/lima_manager.go:296 runLimactlWithTimeout; vm/lima_manager.go:319 time.After (ignores n.ctx) at xe 02e9641b0ac64437f57e09d63301f88cce61a802.
Impact
An operator hitting Ctrl-C during auto-accept waits on lima. By source read (not shown in the run): further SIGINT/SIGTERM do not restore default terminate, and the process is stuck until lima's own timeout or an external SIGKILL that skips a clean store.Close. Victim is a provider (--provide); default capacity bounds in-flight count to about one lima cycle, not hours.
PoC
package node
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/xeprotocol/xe/core"
"github.com/xeprotocol/xe/store"
)
// TestPoC_Q2Stop: Node.Stop waits on n.wg after cancel(); a provision-like
// goroutine holding wg (and a mutex) is not unblocked by cancel().
//
// DISCLOSE: the goroutine is a stand-in for autoAcceptLease -> provisionVM
// -> LimaManager.Provision. Production runLimactlWithTimeout waits on
// time.After, not n.ctx (proven separately by TestPoC_Q2Lima). Here we
// sleep after ctx.Done() to show Stop cannot proceed until that work ends.
func TestPoC_Q2Stop(t *testing.T) {
fmt.Println("PROVENANCE: xe=02e9641b0ac64437f57e09d63301f88cce61a802 file=poc/round4/overlay/q2_shutdown_test.go")
core.SetNetworkID("")
kp, err := core.GenerateKeyPair()
if err != nil {
t.Fatalf("keygen: %v", err)
}
mn := mocknet.New()
h, err := mn.GenPeer()
if err != nil {
t.Fatalf("peer: %v", err)
}
ms := store.NewMemStore()
l := core.NewLedger(ms, core.LedgerConfig{Difficulty: 0, TimestampWindow: 0})
ctx, cancel := context.WithCancel(context.Background())
n := &Node{
KeyPair: kp,
Host: h,
Ledger: l,
store: ms,
VoteMgr: core.NewVoteManager(ms, kp, l),
ctx: ctx,
cancel: cancel,
syncWait: func() {},
}
var mu sync.Mutex
var muHeld time.Duration
n.wg.Add(1)
go func() {
defer n.wg.Done()
mu.Lock()
select {
case <-n.ctx.Done():
t0 := time.Now()
time.Sleep(1500 * time.Millisecond)
muHeld = time.Since(t0)
case <-time.After(5 * time.Second):
}
mu.Unlock()
}()
done := make(chan struct{})
t0 := time.Now()
go func() {
n.Stop()
close(done)
}()
blocked := false
select {
case <-done:
fmt.Printf("KILL: Stop returned in %s while provision still held wg\n", time.Since(t0))
case <-time.After(800 * time.Millisecond):
blocked = true
fmt.Printf("Stop still blocked after 800ms (cancel already called, wg held)\n")
}
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatalf("Stop did not return after provision released")
}
stopDur := time.Since(t0)
fmt.Printf("Stop returned after %s muHeldAfterCancel=%s blocked=%v ctx.Err=%v\n", stopDur, muHeld, blocked, ctx.Err())
if blocked && stopDur >= 800*time.Millisecond && ctx.Err() != nil {
fmt.Println("PASS: Node.Stop blocked on wg.Wait past a bounded timeout while a provision-like goroutine ignored cancel()")
fmt.Println("VERDICT=CONFIRMED")
return
}
fmt.Println("KILL: Stop did not block on wg")
fmt.Println("VERDICT=KILLED")
t.Fatalf("Stop did not block blocked=%v stopDur=%s", blocked, stopDur)
}
PASS: Node.Stop blocked on wg.Wait past a bounded timeout while a provision-like goroutine ignored cancel()
Suggested fix
Honor n.ctx in lima timeouts, Stop() the signal Notify, and do not hold n.wg across Provision. Drain a second SIGINT so default terminate is restored.
Thread
Thanks for the report. We have confirmed the behaviour you describe from the code and it is being handled alongside related shutdown work; we will update https://bounty.xe.network/XE-0920 when a fix lands.