Fix(server,client): keep web terminal alive across heartbeat reconnects

The Go server answered TOKEN_HEARTBEAT with a 33-byte CMD_HEARTBEAT_ACK,
while the Linux and Android clients gate the ACK on 1 + sizeof(HeartbeatACK)
(1025 bytes) and silently drop anything shorter. Their application-level ACK
watchdog therefore never got fed and disconnected the main connection every
60s. Each re-login replaced the whole hub.Device, wiping the screen/shell
sub-conn bindings that ride their own TCP connections, so a web terminal kept
receiving shell output while every keystroke was dropped — the "terminal goes
unresponsive after a while" symptom — and the shell could no longer be torn
down (orphan bash + leaked sockets).

Clients now accept the 32-byte old ACK (Windows already did), and the server
sends the full 1024-byte struct. Hub.Register carries the sub-conn session
state across a re-login, and the disconnect cleanup is pinned to the
connection it belongs to so a stale OnDisconnect cannot tear down a
registration that has already replaced it. A shell sub-conn that can no
longer be bound now tells the viewer instead of leaving the pane waiting.

Verified on aa: RTT restored, zero "Heartbeat ACK timeout" lines, main-conn
port stable; Go build/vet/test pass. Server deploy to the Go host is still
pending, so un-upgraded clients will keep reconnecting until then.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 11:03:57 +01:00
1 parent 4aedca8ee0
commit 81f0d6c031
8 files changed
+412 -46

No files matched your search

+90 -11
View File
@@ -471,6 +471,32 @@ func (h *Hub) Register(d *Device, conn *connection.Context) {
}
d.conn = conn
h.mu.Lock()
// A reconnecting client logs in with a *fresh* Device struct, so every
// sub-conn-scoped field would come back zeroed even though the sub-conns
// themselves (screen / shell) ride their own TCP connections and normally
// survive a main-conn reconnect. Dropping them silently breaks live views:
// - terminal: SendToTerminal returns ErrDeviceOffline for every keystroke
// (handleTermInput ignores the error) while output keeps streaming the
// other way — the browser looks connected but typing does nothing, and
// CloseTerminalSession can no longer tear the shell down (orphan shells
// + leaked sockets);
// - screen: the cached resolution / last IDR are lost, so a viewer that
// joins before the next IDR has nothing to decode.
// The carried bindings may themselves be dead if the whole network path
// went away; the sub-conn read timeout then reaps them through
// UnbindScreenConn / UnbindTerminalConn, which clears the same fields.
// Carry the session state over. UnbindScreenConn / UnbindTerminalConn still
// clear it when a sub-conn really is gone, and a genuine offline closes
// both sessions via the *Pinned disconnect helpers, which no-op if a
// re-login has already taken ownership of this ID.
if prev, ok := h.devices[d.ID]; ok {
d.screenConn = prev.screenConn
d.screenWidth, d.screenHeight = prev.screenWidth, prev.screenHeight
d.lastKeyframe = prev.lastKeyframe
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
}
h.devices[d.ID] = d
info := deviceToInfo(d)
h.mu.Unlock()
@@ -482,19 +508,34 @@ func (h *Hub) Register(d *Device, conn *connection.Context) {
// Unregister removes a device by ID. No-op if not present.
// Subscribers are notified after the device is removed (only if it existed).
func (h *Hub) Unregister(id string) {
h.unregister(id, nil)
}
// UnregisterPinned is Unregister restricted to a device still pinned to main.
// The disconnect path MUST use this: the old main conn's OnDisconnect runs
// concurrently with a re-login, and deleting by ID alone would remove the
// device that just re-registered, leaving a live client invisible in the
// online list until it reconnects again.
func (h *Hub) UnregisterPinned(id string, main *connection.Context) bool {
return h.unregister(id, main)
}
func (h *Hub) unregister(id string, main *connection.Context) bool {
if id == "" {
return
return false
}
h.mu.Lock()
_, existed := h.devices[id]
d, existed := h.devices[id]
if !existed || (main != nil && d.conn != main) {
h.mu.Unlock()
return false
}
delete(h.devices, id)
h.mu.Unlock()
if !existed {
return
}
for _, s := range h.snapshotSubscribers() {
s.OnDeviceOffline(id)
}
return true
}
// ListDevices returns a fresh snapshot slice. The caller may mutate it freely;
@@ -594,11 +635,25 @@ func (h *Hub) PublishCursor(deviceID string, index byte, x, y int) {
// The actual sub-conn teardown is delegated to retireScreenConn, which is
// shared with BindScreenConn's replacement path.
func (h *Hub) CloseScreen(deviceID string) {
h.closeScreenPinned(deviceID, nil)
}
// CloseScreenPinned is CloseScreen restricted to a device that is still
// pinned to main. The main-conn teardown path uses it so a client that has
// already logged in again on a fresh connection is not stripped of the
// screen session Register just carried over to it. The ownership check and
// the detach happen under one hub lock, so a concurrent Register cannot slip
// in between.
func (h *Hub) CloseScreenPinned(deviceID string, main *connection.Context) bool {
return h.closeScreenPinned(deviceID, main)
}
func (h *Hub) closeScreenPinned(deviceID string, main *connection.Context) bool {
h.mu.Lock()
d, ok := h.devices[deviceID]
if !ok {
if !ok || (main != nil && d.conn != main) {
h.mu.Unlock()
return
return false
}
sc := d.screenConn
d.screenConn = nil
@@ -607,6 +662,7 @@ func (h *Hub) CloseScreen(deviceID string) {
d.lastKeyframe = nil
h.mu.Unlock()
h.retireScreenConn(sc)
return true
}
// PublishResolution announces a new (or first-ever) screen geometry for a
@@ -853,11 +909,22 @@ 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)
}
// 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)
}
func (h *Hub) closeTerminalPinned(deviceID string, main *connection.Context) bool {
h.mu.Lock()
d, ok := h.devices[deviceID]
if !ok {
if !ok || (main != nil && d.conn != main) {
h.mu.Unlock()
return
return false
}
tc := d.terminalConn
// hadSession guards against firing spurious OnTerminalClosed events
@@ -872,7 +939,7 @@ func (h *Hub) CloseTerminalSession(deviceID string) {
h.mu.Unlock()
if !hadSession {
return
return true
}
for _, s := range h.snapshotSubscribers() {
@@ -880,7 +947,7 @@ func (h *Hub) CloseTerminalSession(deviceID string) {
}
if tc == nil {
return
return true
}
h.terminalIndexMu.Lock()
delete(h.terminalIndex, tc)
@@ -896,6 +963,18 @@ func (h *Hub) CloseTerminalSession(deviceID string) {
time.Sleep(500 * time.Millisecond)
c.Close()
}(tc)
return true
}
// PublishTerminalClosed tells terminal viewers the session is over without
// touching hub session state. Used when the device opens a shell sub-conn that
// cannot be bound because the terminal request it answers no longer exists
// (the main conn reconnected in between); otherwise the browser pane would
// wait on term_ready forever.
func (h *Hub) PublishTerminalClosed(deviceID, reason string) {
for _, s := range h.snapshotSubscribers() {
s.OnTerminalClosed(deviceID, reason)
}
}
// PublishTerminalData fans out one chunk of shell output to subscribers.