gateway: a handed-off gateway waits for a connection still answering its request, not only new and idle ones (#3451)
68ab4c06dmade the end of a handed-off gateway wait for idle kept-alive connections, so their next request is answered here instead of cut by Shutdown. Its test went red on main's ubuntu CI at56602426d(run 40682193794): `"" unexpected EOF`. net/http notes a connection idle only once the goroutine that wrote its reply returns from the write, and the agent can read that reply and the end can begin first. Then the wait saw the connection as neither new nor idle, Shutdown began, and closed the connection as idle once net/http noted it, cutting the agent's next request: the same #3451 cut, on a busy machine. unreadConns now keeps a connection from StateActive until it is closed or hijacked (kept), and the wait counts those too. A connection never used again still ends the wait at unreadWait (5s), as before. TestHandedOffGatewayAnswersAConnectionStillAnswering holds the gateway's write of the chunked reply's end for a second (lagConn), and hands off while it sits there, for both a handover and `make dev`. Run at this tree (origin/mainf835bd713+ this): - Reproduced first: a 300ms sleep injected before note's StateIdle gave CI's error in the existing kept-alive test, both subtests; green with the fix under the same injection. - The new test on the old handover.go: red, `"" unexpected EOF`, both subtests. A first version lagged the first write, which wasn't the reply's end, and passed on the old code; it now lags the end. - mote: TestHandedOffGateway* -race -count=3 ok; go vet ./internal/gateway for darwin, GOOS=linux, GOOS=windows ok; go test ./internal/gateway ok (481s); ./internal/gui red only on TestMainWindowCorners/macOS_26_SDK, #3949 (mote's macOS 27). - WSL magpie-desktop: TestHandedOffGateway* -count=3 ok. - gofmt -l clean. Not run: the full ./... suite; a real update restart under a busy agent.
This commit is contained in:
@@ -206,7 +206,9 @@ func (s *Server) drainFor() time.Duration {
|
||||
var unreadWait = 5 * time.Second
|
||||
|
||||
// unreadConns are the connections the gateway took whose next request it
|
||||
// hasn't read yet: new ones, and kept-alive ones idle between requests.
|
||||
// hasn't read yet: new ones, and kept-alive ones idle between requests or
|
||||
// still answering one (net/http notes one idle after its reply is out, so
|
||||
// an agent can have read the reply and sent its next request first).
|
||||
// Shutdown closes such a connection unanswered, and a request the agent
|
||||
// sent on it just then is cut ("connection reset", #3451), so a
|
||||
// handover's end waits for them first: each one's next request is
|
||||
@@ -215,29 +217,30 @@ var unreadWait = 5 * time.Second
|
||||
type unreadConns struct {
|
||||
mu sync.Mutex
|
||||
conns map[net.Conn]bool
|
||||
idle map[net.Conn]bool
|
||||
kept map[net.Conn]bool // answering a request, or idle after one
|
||||
// stopped: the server takes none from here on, its Serve returned
|
||||
stopped bool
|
||||
}
|
||||
|
||||
// note is the server's ConnState: a connection is unread from StateNew,
|
||||
// and from StateIdle, to its next state, whichever it is.
|
||||
// note is the server's ConnState: a connection is unread from StateNew
|
||||
// to its next state, and kept from StateActive until it is closed or
|
||||
// hijacked.
|
||||
func (u *unreadConns) note(c net.Conn, state http.ConnState) {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
delete(u.conns, c)
|
||||
delete(u.idle, c)
|
||||
delete(u.kept, c)
|
||||
switch state {
|
||||
case http.StateNew:
|
||||
if u.conns == nil {
|
||||
u.conns = map[net.Conn]bool{}
|
||||
}
|
||||
u.conns[c] = true
|
||||
case http.StateIdle:
|
||||
if u.idle == nil {
|
||||
u.idle = map[net.Conn]bool{}
|
||||
case http.StateActive, http.StateIdle:
|
||||
if u.kept == nil {
|
||||
u.kept = map[net.Conn]bool{}
|
||||
}
|
||||
u.idle[c] = true
|
||||
u.kept[c] = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +261,7 @@ func (u *unreadConns) stop() {
|
||||
func (u *unreadConns) wait(ctx context.Context) {
|
||||
for deadline := time.Now().Add(unreadWait); ; time.Sleep(10 * time.Millisecond) {
|
||||
u.mu.Lock()
|
||||
done := u.stopped && len(u.conns) == 0 && len(u.idle) == 0
|
||||
done := u.stopped && len(u.conns) == 0 && len(u.kept) == 0
|
||||
u.mu.Unlock()
|
||||
if done || time.Now().After(deadline) || ctx.Err() != nil {
|
||||
return
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -429,6 +430,126 @@ func TestHandedOffGatewayAnswersAKeptAliveConnection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// lagListener's connections, once told to, sit a second after the write
|
||||
// that ends the next chunked reply: as a busy machine can hold the
|
||||
// goroutine that wrote a reply, its agent reading it, before net/http
|
||||
// notes the connection idle.
|
||||
type lagListener struct {
|
||||
net.Listener
|
||||
lag atomic.Bool
|
||||
}
|
||||
|
||||
func (l *lagListener) Accept() (net.Conn, error) {
|
||||
c, err := l.Listener.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &lagConn{Conn: c, lag: &l.lag}, nil
|
||||
}
|
||||
|
||||
type lagConn struct {
|
||||
net.Conn
|
||||
lag *atomic.Bool
|
||||
}
|
||||
|
||||
func (c *lagConn) Write(b []byte) (int, error) {
|
||||
n, err := c.Conn.Write(b)
|
||||
if bytes.HasSuffix(b, []byte("0\r\n\r\n")) && c.lag.Swap(false) {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// A kept-alive connection whose reply its agent has read, which net/http
|
||||
// notes idle only once the end has begun (the goroutine that wrote it was
|
||||
// held), has its next request answered there too: the end waited for new
|
||||
// and idle connections only, so Shutdown closed this one as idle when
|
||||
// net/http noted it, and the agent's next request was cut (ubuntu CI,
|
||||
// 56602426d). So too for a `make dev` backend.
|
||||
func TestHandedOffGatewayAnswersAConnectionStillAnswering(t *testing.T) {
|
||||
for _, dev := range []bool{false, true} {
|
||||
name := "handed off"
|
||||
if dev {
|
||||
name = "make dev"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
was := Handover
|
||||
Handover = dev
|
||||
defer func() { Handover = was }()
|
||||
quickChat(t)
|
||||
tcp, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := tcp.Addr().String()
|
||||
t.Setenv("MAGPIE_ADDR", addr)
|
||||
lagged := &lagListener{Listener: tcp}
|
||||
Inherit(lagged, func() {})
|
||||
old := New()
|
||||
stopOld, oldDone := listening(t, old)
|
||||
t.Cleanup(stopOld)
|
||||
|
||||
c, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
rd := bufio.NewReader(c)
|
||||
ask := func() (*http.Response, string, error) {
|
||||
chatOn(t, c, addr)
|
||||
c.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||
r, err := http.ReadResponse(rd, nil)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
b, err := io.ReadAll(r.Body)
|
||||
return r, string(b), err
|
||||
}
|
||||
lagged.lag.Store(true)
|
||||
if r, b, err := ask(); err != nil || r.StatusCode != 200 || r.Close {
|
||||
t.Fatalf("the request before the handover: %v %q %v", r, b, err)
|
||||
}
|
||||
// read, while the gateway's goroutine still sits in its write
|
||||
if !dev {
|
||||
f, err := tcp.(*net.TCPListener).File()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ln, err := net.FileListener(f) // the new one's copy, keeping the socket open
|
||||
f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
old.HandOff()
|
||||
}
|
||||
stopOld()
|
||||
// past the write's second, net/http has noted it idle: inside
|
||||
// the 5s the end waits (unreadWait)
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
r, b, err := ask()
|
||||
if err != nil || r.StatusCode != 200 || !strings.Contains(b, "quick") {
|
||||
t.Fatalf("a request on a connection still answering as the end began: %q %v", b, err)
|
||||
}
|
||||
if !r.Close {
|
||||
t.Fatal("the reply kept the connection alive, for a request the old one's end would cut")
|
||||
}
|
||||
select {
|
||||
case err := <-oldDone:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("the old one never ended once its connections were answered")
|
||||
}
|
||||
if n := len(old.Recent()); n != 2 {
|
||||
t.Fatalf("the old one answered %d of its 2 requests", n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// heldListener holds the next connection it takes, once told to, until
|
||||
// let go: as a busy machine can hold the goroutine that took one, before
|
||||
// net/http notes it.
|
||||
|
||||
Reference in New Issue
Block a user