Files
yetone 51aa4c0ef3 codex: a turn left open by a gone app-server doesn't hold an account switch back (danzi002)
蛋仔 (danzi002 on Discord, 10-29 and 10-30) switched the Codex app's
used-up account in magpie, auth.json changed, and the app stayed grey
until it was quit and opened: "如果打开了 codex 再不点击任何会话的时候,
我手动切换账号它会重启,如果点了会话再切换账号就重启不了". dd9828f2b
made a switch restart every one of the app's own app-servers once no
Codex turn runs. This one never ran.

Why. Codex reads auth.json once; a 401 reloads it only for the same
account id (codex-rs/login/src/auth/manager.rs, reload_if_account_id_
matches), so only a new app-server takes up a switched account.
codexAppStep waits while any session file written to in the last 30
minutes ends in task_started. Tried with the real codex
0.164.0-alpha.11.1 (ChatGPT.app's), sandbox HOME/CODEX_HOME, a fake
Responses server holding a turn open:
- SIGTERM or stdin closed mid-turn: turn_aborted is written.
- SIGKILL (force quit, crash): the file ends in task_started for good.
- thread/resume in a new app-server, which is what opening the thread
  in the app does, appends thread_settings_applied and moves the mtime
  each time, nothing else changed.
- a 429 usage_limit_reached ends the turn with task_complete, so being
  out of credit doesn't leave one open.
So a thread whose turn a killed app-server left open reads as running
for as long as the user keeps opening it, and the switch waits forever.

Change. proc.Started(pid) reads when a process started (darwin
kern.proc.pid P_starttime, Linux /proc/<pid>/stat starttime + btime,
Windows GetProcessTimes; ErrNoStart elsewhere). codexAppStep and
RestartCodexAppWhenIdle pass the oldest start of the app-servers they
would end; an open turn whose task_started line's timestamp is more
than 2s before it can't be running in them and no longer counts. A
start or a timestamp that can't be read counts the turn as running, as
before. A turn started in a running app-server still holds it: no turn
is cut short.

Run (mote-run unless said):
- gofmt -l internal/proc internal/provider: clean.
- go vet ./internal/proc ./internal/provider on darwin, GOOS=linux
  -tags nogui, GOOS=windows: ok.
- go test -count=1 -timeout 45m ./internal/proc ./internal/provider:
  ok (6.2s, 110s).
- wsl-run (magpie-desktop) and win-run (xiaomi-gpu): TestStarted,
  TestCodexTurnOpen, TestCodexApp*: ok.
- The real killed-turn rollout above, copied into a temp test: open
  with since unknown, not open with since after the kill, open with
  since before the turn.
Broken by hand:
- codexAppStep passing a zero since (the old behaviour):
  TestCodexAppSwitchPastATurnItsServerNeverRan red ("ended [], want
  [60]").
- sessionTurnOpen ignoring since: TestCodexTurnOpen red too.
Not tried: the reporter's own app on a real account switch; whether
their app-server was in fact killed (force quit or crash) is not known.
A restarted app-server with the thread left open was not watched
ungreying Send in the real app.
2026-10-30 01:22:45 +08:00

65 lines
1.6 KiB
Go

package proc
import (
"bytes"
"errors"
"os"
"strconv"
"strings"
"time"
)
// clockTicks is USER_HZ, the unit of /proc/<pid>/stat's starttime: 100 on
// every Linux the kernel's ABI keeps it fixed for.
const clockTicks = 100
func startTime(pid int) (time.Time, error) {
b, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat")
if err != nil {
return time.Time{}, err
}
ticks, err := statStartTicks(b)
if err != nil {
return time.Time{}, err
}
s, err := os.ReadFile("/proc/stat")
if err != nil {
return time.Time{}, err
}
boot, err := bootTime(s)
if err != nil {
return time.Time{}, err
}
return boot.Add(time.Duration(ticks) * time.Second / clockTicks), nil
}
// statStartTicks is field 22 of a /proc/<pid>/stat line, starttime: the
// fields are counted after the command's closing parenthesis, since the
// command itself may hold spaces and parentheses.
func statStartTicks(b []byte) (uint64, error) {
i := bytes.LastIndexByte(b, ')')
if i < 0 {
return 0, errors.New("unreadable /proc stat")
}
f := strings.Fields(string(b[i+1:]))
// f[0] is field 3 (state), so field 22 is f[19]
if len(f) < 20 {
return 0, errors.New("unreadable /proc stat")
}
return strconv.ParseUint(f[19], 10, 64)
}
// bootTime is /proc/stat's btime.
func bootTime(b []byte) (time.Time, error) {
for _, line := range strings.Split(string(b), "\n") {
if v, ok := strings.CutPrefix(line, "btime "); ok {
n, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(n, 0), nil
}
}
return time.Time{}, errors.New("no btime in /proc/stat")
}