From a4ff918953e548f3f812802fa889683eab5808d9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 22:03:13 +0000 Subject: [PATCH] Stop spins and races in door telnet, SSH pump, Fido lock, tn3270 - door: telnetClient.Read returns at once for an empty buffer; a failed answer to an option is returned by the next Read or Write rather than reading on for ever. - ssh: sshTerm.pump stops once closed when the channel reads nothing without error, instead of spinning. - ssh: makeHostKey removes the key file when Close fails, not only Write. - fido: flock retries EINTR; Lock retries a failed lock file briefly. - tn3270: negotiation and its 30s watchdog decide by one flag, so a negotiation ending as time runs out keeps its connection. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr --- internal/door/remote.go | 27 ++++++++++++++++++++---- internal/door/remote_test.go | 41 ++++++++++++++++++++++++++++++++++++ internal/fido/fido.go | 14 +++++++++--- internal/server/server.go | 17 +++++++++++---- internal/server/ssh.go | 21 ++++++++++++++---- internal/server/ssh_test.go | 21 ++++++++++++++++++ 6 files changed, 126 insertions(+), 15 deletions(-) diff --git a/internal/door/remote.go b/internal/door/remote.go index e6bc9f4..fd3ed3a 100644 --- a/internal/door/remote.go +++ b/internal/door/remote.go @@ -320,6 +320,7 @@ type telnetClient struct { c net.Conn in *Info wmu sync.Mutex + werr error // send's failure, for the next Read or Write (wmu) rbuf []byte // read from the connection st int // the parser's state cmd byte @@ -339,6 +340,9 @@ func (t *telnetClient) Close() error { return t.c.Close() } func (t *telnetClient) Write(p []byte) (int, error) { t.wmu.Lock() defer t.wmu.Unlock() + if t.werr != nil { + return 0, t.werr + } if bytes.IndexByte(p, tIAC) < 0 { return t.c.Write(p) } @@ -348,14 +352,23 @@ func (t *telnetClient) Write(p []byte) (int, error) { return len(p), nil } -// send writes telnet commands as they are, between Writes; an error is let -// go (the next Read or Write meets it). +// send writes telnet commands as they are, between Writes; a failure is +// kept for the next Read or Write to return. func (t *telnetClient) send(b ...byte) { t.wmu.Lock() - t.c.Write(b) + if _, err := t.c.Write(b); err != nil && t.werr == nil { + t.werr = err + } t.wmu.Unlock() } +// sendErr is send's failure, if there was one. +func (t *telnetClient) sendErr() error { + t.wmu.Lock() + defer t.wmu.Unlock() + return t.werr +} + // states const ( sData = iota @@ -366,8 +379,11 @@ const ( ) // Read is the server's data, telnet commands taken out and answered; it -// reads on until there is some data or an error. +// reads on until there is some data or an error (send's too). func (t *telnetClient) Read(p []byte) (int, error) { + if len(p) == 0 { + return 0, nil // nothing could be read into it: reading on would never end + } for { if cap(t.rbuf) < len(p) { t.rbuf = make([]byte, len(p)) @@ -419,6 +435,9 @@ func (t *telnetClient) Read(p []byte) (int, error) { if len(out) > 0 || err != nil { return len(out), err } + if err := t.sendErr(); err != nil { + return 0, err + } } } diff --git a/internal/door/remote_test.go b/internal/door/remote_test.go index 1ccc860..a598f3b 100644 --- a/internal/door/remote_test.go +++ b/internal/door/remote_test.go @@ -287,3 +287,44 @@ func TestDoorPartyDoor(t *testing.T) { } } } + +// optionConn is a connection that reads telnet options forever and +// fails every write. +type optionConn struct{ net.Conn } + +func (optionConn) Read(p []byte) (int, error) { + return copy(p, []byte{tIAC, tDO, 1}), nil // DO ECHO, and nothing else +} +func (optionConn) Write([]byte) (int, error) { return 0, io.ErrClosedPipe } + +// TestTelnetClientStops: a Read with no room returns at once, and one +// whose answers to options cannot be written ends with that error, not +// reading on forever. +func TestTelnetClientStops(t *testing.T) { + done := make(chan error, 1) + go func() { + c := newTelnetClient(optionConn{}, &Info{}) + if n, err := c.Read(nil); n != 0 || err != nil { + done <- fmt.Errorf("empty Read: %d, %v", n, err) + return + } + _, err := c.Read(make([]byte, 16)) + if err != io.ErrClosedPipe { + done <- fmt.Errorf("Read: %v, want the write's error", err) + return + } + if _, err := c.Write([]byte("x")); err != io.ErrClosedPipe { + done <- fmt.Errorf("Write: %v", err) + return + } + done <- nil + }() + select { + case err := <-done: + if err != nil { + t.Fatal(err) + } + case <-time.After(5 * time.Second): + t.Fatal("Read never returned") + } +} diff --git a/internal/fido/fido.go b/internal/fido/fido.go index cd27ceb..ccf7d2e 100644 --- a/internal/fido/fido.go +++ b/internal/fido/fido.go @@ -114,7 +114,11 @@ func (k *linkLock) flock(how int) bool { if err != nil { return true // no lock file to be had: the mutex will do } - if err := syscall.Flock(int(fh.Fd()), how); err != nil { + err = syscall.Flock(int(fh.Fd()), how) + for err == syscall.EINTR { + err = syscall.Flock(int(fh.Fd()), how) + } + if err != nil { fh.Close() return false } @@ -122,10 +126,14 @@ func (k *linkLock) flock(how int) bool { return true } -// Lock waits for the link. +// Lock waits for the link. Should the lock file fail to lock (as on a +// file system without locks) the mutex alone keeps it, as with no lock +// file; it is retried a while first, in case it is held but not waited for. func (k *linkLock) Lock() { k.mu.Lock() - k.flock(syscall.LOCK_EX) + for i := 0; i < 3 && !k.flock(syscall.LOCK_EX); i++ { + time.Sleep(100 * time.Millisecond) + } } // TryLock takes the link if it is free. diff --git a/internal/server/server.go b/internal/server/server.go index 9c66af5..2349d05 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -34,6 +34,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/racingmars/go3270" @@ -470,13 +471,21 @@ func (s *Server) tn3270(tc *telnet.Conn, l *Listener, node *bbs.Node) error { if err != nil { return err } - // go3270 negotiates without deadlines of its own: a client that - // stops answering is hung up - watchdog := time.AfterFunc(30*time.Second, func() { raw.Close() }) + // go3270 negotiates without deadlines of its own (and clears those + // set): a client that stops answering is hung up. Whichever ends + // first, negotiation or the wait, decides, so a negotiation ending + // as time runs out keeps its connection + var state atomic.Int32 // 0 negotiating, 1 negotiated, 2 timed out + watchdog := time.AfterFunc(30*time.Second, func() { + if state.CompareAndSwap(0, 2) { + raw.Close() + } + }) dev, err := go3270.NegotiateTelnet(raw) - if !watchdog.Stop() { + if !state.CompareAndSwap(0, 1) { return errors.New("tn3270: negotiation timed out") } + watchdog.Stop() if err != nil { return fmt.Errorf("tn3270: %w", err) } diff --git a/internal/server/ssh.go b/internal/server/ssh.go index e196993..682b03d 100644 --- a/internal/server/ssh.go +++ b/internal/server/ssh.go @@ -13,6 +13,7 @@ import ( "net" "os" "path/filepath" + "runtime" "strconv" "strings" "sync" @@ -84,12 +85,15 @@ func makeHostKey(path string) ([]byte, error) { if err != nil { return nil, err } - if _, err := f.Write(b); err != nil { - f.Close() - os.Remove(path) + _, err = f.Write(b) + if cerr := f.Close(); err == nil { + err = cerr + } + if err != nil { + os.Remove(path) // not left half written, to be read as the key return nil, err } - return b, f.Close() + return b, nil } // HostKeyInfo describes a host key file for bbs check: its type and @@ -348,6 +352,15 @@ func (t *sshTerm) pump() { close(t.in) return } + if n == 0 { + // nothing read and no error: stop if closed, never spin + select { + case <-t.done: + return + default: + runtime.Gosched() + } + } } } diff --git a/internal/server/ssh_test.go b/internal/server/ssh_test.go index cbe0f3e..5459b6a 100644 --- a/internal/server/ssh_test.go +++ b/internal/server/ssh_test.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" "time" @@ -370,3 +371,23 @@ func TestSSHLegacy(t *testing.T) { t.Fatalf("CBC with legacy: %v", err) } } + +// emptyReader reads nothing, without error, for ever. +type emptyReader struct{ io.Writer } + +func (emptyReader) Read([]byte) (int, error) { return 0, nil } + +// TestSSHPumpStops: a channel reading nothing without error does not keep +// the reading goroutine once the terminal is closed. +func TestSSHPumpStops(t *testing.T) { + before := runtime.NumGoroutine() + tm := newSSHTerm(emptyReader{io.Discard}, io.NopCloser(nil), sshPty{}) + tm.close() + deadline := time.Now().Add(5 * time.Second) + for runtime.NumGoroutine() > before { + if time.Now().After(deadline) { + t.Fatal("the reading goroutine is still running") + } + time.Sleep(10 * time.Millisecond) + } +}