Feature(Go): terminal_close, idle sweep and legacy-client guard
Add the terminal_close tool so a caller can release a shell session explicitly instead of waiting for it to age out. The tool is idempotent for the session it names, rejects a session_id that does not match the live session with -32002, and runs without the per-device lock so it can interrupt a terminal_exec call that is still polling on the same device. Give hub leases a generation counter: the generation is bumped when a pending lease is created and carried across a device re-login. MCP-side closes pass the generation, so a stale session can no longer tear down a shell a human operator opened after the device reconnected. The reaper runs on a 30 s ticker (shortened when TermIdle is smaller), skips sessions that are still opening, and reclaims idle sessions as well as commands overdue past the one-hour busy cap. The pre-bind terminal-close path in cmd no longer closes unconditionally; it touches only a pending lease, because an unbound sub-connection carries no lease identity and may be a stale orphan. Covered by unit tests for close semantics, lock exemption, reaper lifecycle and lease-generation ownership, plus a live run against a Debug ghost: close before and during a command, reopen, idle reap and mismatched-id rejection all behave as specified. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
599c891c00
commit
808747f9d2
10 files changed
+633
-52
No files matched your search
@@ -239,6 +239,7 @@ func TestSessionIdleSweep(t *testing.T) {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
sess.mu.Lock()
|
||||
sess.ready = true
|
||||
sess.lastUsed = time.Now().Add(-time.Hour)
|
||||
sess.mu.Unlock()
|
||||
|
||||
@@ -408,3 +409,137 @@ func TestSentinelSplitAcrossChunks(t *testing.T) {
|
||||
t.Fatalf("split sentinel: done=%v code=%d", done, code)
|
||||
}
|
||||
}
|
||||
|
||||
// A quiet command still owns the device's shell lease: the reaper must not
|
||||
// reclaim a session just because it printed nothing for a while.
|
||||
func TestSweepKeepsRunningCommand(t *testing.T) {
|
||||
tbl := newSessionTable(nil)
|
||||
sess := newTermSession("1001")
|
||||
sess.ready = true
|
||||
sess.lastUsed = sess.lastUsed.Add(-time.Hour)
|
||||
sess.pending = &pendingCmd{start: 0, busySince: time.Now()}
|
||||
tbl.byDevice["1001"] = sess
|
||||
|
||||
if n := tbl.sweepIdle(time.Now(), time.Minute); n != 0 {
|
||||
t.Fatalf("swept = %d, want 0 while the command runs", n)
|
||||
}
|
||||
if tbl.get("1001") != sess {
|
||||
t.Fatal("running session was removed")
|
||||
}
|
||||
}
|
||||
|
||||
// Once the shell is gone, only the undelivered tail keeps the session alive;
|
||||
// a model that never polls must not pin the slot indefinitely.
|
||||
func TestSweepReapsClosedDrainingSession(t *testing.T) {
|
||||
tbl := newSessionTable(nil)
|
||||
sess := newTermSession("1001")
|
||||
sess.ready = true
|
||||
sess.closed = true
|
||||
sess.pending = &pendingCmd{busySince: time.Now()}
|
||||
sess.lastUsed = sess.lastUsed.Add(-time.Hour)
|
||||
tbl.byDevice["1001"] = sess
|
||||
|
||||
if n := tbl.sweepIdle(time.Now(), time.Minute); n != 1 {
|
||||
t.Fatalf("swept = %d, want 1", n)
|
||||
}
|
||||
if tbl.get("1001") != nil {
|
||||
t.Fatal("draining session not removed")
|
||||
}
|
||||
}
|
||||
|
||||
// The reaper is wired to a ticker in New: an idle session is reclaimed without
|
||||
// any further tool call.
|
||||
func TestSweepTickerReclaimsIdleSession(t *testing.T) {
|
||||
h := hub.New()
|
||||
h.SetSender(func(*connection.Context, []byte) error { return nil })
|
||||
h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.1:54321"})
|
||||
s := New(Config{Token: testToken, Hub: h, TermIdle: 100 * time.Millisecond})
|
||||
t.Cleanup(s.Close)
|
||||
|
||||
if _, err := s.sessions.open("1001"); err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
s.OnTerminalReady("1001", true)
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for s.sessions.get("1001") != nil {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("idle session was not reaped by the ticker")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if h.HasTerminalSession("1001") {
|
||||
t.Fatal("reaped session still holds the hub lease")
|
||||
}
|
||||
}
|
||||
|
||||
// A command that outlives the busy cap is reclaimed even with no output and no
|
||||
// poller, so a hung shell cannot hold the device slot forever.
|
||||
func TestSweepReapsOverdueCommand(t *testing.T) {
|
||||
tbl := newSessionTable(nil)
|
||||
sess := newTermSession("1001")
|
||||
sess.ready = true
|
||||
sess.lastUsed = sess.lastUsed.Add(-time.Minute)
|
||||
sess.pending = &pendingCmd{start: 0, busySince: time.Now().Add(-termBusyMax - time.Second)}
|
||||
tbl.byDevice["1001"] = sess
|
||||
|
||||
if n := tbl.sweepIdle(time.Now(), time.Minute); n != 1 {
|
||||
t.Fatalf("swept = %d, want 1 for an overdue command", n)
|
||||
}
|
||||
if tbl.get("1001") != nil {
|
||||
t.Fatal("overdue session not removed")
|
||||
}
|
||||
}
|
||||
|
||||
// A session whose shell never bound has a caller still waiting on it; the
|
||||
// reaper must leave it to that caller's own release path.
|
||||
func TestSweepKeepsNotReadySession(t *testing.T) {
|
||||
tbl := newSessionTable(nil)
|
||||
sess := newTermSession("1001")
|
||||
sess.lastUsed = sess.lastUsed.Add(-time.Hour)
|
||||
tbl.byDevice["1001"] = sess
|
||||
|
||||
if n := tbl.sweepIdle(time.Now(), time.Minute); n != 0 {
|
||||
t.Fatalf("swept = %d, want 0 for a session still opening", n)
|
||||
}
|
||||
if tbl.get("1001") != sess {
|
||||
t.Fatal("not-ready session was removed")
|
||||
}
|
||||
}
|
||||
|
||||
// A device re-login drops a pending lease without emitting OnTerminalClosed.
|
||||
// If a human then takes the freed slot, a late close from the MCP session that
|
||||
// lost it must not tear the human's shell down (design §6.2: never pre-empt).
|
||||
func TestCloseLeavesRenewedLeaseAlone(t *testing.T) {
|
||||
s, h := terminalTestServer(t)
|
||||
sess, err := s.sessions.open("1001")
|
||||
if err != nil {
|
||||
t.Fatalf("open: %v", err)
|
||||
}
|
||||
// Register carries the terminal generation over, exactly like a real
|
||||
// re-login racing the old connection's disconnect.
|
||||
h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.1:54321"})
|
||||
if err := h.OpenTerminalSession("1001"); err != nil {
|
||||
t.Fatalf("human open: %v", err)
|
||||
}
|
||||
|
||||
if !s.sessions.closeSessionIf("1001", sess) {
|
||||
t.Fatal("close did not remove the stale session")
|
||||
}
|
||||
if !h.HasTerminalSession("1001") {
|
||||
t.Fatal("close tore down a lease the stale session no longer owned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseStopsReaper(t *testing.T) {
|
||||
s, _ := terminalTestServer(t)
|
||||
s.Close()
|
||||
select {
|
||||
case <-s.reaperDone:
|
||||
default:
|
||||
t.Fatal("Close returned before the reaper stopped")
|
||||
}
|
||||
s.Close() // idempotent; the Cleanup will call it a third time
|
||||
}
|
||||
Reference in new issue
Block a user