System() ran readSystem under sysCache's lock, and on macOS that is
scutil --proxy, started with proc.Command and no timeout. When neither
magpie's settings nor the environment name a proxy, every request's
proxy comes from there (For, through Func: http.DefaultTransport, the
gateway's upstream transport, MCP, updates), so a scutil that never
answered held each of them on that lock for as long as it hung, past
its own deadline too: net/http looks at a request's context before it
calls Proxy, not while. With no system proxy set, scutil runs again
every 2 s it is asked. And a read that failed was taken as no proxy:
where vendors are reached only through the system's proxy, a scutil
that failed once sent the next 2 s of requests direct. The code
standards' "nothing that can block runs under a lock its reader needs"
and "a failed read means unknown".
Found while chasing #3362, an app whose partner fetch never came back.
Nothing there shows scutil was what hung; it is the one wait on that
path with no end.
Now:
- One read runs at a time, outside the lock. A request waits for it
until readWait (2 s) after it began, then takes what was read
before. Before the first read that is nothing, so direct, as a
failed first read was. A read already past readWait holds nobody.
- A read that fails keeps what was read before, and the read after it
holds nobody either: requests go on with the proxy read before until
a read answers.
- scutil gets toolWait (10 s) and is then ended (readTool, through
proc.CommandContext), and that read fails.
- Windows' registry: a key or value that isn't there is no proxy, as
before; any other error is a failed read. Elsewhere there is no
system proxy to read, and nothing changes.
ggbdpq's patch on #2562 has testenv stand in for scutil with exit 1.
Under this change that read fails with nothing read before it, which
is still no proxy, so it does what it did.
What was run (tree 6f5e4b92, on f3eb9292, go1.26.8):
before this change (2374ffe1, whose internal/netproxy differs from
f3eb9292 only in login_test.go), a scratch test with readSystem
held: a GET through Func with a 1 s deadline was still waiting
after 10.008s
go test ./internal/netproxy -run
'Stuck|FailedSystem|ToolThatHangs|SetAfterStart': ok
-race -count=20 ./internal/netproxy: no race; red in every run only
at TestBunProxyLoginWithReservedCharacters (Bun 1.3.11 on PATH names
port 80 in the request line, #3397)
-race -count=40 -cpu 1,2 of the four tests above: ok
broken by hand, with this change:
the read back under the lock (System as it was, new signature):
stuckread_test.go:103: 8 of 8 requests still wait on a read of
the system proxy that never ends
stuckread_test.go:137: a request still waits on a read of the
system proxy that never ends
a request waiting for the read with no bound: the same two
a read for each request:
stuckread_test.go:107: 8 reads of the system proxy at once,
want 1
readWait counted from each request, not from the read:
stuckread_test.go:114: a request waited 1.000135471s for a read
already past readWait (1s)
a failed read replacing what was read before:
stuckread_test.go:144: a failed read dropped the proxy read
before: {URL: HTTP: SOCKS: Bypass:[]}
the request after a failed read waiting for the next one:
stuckread_test.go:159: a request waited 1.000232607s for the
read after a failed one
the tool never ended (WithCancel for WithTimeout):
stuckread_test.go:185: a tool that never answered read as {URL:
HTTP: SOCKS: Bypass:[]}, true (sleep 30 ran out, 30.00s)
gofmt -l internal/netproxy: nothing
GOOS=linux, darwin, windows go vet -tags nogui ./internal/netproxy:
exit 0; CGO_ENABLED=0 GOOS=darwin and windows go build -tags nogui
.: exit 0
full suite, go test -tags nogui -count=1 -timeout 45m ./...
(467 s): 62 packages ok. 14 reds in 7 packages, none in a file this
change touches:
internal/agent TestInstalledVersion, TestOmpDetectedWithPiDir,
TestCursorFolderFromTheEditorIsNotTheCLI,
TestCopilotFolderFromAnIDEPluginIsNotTheCLI and
TestQoderSettingsFromAnIDEPluginIsNotTheCLI: proc.Look keeps "not
found" for a tool written in the tick of its folder's last change
(#3141).
internal/agent TestReasonixCLIRequiresSupportedNativeVersion and
TestReasonixCLIRecoversAfterProbeTimeout: their fixture build under
/tmp meets a /tmp/.git that isn't a repository of this user's
("error obtaining VCS status: exit status 128").
internal/claudebridge TestCallbackGoesPastTheAgentsProxy,
internal/gateway TestClaudeToolCallbackOnANonLoopbackAddress,
internal/gui TestGatewayHeldByAContainer and
TestGatewayFixQuitsOnlyAMagpie: the processes this run started had
their outgoing connections sent through a proxy (graftcp).
internal/edit TestWriteAtomicReadOnlyHardLink and internal/testenv
TestProgram: umask 077 here; the fixtures' own modes go through it
(0444 is written as 0400).
internal/netproxy TestBunProxyLoginWithReservedCharacters: as above
(#3397), and red the same on f3eb9292 without this change.
Not tried: macOS (a real scutil, hung or not) and Windows (registry
errors). The tool test runs echo and sleep on Linux in scutil's place.
liu_dyj: with the VPN on, magpie said Codex reached magpie through a
proxy that didn't pass its requests on; versions before f156d9046 had
no such check, which is why upgrading brought it.
Cause: codexProxyFor didn't read the proxy the way Codex does.
- It read magpie's own environment. Codex in a terminal has the login
shell's, and the ChatGPT app's Codex too (ChatGPT.app 26.1007 runs
$SHELL -ilc at start and Object.assign()s its env into process.env).
magpie opened from the Dock has neither, so a NO_PROXY=127.0.0.1 in
.zshrc, which keeps Codex direct, wasn't seen, and magpie probed the
system proxy and warned.
- For the system proxy it took netproxy's one URL (HTTPS over HTTP over
SOCKS). hyper-util 0.1.20's Matcher::from_system, which reqwest
0.12.28 uses for Codex, takes only macOS' HTTP entry for an http://
URL; with only the HTTPS or SOCKS entry set (ShadowsocksX-NG's global
mode is SOCKS only) Codex goes direct and magpie warned. On Windows
it takes ProxyServer as one address, which a per-protocol list
("http=..;https=..") isn't (http's Authority::parse refuses two
colons), so Codex goes direct there too.
- With respect_system_proxy on macOS, CFNetwork's SOCKS entry is
UnsupportedProxyScheme, and Codex falls back to HTTP_PROXY/ALL_PROXY
with NO_PROXY (codex-rs outbound_proxy.rs resolve_env_proxy_route);
magpie used the SOCKS URL.
- The probe spoke real SOCKS5 to a socks5:// proxy. Codex's reqwest is
built without SOCKS (no tokio-socks in codex-rs Cargo.lock) and sends
an absolute-URI HTTP request to it, so the probe now does the same:
it passes a Clash mixed port as Codex does and fails a SOCKS-only
port as Codex does.
So netproxy.Proxy carries the HTTP and SOCKS entries alone beside URL
(parseScutil, parseWindows), proc asks the login shell for the
*_PROXY variables with agentenv.Vars in the same probe and only reads
them (proc.LoginEnv; never set in magpie's env, whose requests use
netproxy), and codexProxyFor uses both. Failover's gate
(codexProxyOK) and the drift share codexProxyFor and proxyProbe, so
both follow. The pill's How to fix now names the shell profile for
NO_PROXY. Clash/mihomo rule mode with a 127.0.0.0/8 DIRECT rule never
warned and still doesn't; global mode or a rule set without it still
warns, rightly (real codex 502s there). TUN mode sets no proxy and
never warned.
Run:
- real codex-cli 0.162.1 in a sandbox HOME/CODEX_HOME against a fake
gateway and mihomo v1.19.32 (rule, no-LAN-rule, global ports):
direct, rule, and ALL_PROXY=socks5:// on the rule port reach the
gateway; no-LAN-rule and global 502; global with NO_PROXY=127.0.0.1
reaches it. The Go probe agrees on every one.
- go test -tags nogui ./internal/agent, ./internal/netproxy,
./internal/proc; gofmt -l; GOOS=linux and GOOS=windows go vet
-tags nogui on the three packages.
- Broke each part by hand: sys.URL back in codexProxyFor (Windows per
protocol, macOS HTTPS/SOCKS alone red), the probe's socks->http
(TestRealProxyProbe red), codexEnv without the login shell
(TestCodexEnvTakesLoginShell red), the respect-path SOCKS fallback
(three respect cases red), setUnset of every shell var and the probe
without ProxyVars (TestLoginEnvProxyVarsNotSet red).
Not tried: the macOS system proxy cases with real Codex (changing the
system's proxy settings is off limits here; read from hyper-util,
reqwest and codex-rs sources), the Windows per-protocol ProxyServer
with real Codex (from the http crate's source; no Windows run), and
the reporter's own app and mode, which are asked on X.