gateway: a request waiting for body room is not answered 408 (#4511 review)
The previous commit counted a body as still arriving whenever less of it
had been read than its Content-Length. Before the first read that holds
for a request waiting in bodyRoom.take, which is magpie's own wait. Its
client may have sent the whole body already, and a 408 there tells an
SDK to send it again at once, onto a room already full. bodyArriving
now holds only once the body's read has begun ("body" wait seen) and
while no body-room wait is under way. That is the gap between two reads
that #4511's full run raced into.
The patch's TestUnroutedBodyShortOfItsLengthAnswered408 asserted the
408 in the room wait. It is replaced by:
- TestUnroutedBodyRoomWaitNot408: same setup, not a 408.
- TestUnroutedBodyArrivingBetweenReads: not read yet / in the room wait /
between reads / read whole / no length.
Reproduced #4511 by injecting a gap. waitedBody.Read ended its wait and
slept 30ms after each read, and TestUnroutedTrickledBodyAnswered408
-count=5:
- with the narrowed check: ok 5 of 5;
- with giveUp back to "case u.inBody():": red, answered 504 "held this
request for 1s without routing", the CI red.
Broken by hand: bodyArriving back to the patch's (length only). Both
new tests fail, and the room wait gets the 408.
Run (on mote, through mote-run):
- Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack
on c30d3b7b6: 82 packages ok. internal/gui was red only on
TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll from
b09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither
touches this stack.
- Rebased onto 29d0ab39f, 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 at fb29cd30f: ok.
This commit is contained in:
@@ -474,16 +474,35 @@ 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
|
||||
// bodyArriving is whether the handler has begun reading the body, is
|
||||
// between two of its reads, and less has come than the request's own
|
||||
// Content-Length says. The hold can run out in the gap between two reads
|
||||
// of a trickled body, 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.
|
||||
// the client's. Not before its first read: a request waiting for body
|
||||
// room (bodyRoom.take) is held by magpie, whose 503 or 504 says so, and
|
||||
// its client may have sent the whole body already. 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
|
||||
if d <= 0 || u.bodyRead.Load() >= d {
|
||||
return false
|
||||
}
|
||||
u.wmu.Lock()
|
||||
defer u.wmu.Unlock()
|
||||
read := false
|
||||
for _, k := range u.kinds {
|
||||
switch k.kind {
|
||||
case "body":
|
||||
read = true
|
||||
case "body room":
|
||||
if k.n > 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return read
|
||||
}
|
||||
|
||||
// bodySaid is how much of u's request's body had come, and when the last
|
||||
|
||||
@@ -104,11 +104,42 @@ func isClosedConn(err error) bool {
|
||||
// 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) {
|
||||
// the request's own Content-Length says, once its read has begun, the
|
||||
// body is still arriving and the time is the client's, inside a read or
|
||||
// between two. Before its first read, in the wait for body room, the
|
||||
// time is magpie's own.
|
||||
func TestUnroutedBodyArrivingBetweenReads(t *testing.T) {
|
||||
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(strings.Repeat(" ", 400)))
|
||||
u := &unroutedHold{r: r}
|
||||
if u.bodyArriving() {
|
||||
t.Fatal("a body not yet read counted as arriving")
|
||||
}
|
||||
room := u.wait("body room")
|
||||
if u.bodyArriving() {
|
||||
t.Fatal("a body waiting for room counted as arriving")
|
||||
}
|
||||
room()
|
||||
// one read returned 19 bytes; the next has not begun
|
||||
u.wait("body")()
|
||||
u.bodyRead.Store(19)
|
||||
if !u.bodyArriving() {
|
||||
t.Fatal("a body between two reads, 19 of 400 come, did not count as arriving")
|
||||
}
|
||||
u.bodyRead.Store(400)
|
||||
if u.bodyArriving() {
|
||||
t.Fatal("a body read whole counted as arriving")
|
||||
}
|
||||
u.bodyRead.Store(19)
|
||||
r.ContentLength = -1
|
||||
if u.bodyArriving() {
|
||||
t.Fatal("a body of no said length counted as arriving between reads")
|
||||
}
|
||||
}
|
||||
|
||||
// A request waiting for body room before its first read is held by
|
||||
// magpie, not by its client, whose body may all be sent: it is not
|
||||
// answered 408 "its body was still arriving".
|
||||
func TestUnroutedBodyRoomWaitNot408(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
|
||||
@@ -136,18 +167,13 @@ func TestUnroutedBodyShortOfItsLengthAnswered408(t *testing.T) {
|
||||
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)
|
||||
res, err := http.ReadResponse(bufio.NewReader(conn), 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)
|
||||
if res.StatusCode == http.StatusRequestTimeout || strings.Contains(string(b), "its body was still arriving") {
|
||||
t.Fatalf("a request waiting for magpie's body room was answered %d %s", res.StatusCode, b)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user