magpie icon indicating copy to clipboard operation
magpie copied to clipboard

Codex side-session (title/prediction) requests trigger a nil-pointer panic in agent.CodexSideModel that kills the whole gateway (0.1.4350 / 0.1.4656)

Open woniu-huang opened this issue 2 weeks ago • 1 comments

Summary

When Codex (desktop app, ChatGPT sign-in) is routed through the magpie gateway, a Codex side-session request (session title / predictions) reliably panics the gateway with a nil-pointer dereference in agent.CodexSideModel. The recover middleware in busy.go re-panics ([recovered, repanicked]), so the entire process dies and every connected Codex session drops into a reconnect loop until magpie is relaunched.

This happened 3 times in ~15 minutes on 2026-11-22, across two versions:

Version Crash time (local) Panic site
0.1.4350 01:21 and 01:27 internal/agent/codex.go:3246
0.1.4656 ~01:33 (right after hot-update handover) internal/agent/codex.go:3328

An earlier, possibly related crash on 0.1.4026 hit agent.CodexOwnOnly at internal/agent/codex.go:3158 (models-etag path) with the same [recovered, repanicked] pattern.

Environment

  • macOS (darwin/arm64), magpie menu-bar app, auto-update enabled
  • Codex desktop app authenticated via ChatGPT plan, routed through magpie (openai_base_url = http://127.0.0.1:3425/v1)
  • Multiple Codex sessions open concurrently
  • Corporate network with TLS-intercepting proxy (may or may not be relevant; the panic is a local nil deref)

Steps to reproduce

  1. Route Codex through magpie with a ChatGPT-plan provider.
  2. Open/use a Codex session so the app issues side-session requests (title generation / predictions).
  3. Gateway panics and exits; all Codex sessions show "reconnecting".

It reproduced on both 0.1.4350 and 0.1.4656 within minutes of normal use — no special action needed beyond having active Codex sessions.

Panic stack (0.1.4656, trimmed to the relevant frames)

panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1061d9578]

goroutine 3243 [running]:
github.com/yetone/magpie/internal/gateway.(*Server).Handler.(*Server).counted.func10.2.deferwrap1()
	github.com/yetone/magpie/internal/gateway/busy.go:104 +0x20
github.com/yetone/magpie/internal/gateway.(*Server).Handler.(*Server).counted.func10.2()
	github.com/yetone/magpie/internal/gateway/busy.go:116 +0x188
panic({0x108e38460?, 0x10a0036a0?})
	runtime/panic.go:860 +0x12c
github.com/yetone/magpie/internal/agent.CodexSideModel()
	github.com/yetone/magpie/internal/agent/codex.go:3328 +0x98
github.com/yetone/magpie/internal/gateway.(*Server).servedSession(0x5338725622c8?, 0x53387dcdb540, {0x0?, 0x603823?})
	github.com/yetone/magpie/internal/gateway/codex_follow.go:225 +0x4c
github.com/yetone/magpie/internal/gateway.(*Server).codexSideSession(0x533872562008, 0x53387dcdb540, {0x53389eedc000?, 0x53388c95a530?, 0x53388c95a530?})
	github.com/yetone/magpie/internal/gateway/codex_follow.go:204 +0x48
github.com/yetone/magpie/internal/gateway.(*Server).routes.(*Server).handle.func7.1()
	github.com/yetone/magpie/internal/gateway/gateway.go:2569 +0x2c
github.com/yetone/magpie/internal/gateway.codexSideTo({0x53389eedc000, 0x603823, 0xc05fbb}, 0x1, 0x53388e68db80)
	github.com/yetone/magpie/internal/gateway/codex_titles.go:68 +0x90
github.com/yetone/magpie/internal/gateway.codexPredictionsTo(0x53388cbc7c20, {0x53389eedc000, 0x603823, 0xc05fbb}, 0x1, 0x53388e68db80)
	github.com/yetone/magpie/internal/gateway/codex_titles.go:270 +0x174
github.com/yetone/magpie/internal/gateway.(*Server).routes.(*Server).handle.func7({0x1092b3d40, 0x53388cbc7e90}, 0x53387dcdb540)
	github.com/yetone/magpie/internal/gateway/gateway.go:2586 +0x6b8
net/http.HandlerFunc.ServeHTTP(...)
	... (standard middleware chain: keySlots → keyLimited → keptBodyRequests → withCaller → readOnlyGuard → callerGuard → corsGuard → holdCatalog → headerLogged → counted → retryCapped → dumpFirst → socketEntry → lanGuard)

On 0.1.4350 the stack is identical except CodexSideModel is at codex.go:3246. Note the {0x0?, ...} argument into servedSession — something in the side-session lookup appears to be nil when CodexSideModel() dereferences it.

Impact

  • The whole gateway process exits (not just the one request), because the counted middleware's recover re-panics at busy.go:116.
  • Every Codex session routed through magpie disconnects and loops "reconnecting 3/5…" until the app is manually relaunched.
  • Auto-update does not help: the bug survived 0.1.4026 → 0.1.4350 → 0.1.4656.

Workaround applied

Disconnected Codex from magpie entirely (magpie codex default + removed openai_base_url / model_catalog_json from ~/.codex/config.toml); Codex now talks to OpenAI directly and is stable.

Suggestions

  1. Nil-guard the side-session/model lookup in CodexSideModel (and CodexOwnOnly) so a missing session degrades to a normal HTTP error instead of a panic.
  2. Consider not re-panicking in the busy.go recover middleware for request-scoped panics — a side-session title request should never be able to take down the whole gateway.

Full logs available on request (~/Library/Logs/magpie/app.log).

woniu-huang avatar Nov 21 '26 17:11 woniu-huang

I have a fix ready on fork branch eisen0419:fix/7452-codex-side-model-nil-panic (03879575, based on e5c2eb94):

  • Root cause: When codex.scope is "profile", codex(home) returns codexProfileIn, which leaves SideModel, FailingOver, and EndFailover nil. CodexSideModel, CodexOwnOnly, EndCodexFailover, and TakeCodexLeftovers called those hooks without nil checks, so a side-call request panicked with a nil-pointer dereference and crashed the process.
  • Fix: Nil-guard all four optional hook wrappers in internal/agent/codex.go, implement TakeLeftovers on codexProfileIn in internal/agent/codex_profile.go so uninstall cleans orphaned magpie.config.toml/magpie-models.json and leftover config.toml entries while preserving user-owned settings, update docs/subsystems/agent-wiring.md and docs/subsystems/gateway-routing.md, and add regression coverage in internal/agent/codex_side_test.go.
  • Verification: go test -race -count=1 ./internal/agent -run 'TestCodex(Profile|Side|Failover|OwnOnly|Leftover|Scope)', go test -race -count=1 ./internal/uninstall/..., go test -race -count=1 ./internal/gateway -run 'TestCodexSide', go test -count=1 -run TestDocs ./internal/testenv, go test -count=1 ., and 3-OS go vet all pass.

eisen0419 avatar Nov 21 '26 20:11 eisen0419