gateway: Codex's own sign-in asks again after a connection dropped with no reply
aetrox333 on Discord: on Codex, bare gpt-6-luna and gpt-6.1-terra
failed with 502 "OpenAI can't be reached (Post …: EOF)" after 43ms
every time, through a fake-IP proxy (chatgpt.com at 198.18.0.12),
while auto/gpt-6-luna and auto/gpt-6.1-terra answered 200 every time.
Cause: a bare Codex model goes to ChatGPT on Codex's own sign-in
(codexUpstreamOn), which ended the turn with 502 at the first transport
error. Go's transport sends a POST again by itself only on a reused
connection (replayable, #2136), so an EOF on a fresh one, a proxy
closing it before a byte of a reply, came straight back to Codex.
auto/ and codex/ go through the accounts loop, which takes the same
502 as passing and asks again after a pause on a new connection, or
asks the group's next member. Both routes share one client, transport,
proxy choice and HTTP/1.1; a recorded diff of the two requests showed
no difference that would change how a proxy treats them (the routed
one adds instructions, Openai-Beta, Session-Id and its own token).
Now a dropped or reset connection with no reply on the own sign-in's
/responses is asked again, retryPause<<n apart, up to lastRetries, as
an overload is (askAgain: the trace shows each try, Codex is kept
alive). The caller gone and a wait that ran out are not asked again.
After the last try Codex is told OpenAI can't be reached, as before.
Run (mote, at this head rebased on dca1d138c): gofmt -l; go vet
./internal/gateway on darwin, linux (-tags nogui) and windows;
go test ./internal/gateway -run 'Codex|Overload|Busy|Retry|Unreach|Socket'
ok 54s; the two new tests -count=3 ok.
Broken by hand: with the new branch turned off,
TestCodexOwnSignInAskedAgainAfterEOF fails with the reporter's exact
text ("OpenAI can't be reached (Post \"…/backend-api/codex/responses\":
EOF)"), and TestCodexOwnSignInEOFTold sees 1 try, not 3. The codex/
case in the first test passes on the old code too (the accounts loop
asked again already), which is the asymmetry reported.
Not tried: the real ChatGPT backend through a real Clash/Surge TUN
proxy; why the reporter's proxy drops the first connection is not
known, and a proxy that drops every connection still ends in 502 (now
after three tries). Whether the reporter's auto/ 200s came from Codex's
sign-in on a second try or from another member of the group is not
known. Magpie's own ChatGPT accounts relayed on this path (on != nil)
and the API-key route are left as they were; the full suite runs after
the push.
This commit is contained in:
@@ -1205,6 +1205,19 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
|
||||
sent = provider.CodexCompressed(req, body)
|
||||
if res, err = s.client.Do(req); err != nil {
|
||||
msg := "OpenAI: " + err.Error()
|
||||
if overload && busyAgain < lastRetries && droppedUnanswered(r.Context(), err) {
|
||||
// the connection closed with nothing of a reply (a
|
||||
// proxy's or ChatGPT's front's "EOF" on a fresh
|
||||
// connection, which Go doesn't send again by itself):
|
||||
// asked again after a moment on a new one, as the
|
||||
// accounts loop asks its last account again after the
|
||||
// same 502 (aetrox333 on Discord: gpt-6-luna failed
|
||||
// in 43ms every time, auto/gpt-6-luna never)
|
||||
if !askAgain(502, msg, retryPause<<busyAgain) {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
if rest == "/responses" {
|
||||
msg = codexUnreached(modelOf(body), err)
|
||||
}
|
||||
@@ -1694,6 +1707,19 @@ func codexKeyRefused(msg []byte) string {
|
||||
"or sign Codex in with ChatGPT, or with an OpenAI API key"
|
||||
}
|
||||
|
||||
// droppedUnanswered says whether err, a request's that got no reply, is
|
||||
// one that may pass when asked again: the connection closed or reset
|
||||
// before a byte of a reply, or couldn't be made. Not the caller gone, nor
|
||||
// a wait for the reply that ran out, which a second try would wait out
|
||||
// again.
|
||||
func droppedUnanswered(ctx context.Context, err error) bool {
|
||||
if err == nil || ctx.Err() != nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return false
|
||||
}
|
||||
var ne interface{ Timeout() bool }
|
||||
return !errors.As(err, &ne) || !ne.Timeout()
|
||||
}
|
||||
|
||||
// codexUnreached says why a model of Codex's own failed with 502: OpenAI,
|
||||
// where it goes with Codex's own sign-in, couldn't be reached — often from
|
||||
// a Codex whose only sign-in is a relay's key, whose own models it still
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// TestCodexOwnSignInAskedAgainAfterEOF: a turn of one of Codex's own
|
||||
// models, which goes to ChatGPT on Codex's own sign-in, met a connection
|
||||
// closed with nothing of a reply — a fresh one, which Go's transport
|
||||
// doesn't send again by itself (aetrox333 on Discord: "OpenAI can't be
|
||||
// reached (Post …: EOF)" after 43ms, every time, through a fake-IP
|
||||
// proxy). It is asked again on a new connection, as the accounts loop asks
|
||||
// its last account again after the same 502, and Codex has the reply.
|
||||
func TestCodexOwnSignInAskedAgainAfterEOF(t *testing.T) {
|
||||
codexSignedIn(t)
|
||||
pause := retryPause
|
||||
retryPause = time.Millisecond
|
||||
t.Cleanup(func() { retryPause = pause })
|
||||
var posts atomic.Int64
|
||||
h := func(w http.ResponseWriter, r *http.Request) {
|
||||
io.ReadAll(r.Body)
|
||||
if posts.Add(1) == 1 {
|
||||
// closed before a byte of a reply, on the connection's first
|
||||
// request
|
||||
c, _, err := w.(http.Hijacker).Hijack()
|
||||
if err == nil {
|
||||
c.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
io.WriteString(w, sse(
|
||||
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6-luna"}}`,
|
||||
`data: {"type":"response.output_text.delta","delta":"pong"}`,
|
||||
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`))
|
||||
}
|
||||
up := httptest.NewServer(http.HandlerFunc(h))
|
||||
t.Cleanup(up.Close)
|
||||
forgetCodexLists(t)
|
||||
was := provider.CodexBase
|
||||
provider.CodexBase = up.URL + "/backend-api/codex"
|
||||
t.Cleanup(func() { provider.CodexBase = was })
|
||||
|
||||
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-6-luna"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
s := New()
|
||||
// the bare name goes on Codex's own sign-in; codex/ through the
|
||||
// accounts loop, which asked again already
|
||||
for _, m := range []string{"gpt-6-luna", "codex/gpt-6-luna"} {
|
||||
posts.Store(0)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"`+m+`","stream":true,"input":"ping"}`))
|
||||
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
||||
req.Header.Set("chatgpt-account-id", "acct-1")
|
||||
req.Header.Set("Accept", "text/event-stream")
|
||||
s.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") {
|
||||
t.Fatalf("%s: status %d after a dropped connection, want the reply asked again for: %.300s", m, rec.Code, rec.Body.String())
|
||||
}
|
||||
if n := posts.Load(); n != 2 {
|
||||
t.Fatalf("%s: ChatGPT was asked %d times, want 2 (the dropped try and one more)", m, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodexOwnSignInEOFTold: a connection dropped on every try is asked
|
||||
// again only as many times as an overload is, and Codex is then told
|
||||
// OpenAI couldn't be reached, as before.
|
||||
func TestCodexOwnSignInEOFTold(t *testing.T) {
|
||||
codexSignedIn(t)
|
||||
pause := retryPause
|
||||
retryPause = time.Millisecond
|
||||
t.Cleanup(func() { retryPause = pause })
|
||||
var posts atomic.Int64
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.ReadAll(r.Body)
|
||||
posts.Add(1)
|
||||
if c, _, err := w.(http.Hijacker).Hijack(); err == nil {
|
||||
c.Close()
|
||||
}
|
||||
}))
|
||||
t.Cleanup(up.Close)
|
||||
forgetCodexLists(t)
|
||||
was := provider.CodexBase
|
||||
provider.CodexBase = up.URL + "/backend-api/codex"
|
||||
t.Cleanup(func() { provider.CodexBase = was })
|
||||
|
||||
s := New()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-6-luna","stream":false,"input":"ping"}`))
|
||||
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
||||
req.Header.Set("chatgpt-account-id", "acct-1")
|
||||
s.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != 502 || !strings.Contains(rec.Body.String(), "OpenAI can't be reached") {
|
||||
t.Fatalf("status %d, want 502 saying OpenAI can't be reached: %.300s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if n := posts.Load(); n != int64(lastRetries)+1 {
|
||||
t.Fatalf("ChatGPT was asked %d times, want %d", n, lastRetries+1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user