Files
magpie/internal/gateway/plugin_head_wait_test.go
T
yetone 1c59acd36d plugin: a fetch whose head never comes is given up on; Devin min 0.1.45 (wu_chong4136 on X)
wu_chong4136 on X: with the Devin plugin a session sometimes hung for
10 to 20 minutes with no reply, until the effort or the context was
changed. The Devin plugin returns its fetch's head only once Devin sends
the reply's first content, and the host's headRead waited for that head
with no bound, while the gateway kept the agent alive on keepalives. A
request Devin took and never answered therefore hung silently. The body
already had bodyIdleWait (30m between bytes, #4934); the head had none.

- host.go headRead: the same bodyIdleWait bounds the wait for the head.
  On it the fetch ends with "the plugin sent no reply in 30m0s; that
  fetch was given up on", Fetch settles the call, and the host writes
  "abort", so the plugin's fetch signal ends its vendor request too. The
  gateway answers the agent its retryable 502 (an SSE error event on a
  stream). This holds for every plugin fetch, not only Devin's.
- ReplyWaitForTest lets the gateway test shorten the wait.
- Devin mover min 0.1.45 (on npm, `npm view --prefer-online`): the
  plugin itself gives up after 5 minutes of quiet from Devin (no head,
  or no frame between reads) with a retryable 504. A real probe saw
  Devin's first frame at 2.5s and at most 13s between frames.
  (magpie-community/plugins e30f8ed0)
- provider-plugins.md says so.

Tests:
- TestHostHeadWaitEndsAFetchNeverAnswered: a head that never comes ends
  in "sent no reply", the host is sent "abort", and the call is
  released; TestHostHeadWaitSparesAHeadInTime: a head in time is kept.
- TestPluginHeadNeverSentEndsInAnError (gateway, through the fake
  plugin in Bun): a vendor that reads the request and never answers
  gives the agent the error on a stream and on a whole reply, and every
  vendor request the plugin made is closed afterwards (asked 30, open 0).

Run:
- mote: the three tests above, ok.
- Broken by hand (headRead without the timer case): the host test fails
  ("held its reader for ever") and the gateway test hangs to its 60s
  timeout in io.ReadAll.
- mica: gofmt -l clean; go vet for darwin, GOOS=linux and GOOS=windows
  on internal/plugin, internal/gateway, internal/provider: ok.
- mica: go test -tags nogui -timeout 45m ./... at this tree (rebased on
  ac4a4cf84): 101 packages ok; internal/gateway FAIL on
  TestImageMCPKeepsDrawingsOffTheHeap alone ("the heap grew by 31 MB"),
  which passes 3 of 3 alone at the same tree and reads the whole
  process heap; filed as #6890.

Not tried: a real Devin request that goes quiet (it can't be made to
happen on demand).
2026-11-17 00:39:50 +08:00

67 lines
2.4 KiB
Go

package gateway
import (
"context"
"io"
"net/http"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// A plugin whose vendor took the request and never answered it (the Devin
// plugin answers its head only once Devin sends the reply's first words)
// left the agent on keepalives with no error for as long as it waited
// (wu_chong4136 on X: 10 to 20 minutes). The plugin's fetch now gives the
// head up after the reply wait, and the agent gets an error it can retry,
// on a stream and on a whole reply. Before, both requests held until the
// client's own deadline.
func TestPluginHeadNeverSentEndsInAnError(t *testing.T) {
t.Cleanup(plugin.ReplyWaitForTest(300 * time.Millisecond))
var asked, open atomic.Int64
up := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked.Add(1)
open.Add(1)
defer open.Add(-1)
// read whole, so the server watches the connection and sees the
// host's abort close it
_, _ = io.Copy(io.Discard, r.Body)
<-r.Context().Done() // took the request, never answers
})
url, model, client := pluginReplyGateway(t, up)
for _, stream := range []bool{true, false} {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url+"/v1/chat/completions", strings.NewReader(pluginReplyRequest(model, stream)))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
cancel()
t.Fatalf("stream=%t: %v", stream, err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
cancel()
if err != nil {
t.Fatalf("stream=%t: the reply was held to the client's deadline: %v", stream, err)
}
if !strings.Contains(string(b), "sent no reply") {
t.Fatalf("stream=%t: status %d, body %q, want the give-up named", stream, res.StatusCode, b)
}
if res.StatusCode == http.StatusOK && !strings.Contains(string(b), "error") {
t.Fatalf("stream=%t: a 200 with no error: %q", stream, b)
}
t.Logf("stream=%t: %d %s", stream, res.StatusCode, b)
}
deadline := time.Now().Add(10 * time.Second)
for open.Load() > 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
t.Logf("asked %d, open %d", asked.Load(), open.Load())
if n := open.Load(); n > 0 {
t.Fatalf("%d of %d vendor requests still open after the fetch was given up on", n, asked.Load())
}
}