mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
A new internal/leaktest, standard library only, so the proxy's and the daemon's tests use it too (depguard's proxy rule allows it, and the structure test holds it to tests and to importing nothing of devvm's): - leaktest.Main(m), in TestMain: once the tests pass, every goroutine's stack (runtime.Stack), a settle of 5 s, then a failure naming those still running, but the standard library's own that outlive every test (allowed: signal.Notify's loop, an HTTP client's idle connections); then Go 1.27's goroutineleak profile, which fails on any goroutine blocked for good. - leaktest.Check(t), first in a test: its own goroutines, followed by parent through those still running, and by which ran before it, never by ID (each thread takes a batch of IDs). Main runs in proxy, vmssh, daemon, desktop, ui, vm, backend/lima, platform, cli and gitprotect (main_test.go, or the TestMain they had). Check runs in TestLinksEndStuckRelays (replacing its NumGoroutine count), TestForwarder, the desktop relay's serve helper, TestRunChild, TestPinWaits, and the proxy's new close tests. The structure test's goroutines rule (internal/structure/goroutines_test.go): goroutineSites lists every function outside tests that starts a goroutine, with a go statement, a wait group's Go, or time's or context's AfterFunc, function literals in package-level vars included, as "file: Func", with its waiter, or "detached: " and why nothing waits. A new one fails until listed, a listed one that starts none fails, and a package that starts one fails until its tests call leaktest.Main. Leaks it found, each fixed with a test: - The proxy's close left its connections running: http.Server's Close neither closes a connection a handler took (a CONNECT's tunnel, a WebSocket upgrade ReverseProxy takes) nor waits for a handler. conns (conns.go) holds every connection the listener accepted (track) and counts every handler (handle); serve calls closeConns once Serve returns, which closes them, closes any accepted after, and waits up to 5 s for the handlers. splice also ends when its client's connection can't be read (closed, reset), rather than half-closing the far end and waiting on it; a failed write to the server (one that answered early and closed) still lets its answer through. Tests: TestCloseConns (a tunnel and an upgrade), TestConnectAfterClose, TestSpliceEndsWhenItsClientCloses, TestSpliceKeepsAnEarlyAnswer; the tests' listen closes as the proxy does, and the fake upstream closes its target once its client's end comes. - platform's runChild left its signal-passing goroutine blocked on a channel signal.Stop never closes; it now closes it and waits (TestRunChild). - platform's WaitExit and WatchFolders blocked in poll(2) and read(2), which closing their fd from the stop doesn't wake on every kernel (Debian's didn't), and a retried call could land on a reused fd. They now wait in Go's poller (pollable), which the stop wakes (TestPinWaits, with a wait stopped before its process ends). Docs: Contributing's tests page (Goroutine leaks), the structure page (the goroutines rule, goroutineSites, the layers, the packages), the Go lint rules' depguard line, the proxy's shutdown in How it works' processes page and the proxy's README, and AGENTS.md. Coverage baselines: internal/leaktest's line on both systems, netpolicy's raise on macOS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
package proxy
|
|
|
|
// Tunnels: a CONNECT the proxy carries itself (to a host the policy allows, or to one of the VM's
|
|
// links) and each connection the relay passes on are spliced, their bytes passed as they are both
|
|
// ways (splice), the far end's from the start; a CONNECT's connection is first taken from the HTTP
|
|
// server (hijack).
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
// hijack takes a CONNECT request's connection from the HTTP server and tells the client its tunnel
|
|
// is open. Bytes the client sent after the request, which the server may already have read, are
|
|
// the connection's first.
|
|
func hijack(w http.ResponseWriter) (bufConn, error) {
|
|
c, buf, err := http.NewResponseController(w).Hijack()
|
|
if err != nil {
|
|
return bufConn{}, err
|
|
}
|
|
if _, err := c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")); err != nil {
|
|
_ = c.Close()
|
|
return bufConn{}, err
|
|
}
|
|
return bufConn{c, buf.Reader}, nil
|
|
}
|
|
|
|
// splice passes client's bytes to server and server's to client, as they are. The end of what
|
|
// client sends ends what server reads (closeWrite), and server can still answer. Once server's
|
|
// bytes end, or client can't be written to (it went away), or client can't be read from (it was
|
|
// reset, or closed as the proxy closes: conns.close), splice closes both connections and returns.
|
|
// A server that can't be written to, as it answered early and closed, still has its answer passed
|
|
// to the end. Server's bytes pass from the start; client's only once before, when there's one, has
|
|
// returned true (it may read client's first bytes and pass them on itself), and when it returns
|
|
// false, none do, and both connections close.
|
|
func splice(client, server net.Conn, before func() bool) {
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
if before != nil && !before() {
|
|
_ = client.Close()
|
|
_ = server.Close()
|
|
return
|
|
}
|
|
// Only the client's own failure ends both: a server that answers early and closes while
|
|
// the client still sends (a 413, a refused login) fails the writes, and its answer still
|
|
// passes.
|
|
rc := &readErr{r: client}
|
|
_, _ = io.Copy(server, rc)
|
|
if rc.err != nil && !errors.Is(rc.err, io.EOF) {
|
|
_ = client.Close()
|
|
_ = server.Close()
|
|
return
|
|
}
|
|
closeWrite(server)
|
|
}()
|
|
_, _ = io.Copy(client, server)
|
|
_ = client.Close()
|
|
_ = server.Close()
|
|
<-done
|
|
}
|
|
|
|
// readErr is r, keeping the error its last read returned: what splice tells a client that failed
|
|
// from a server it couldn't write to.
|
|
type readErr struct {
|
|
r io.Reader
|
|
err error
|
|
}
|
|
|
|
func (r *readErr) Read(b []byte) (int, error) {
|
|
n, err := r.r.Read(b)
|
|
r.err = err
|
|
return n, err
|
|
}
|
|
|
|
// closeWrite closes c's writing side, so the other end reads the end of what c sent, or closes c
|
|
// when it can't.
|
|
func closeWrite(c net.Conn) {
|
|
if cw, ok := c.(interface{ CloseWrite() error }); ok {
|
|
_ = cw.CloseWrite()
|
|
return
|
|
}
|
|
_ = c.Close()
|
|
}
|
|
|
|
// bufConn is a connection whose first bytes may already sit in a bufio.Reader: a hijacked one's in
|
|
// the HTTP server's, an upstream tunnel's in what read the upstream's answer.
|
|
type bufConn struct {
|
|
net.Conn
|
|
r *bufio.Reader
|
|
}
|
|
|
|
func (c bufConn) Read(b []byte) (int, error) { return c.r.Read(b) }
|
|
|
|
// CloseWrite closes the writing side of the connection underneath, when it has one.
|
|
func (c bufConn) CloseWrite() error {
|
|
if t, ok := c.Conn.(interface{ CloseWrite() error }); ok {
|
|
return t.CloseWrite()
|
|
}
|
|
return nil
|
|
}
|