mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Timeouts, graces and closes are tested in testing/synctest bubbles on the
bubble's fake clock, to the moment, instead of sleeping and polling on the
real one, with stand-ins in memory behind seams that are fields. Each kind
keeps one real-process test. No behaviour change: each seam's production
value is what the code did before.
Seams:
- vmssh: sshRelay{grace, start} carries a relay (pipeSSH's body, as
pipeSSHFor was); start is execSSH in production.
- vmssh: Links.Listen (listenSocket unless set), for the links' sockets.
- proxy: egress.connect, the dialer for allowed far ends (a net.Dialer
with its 15 s timeout, set in newServer).
- daemon: Proc.Alive (Alive unless set).
- proxy: httpServer, the proxy's http.Server as run serves it, which the
tests' memServe shares.
New test-only package internal/memnet (standard library only): Pipe, whose
connections half-close, buffer 64 KiB and have deadlines, and Network,
listeners by address with Listen, Dial, DialContext and SetDeadline. The
structure test keeps it for tests and a leaf, depguard keeps it standard
library only, and its AfterFuncs are in goroutineSites.
Bubble tests:
- relay: TestRelayGrace (silent and talking, closed and half closed),
TestRelayGraceSlowClient, TestRelayGraceWriteError,
TestRelayCancelEndsBlockedWrite, TestGraceTimer (exact grace).
- forwarder: TestForwarder, TestForwarderAliveOnEnd, TestForwarderOnly,
TestForwarderGap (reports exactly Gap apart), TestForwarderGivesUpSilently,
TestForwarderClosesConnections (now also its stuck relay's ssh and slot).
- links: TestLinks, TestLinksSessionLost, TestLinksCap,
TestLinksEndStuckRelays (memSSH; relays end at the next sync on removal,
master replacement and target change), TestLinksList.
- proxy: TestTunnelHelloDeadline (EOF at exactly helloTimeout),
TestTunnelHelloStall (and a hello whole a moment before the deadline),
TestTunnelAddressSplitHello, TestConnectLimit, TestCloseConns,
TestCloseConnsWaitsBounded (close gives up at exactly closeWait),
TestSpliceEnds, TestSpliceEndsWhenItsClientCloses.
- secrets: TestSecretLookupFailuresNeverCached (hung lookups cut at
lookupTimeout, cancelled, failed and empty ones never cached; a request's
end doesn't cancel a lookup; the TTL to the moment).
- daemon: TestProcStopWaitsForTheEnd, TestProcStopKillsOnlyItsOwn,
TestProcStopKillsAfterWait (fakeProc), TestFollowLifeline.
- TestCursorReplyScans uses httptest.NewTestServer.
Kept on real processes and the real clock: TestPipeSSHGrace,
TestPipeSSHGraceSlowClient, TestPipeSSHGraceWriteError,
TestForwarderEndsStuckRelay, TestLinksBurst, TestSpliceKeepsAnEarlyAnswer,
TestProcStop.
Docs: Contributing's Tests has Time and lifecycles; structure.md, _index.md
and AGENTS.md name memnet.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
208 lines
6.4 KiB
Go
208 lines
6.4 KiB
Go
package vmssh
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"os/exec"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// relayGrace is how long a relay goes on once its client has closed its
|
|
// side with nothing coming from the VM (sshRelay.carry).
|
|
const relayGrace = 30 * time.Second
|
|
|
|
// pipeSSH carries conn through ssh run with args (-W), until either side
|
|
// ends, then closes conn; a VM that never ends its side is cut off
|
|
// relayGrace after the client has (sshRelay.carry).
|
|
func pipeSSH(ctx context.Context, conn net.Conn, args []string) {
|
|
sshRelay{grace: relayGrace, start: execSSH}.carry(ctx, conn, args)
|
|
}
|
|
|
|
// sshRelay carries a connection through an ssh -W (carry).
|
|
type sshRelay struct {
|
|
// grace is how long the VM may send nothing once the client's side
|
|
// has closed (carry).
|
|
grace time.Duration
|
|
// start starts ssh with args, killed once ctx ends: execSSH, or a
|
|
// test's stand-in.
|
|
start func(ctx context.Context, args []string) (sshPipes, error)
|
|
}
|
|
|
|
// sshPipes is a started ssh's input and output, and how to wait for it
|
|
// to end.
|
|
type sshPipes struct {
|
|
in io.WriteCloser
|
|
out io.Reader
|
|
wait func() error
|
|
}
|
|
|
|
// execSSH starts ssh with args, which ctx's end kills.
|
|
func execSSH(ctx context.Context, args []string) (sshPipes, error) {
|
|
cmd := exec.CommandContext(ctx, "ssh", args...) //nolint:gosec // devvm's own arguments
|
|
in, err := cmd.StdinPipe()
|
|
if err != nil {
|
|
return sshPipes{}, err
|
|
}
|
|
out, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return sshPipes{}, err
|
|
}
|
|
if err := cmd.Start(); err != nil {
|
|
return sshPipes{}, err
|
|
}
|
|
return sshPipes{in: in, out: out, wait: cmd.Wait}, nil
|
|
}
|
|
|
|
// carry carries conn through ssh run with args (-W), until either side
|
|
// ends, then closes conn. The ssh ends only when the VM's sshd ends its
|
|
// channel, so a VM that ignored the client's EOF would keep it, and its
|
|
// owner's relay slot, for as long as the VM liked. Once the client's side
|
|
// has closed, the ssh is killed when the VM has sent nothing for the
|
|
// grace, so a client that closes for writing alone still gets an answer
|
|
// that keeps coming, as long as the VM sends something at least every
|
|
// grace, its first byte included; once the client can take nothing more,
|
|
// it's killed when it hasn't exited within the grace. The grace doesn't
|
|
// run while a write to the client waits for it, so a slow client is
|
|
// never cut off from what the VM has sent it, but the owner's context
|
|
// (ctx) closes the connection when it ends, which ends such a write too.
|
|
func (r sshRelay) carry(ctx context.Context, conn net.Conn, args []string) {
|
|
defer func() { _ = conn.Close() }()
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
stop := context.AfterFunc(ctx, func() { _ = conn.Close() })
|
|
defer stop()
|
|
ssh, err := r.start(ctx, args)
|
|
if err != nil {
|
|
return
|
|
}
|
|
idle := newGraceTimer(r.grace, cancel)
|
|
defer idle.stop()
|
|
go func() {
|
|
_, _ = io.Copy(ssh.in, conn)
|
|
_ = ssh.in.Close()
|
|
idle.arm()
|
|
}()
|
|
_, _ = io.Copy(&graceWriter{w: conn, idle: idle}, ssh.out)
|
|
_ = conn.Close()
|
|
idle.arm()
|
|
_ = ssh.wait()
|
|
}
|
|
|
|
// graceTimer cancels a relay once its grace runs out. It runs only once
|
|
// armed, as the client's side has closed, or the copy to the client has
|
|
// ended (a write that failed, or the VM's EOF), and starts again with
|
|
// each write to the client, which it waits out (write): the grace is time
|
|
// the VM sends nothing, never time a slow client takes. Once stopped, as the
|
|
// relay has ended, nothing starts it again, so the client's goroutine,
|
|
// which can outlive the relay, never does.
|
|
type graceTimer struct {
|
|
grace time.Duration
|
|
t *time.Timer
|
|
|
|
mu sync.Mutex
|
|
armed, writing, ended bool
|
|
}
|
|
|
|
// newGraceTimer is a graceTimer for cancel, not yet armed.
|
|
func newGraceTimer(grace time.Duration, cancel context.CancelFunc) *graceTimer {
|
|
g := &graceTimer{grace: grace, t: time.AfterFunc(grace, cancel)}
|
|
g.t.Stop()
|
|
return g
|
|
}
|
|
|
|
// arm starts the grace, unless a write is waiting, which starts it once
|
|
// done.
|
|
func (g *graceTimer) arm() {
|
|
g.mu.Lock()
|
|
defer g.mu.Unlock()
|
|
g.armed = true
|
|
g.restart()
|
|
}
|
|
|
|
// write holds the grace while a write to the client waits, then starts it
|
|
// again (once armed): the VM has just sent something.
|
|
func (g *graceTimer) write(w func() (int, error)) (int, error) {
|
|
g.mu.Lock()
|
|
g.writing = true
|
|
g.t.Stop()
|
|
g.mu.Unlock()
|
|
n, err := w()
|
|
g.mu.Lock()
|
|
defer g.mu.Unlock()
|
|
g.writing = false
|
|
g.restart()
|
|
return n, err
|
|
}
|
|
|
|
// restart starts the grace again when it's armed, no write waits and the
|
|
// relay hasn't ended. Its caller holds mu.
|
|
func (g *graceTimer) restart() {
|
|
if g.armed && !g.writing && !g.ended {
|
|
g.t.Reset(g.grace)
|
|
}
|
|
}
|
|
|
|
// stop stops the timer for good.
|
|
func (g *graceTimer) stop() {
|
|
g.mu.Lock()
|
|
defer g.mu.Unlock()
|
|
g.ended = true
|
|
g.t.Stop()
|
|
}
|
|
|
|
// graceWriter is the client's side of a relay, each write to which holds
|
|
// the grace while it waits and starts it again after (graceTimer.write).
|
|
type graceWriter struct {
|
|
w io.Writer
|
|
idle *graceTimer
|
|
}
|
|
|
|
func (g *graceWriter) Write(p []byte) (int, error) {
|
|
return g.idle.write(func() (int, error) { return g.w.Write(p) })
|
|
}
|
|
|
|
// relayConns is the connections a forward, a link or a link's target
|
|
// master carries that are open, each with what ends its relay's context,
|
|
// and whether they were cut, after which it takes no more. Its owner's
|
|
// mutex guards it.
|
|
//
|
|
// A relay's ssh (-W) waits for the VM's sshd to end its channel, so a VM
|
|
// that ignored EOF would keep that ssh, and one of its owner's relay
|
|
// slots, after the connection closed. Each relay runs with a context of
|
|
// its own instead, which cut cancels, so exec.CommandContext kills its
|
|
// ssh, and a relay that ends by itself releases its context (done).
|
|
type relayConns struct {
|
|
open map[net.Conn]context.CancelFunc
|
|
cut bool
|
|
}
|
|
|
|
// add tracks conn and its relay's cancel, and reports false, tracking
|
|
// neither, once they were cut: its caller then cancels and closes it.
|
|
func (r *relayConns) add(conn net.Conn, cancel context.CancelFunc) bool {
|
|
if r.cut {
|
|
return false
|
|
}
|
|
if r.open == nil {
|
|
r.open = map[net.Conn]context.CancelFunc{}
|
|
}
|
|
r.open[conn] = cancel
|
|
return true
|
|
}
|
|
|
|
// done stops tracking a connection whose relay ended.
|
|
func (r *relayConns) done(conn net.Conn) { delete(r.open, conn) }
|
|
|
|
// cutAll closes each connection and cancels its relay's context, so its
|
|
// ssh is killed even when the VM never ends its channel, and refuses any
|
|
// added after.
|
|
func (r *relayConns) cutAll() {
|
|
r.cut = true
|
|
for c, cancel := range r.open {
|
|
_ = c.Close()
|
|
cancel()
|
|
}
|
|
clear(r.open)
|
|
}
|