gateway: a body short of its own Content-Length is answered 408 even with no read under way
The unrouted hold's 408 (the client is still sending its body) was judged by whether the handler was inside a body read that instant. unroutedMost can run out between two reads of a trickled body, or before its first, and there the read's absence answered 504, as if magpie held the request of its own (#4511, red in a full run). The body is judged by its state now: less come than the request's own Content-Length says is still arriving, and the time is the client's. A body read whole, a length never said and an empty one are judged by the read alone, as before. Reviewed and applied from #4511 (ggbdpq, c59083f5f, SHA-256 matches). The next commit narrows it. As written, a request still waiting for magpie's own body room, before its first read, was answered 408 "its body was still arriving". That blames a client whose body may all be sent already. Run (on mote, through mote-run): - Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack onc30d3b7b6: 82 packages ok. internal/gui was red only on TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll fromb09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither touches this stack. - Rebased onto29d0ab39f, which changed internal/gateway since then: gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -count=1 ./internal/gateway ./internal/usage ./internal/provider ./internal/sessions all ok at the pushed head. go build ./... ok for darwin, linux (nogui) and windows. - go test -race of the gateway tests this stack adds or touches, on the stack atfb29cd30f: ok.
This commit is contained in:
@@ -474,6 +474,18 @@ func (u *unroutedHold) inBody() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// bodyArriving is whether less of the body has come than the request's
|
||||
// own Content-Length says. The hold can run out between two reads, or
|
||||
// before the first, and a read's absence alone answered such a request
|
||||
// 504, as if magpie held it of its own (a full run raced one out there,
|
||||
// #4511); a body short of its length is still arriving, and the time is
|
||||
// the client's. A body read whole, a length never said and an empty one
|
||||
// are judged by the read alone.
|
||||
func (u *unroutedHold) bodyArriving() bool {
|
||||
d := u.r.ContentLength
|
||||
return d > 0 && u.bodyRead.Load() < d
|
||||
}
|
||||
|
||||
// bodySaid is how much of u's request's body had come, and when the last
|
||||
// of it did: "12 KB of 3 MB, the last 2s ago".
|
||||
func (u *unroutedHold) bodySaid() string {
|
||||
@@ -602,7 +614,7 @@ func (u *unroutedHold) giveUp(unknown bool) {
|
||||
where = fmt.Sprintf(" %s of it in no wait magpie knows of", unroutedUnknown)
|
||||
case u.masking.Load() > 0:
|
||||
where = " masking it"
|
||||
case u.inBody():
|
||||
case u.inBody() || u.bodyArriving():
|
||||
// the client was still sending it (heatonz on Discord: 2m0s
|
||||
// "without routing" with the body half in): a 408 says whose
|
||||
// the time was, and an SDK sends it again at once
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -99,3 +100,54 @@ func TestUnroutedTrickledBodyAnswered408(t *testing.T) {
|
||||
func isClosedConn(err error) bool {
|
||||
return err != nil && (strings.Contains(err.Error(), "connection reset") || strings.Contains(err.Error(), "closed"))
|
||||
}
|
||||
|
||||
// A full run caught the trickle above red with a 504 (#4511): the hold
|
||||
// ran out between two of its reads, and the read's absence alone was
|
||||
// taken for a hold of magpie's own. Less of the body having come than
|
||||
// the request's own Content-Length says, the body is still arriving and
|
||||
// the time is the client's, whether its handler was inside a read when
|
||||
// the hold ran out or between two, or before its first. Here the hold
|
||||
// runs out before the first read, in the wait for body room.
|
||||
func TestUnroutedBodyShortOfItsLengthAnswered408(t *testing.T) {
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
unroutedLimits(t, 20*time.Millisecond, 1500*time.Millisecond)
|
||||
// the room holds 2 MiB and this test holds 3 of it, so a request
|
||||
// with a 1 MiB body waits for room before its first read, and keeps
|
||||
// waiting while unroutedMost runs out
|
||||
wasRoom := bodyRoom
|
||||
bodyRoom = roomOf(2 << 20)
|
||||
t.Cleanup(func() { bodyRoom = wasRoom })
|
||||
hold, end := context.WithCancel(context.Background())
|
||||
defer end()
|
||||
if err := bodyRoom.take(hold, 3<<20); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
s, _ := slowEyed(t, 0)
|
||||
srv := httptest.NewServer(s.Handler())
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
// a body of a declared 1 MiB, none of it sent
|
||||
conn, err := net.Dial("tcp", strings.TrimPrefix(srv.URL, "http://"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { conn.Close() })
|
||||
conn.SetDeadline(time.Now().Add(20 * time.Second))
|
||||
fmt.Fprintf(conn, "POST /v1/chat/completions HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n", 1<<20)
|
||||
|
||||
br := bufio.NewReader(conn)
|
||||
res, err := http.ReadResponse(br, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusRequestTimeout || !strings.Contains(string(b), "its body was still arriving") || !strings.Contains(string(b), "read: none of 1.0 MB") {
|
||||
t.Fatalf("a body none of which had come was answered %d %s", res.StatusCode, b)
|
||||
}
|
||||
// the connection ends with the answer
|
||||
if _, err := br.ReadByte(); err != io.EOF && !isClosedConn(err) {
|
||||
t.Fatalf("after the 408 the connection read %v, not its end", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user