From 2939a82ecaa198daf421702f6d67f258706c9bf3 Mon Sep 17 00:00:00 2001 From: realrasengan Date: Mon, 30 Mar 2026 19:46:22 -0700 Subject: [PATCH] add 60s association timeout: kill mosh server if no client connects Prevents stale mosh servers from failed setups holding relay bridges indefinitely. Once a client connects, the 24h network timeout applies. --- server.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 1c9c8b6..3c4894f 100644 --- a/server.go +++ b/server.go @@ -258,6 +258,9 @@ func (s *Server) readIO(r io.Reader, out chan<- []byte) { } func (s *Server) mainLoopRW(rw io.Writer, resize func(cols, rows uint16), ioOutput <-chan []byte, userInput <-chan UserInstruction) { + // Wait for the first client datagram. If no client connects within + // 60 seconds, this was a failed setup — shut down to release resources. + deadline := time.After(associationTimeout) for { s.mu.Lock() addr := s.clientAddr @@ -268,6 +271,9 @@ func (s *Server) mainLoopRW(rw io.Writer, resize func(cols, rows uint16), ioOutp select { case <-s.done: return + case <-deadline: + close(s.done) + return case <-time.After(100 * time.Millisecond): } } @@ -357,7 +363,9 @@ func (s *Server) Close() { // snapshots the framebuffer, diffs against the last-acked state, and // sends CUP-based updates via the transport. func (s *Server) mainLoop(ptyOutput <-chan []byte, userInput <-chan UserInstruction) { - // Wait for client to connect before processing. + // Wait for the first client datagram. If no client connects within + // 60 seconds, this was a failed setup — shut down to release resources. + deadline := time.After(associationTimeout) for { s.mu.Lock() addr := s.clientAddr @@ -368,6 +376,9 @@ func (s *Server) mainLoop(ptyOutput <-chan []byte, userInput <-chan UserInstruct select { case <-s.done: return + case <-deadline: + close(s.done) + return case <-time.After(100 * time.Millisecond): } }