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:
yuanyuanxianganddeepseek-flash committed 2026-11-12 21:53:54 +01:00
1 parent 599c891c00
commit 808747f9d2
10 files changed
+633 -52

No files matched your search

+45 -11
View File
@@ -94,9 +94,13 @@ type Device struct {
// and after teardown.
// terminalIsPTY: distinguishes Linux/macOS/ConPTY (true) from the legacy
// Windows cmd-pipe path. PTY mode supports resize; cmd-pipe ignores it.
// terminalGen: identifies one lease. Every successful open bumps it, so a
// viewer that lost the lease (a re-login drops a pending request, then
// someone else opens) cannot close the new owner's shell.
terminalPending bool
terminalConn *connection.Context
terminalIsPTY bool
terminalGen uint64
}
// ScreenCache is a read-only snapshot of a device's last-seen screen state,
@@ -496,6 +500,7 @@ func (h *Hub) Register(d *Device, conn *connection.Context) {
d.cursorSeen, d.lastCursorIndex = prev.cursorSeen, prev.lastCursorIndex
d.lastCursorX, d.lastCursorY, d.cursorPubAt = prev.lastCursorX, prev.lastCursorY, prev.cursorPubAt
d.terminalConn, d.terminalIsPTY = prev.terminalConn, prev.terminalIsPTY
d.terminalGen = prev.terminalGen
}
h.devices[d.ID] = d
info := deviceToInfo(d)
@@ -754,37 +759,57 @@ var ErrTerminalBusy = errors.New("terminal already open by another viewer")
// returns ErrTerminalBusy. Mirrors C++ CWebService::HandleTermOpen
// (server/2015Remote/WebService.cpp:1838).
func (h *Hub) OpenTerminalSession(deviceID string) error {
_, err := h.openTerminalSession(deviceID)
return err
}
// OpenTerminalSessionLease is OpenTerminalSession that also returns the lease
// generation. A caller that later closes via CloseTerminalSessionLease can
// only tear down the lease it opened: if the session was dropped (device
// re-login) and taken over by another viewer, the generation no longer matches
// and the close is a no-op.
func (h *Hub) OpenTerminalSessionLease(deviceID string) (uint64, error) {
return h.openTerminalSession(deviceID)
}
func (h *Hub) openTerminalSession(deviceID string) (uint64, error) {
if deviceID == "" {
return ErrDeviceOffline
return 0, ErrDeviceOffline
}
h.mu.Lock()
d, ok := h.devices[deviceID]
if !ok || d.conn == nil {
h.mu.Unlock()
return ErrDeviceOffline
return 0, ErrDeviceOffline
}
if d.terminalPending || d.terminalConn != nil {
h.mu.Unlock()
return ErrTerminalBusy
return 0, ErrTerminalBusy
}
d.terminalPending = true
// The generation is bumped with the pending flag, under the same lock: a
// later open can only start once this lease is gone, and it bumps again, so
// an older generation can never match a newer owner's lease.
d.terminalGen++
gen := d.terminalGen
mainConn := d.conn
h.mu.Unlock()
if h.sender == nil {
// Roll back so a retry isn't permanently blocked.
// Roll back so a retry isn't permanently blocked. The generation stays
// bumped; it is only an identity, not a resource.
h.mu.Lock()
d.terminalPending = false
h.mu.Unlock()
return ErrNoSender
return 0, ErrNoSender
}
if err := h.sender(mainConn, []byte{protocol.CommandShell}); err != nil {
h.mu.Lock()
d.terminalPending = false
h.mu.Unlock()
return err
return 0, err
}
return nil
return gen, nil
}
// IsTerminalPending tells the TCP layer whether the next-arriving shell
@@ -919,20 +944,29 @@ func (h *Hub) TerminalIsPTY(id string) bool {
// send COMMAND_BYE, then close after a short grace period so the client's
// IOCPClient reconnect logic doesn't fire.
func (h *Hub) CloseTerminalSession(deviceID string) {
h.closeTerminalPinned(deviceID, nil)
h.closeTerminalPinned(deviceID, nil, 0)
}
// CloseTerminalSessionLease tears down the session only if its lease
// generation still matches gen. False means the lease was already gone (or was
// renewed by another open) and nothing was touched.
func (h *Hub) CloseTerminalSessionLease(deviceID string, gen uint64) bool {
return h.closeTerminalPinned(deviceID, nil, gen)
}
// CloseTerminalSessionPinned is CloseTerminalSession restricted to a device
// still pinned to main; see CloseScreenPinned for why the disconnect path
// needs the ownership check.
func (h *Hub) CloseTerminalSessionPinned(deviceID string, main *connection.Context) bool {
return h.closeTerminalPinned(deviceID, main)
return h.closeTerminalPinned(deviceID, main, 0)
}
func (h *Hub) closeTerminalPinned(deviceID string, main *connection.Context) bool {
// closeTerminalPinned tears down the session. gen==0 closes whatever lease the
// device holds; a non-zero gen closes only that lease.
func (h *Hub) closeTerminalPinned(deviceID string, main *connection.Context, gen uint64) bool {
h.mu.Lock()
d, ok := h.devices[deviceID]
if !ok || (main != nil && d.conn != main) {
if !ok || (main != nil && d.conn != main) || (gen != 0 && d.terminalGen != gen) {
h.mu.Unlock()
return false
}