Skip to content

fix(gateway): bootstrap env proxy dispatcher at startup - #71833

Closed
mjamiv wants to merge 1 commit into
openclaw:mainfrom
mjamiv:fix/gateway-env-proxy-bootstrap
Closed

mjamiv wants to merge 1 commit into
openclaw:mainfrom
mjamiv:fix/gateway-env-proxy-bootstrap

Conversation

@mjamiv

@mjamiv mjamiv commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • install the global Undici env proxy dispatcher at gateway startup
  • keep the existing embedded-runner timeout/proxy wrapping behavior unchanged
  • add a startup regression test for gateway-side provider requests

Why

Gateway-side provider/plugin paths can execute before an embedded agent attempt has installed the env proxy dispatcher. In proxy-only environments this leaves OpenAI SDK traffic without the configured HTTP(S) proxy and surfaces as LLM request failed: network connection error / Connection error, even though credentials resolve correctly.

Validation

  • pnpm exec vitest run --config vitest.gateway.config.ts src/gateway/server-startup.test.ts --pool=forks
  • pnpm exec oxlint --type-aware src/gateway/server.impl.ts src/gateway/server-startup.ts src/gateway/server-startup.test.ts
  • pnpm exec oxfmt --check src/gateway/server.impl.ts src/gateway/server-startup.ts src/gateway/server-startup.test.ts

Canary

Validated the same fix adapted onto an OpenClaw 2026.4.24 Atlas sandbox canary:

  • openai/gpt-5.5 agent smoke returned atlas 4.24 proxy bootstrap ok
  • openai/gpt-5.4-mini gateway model smoke returned atlas 4.24 mini smoke ok
  • no fallback was used for the gpt-5.5 smoke

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels Apr 25, 2026
@greptile-apps

greptile-apps Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a proxy-bypass bug in proxy-only environments by calling ensureGlobalUndiciEnvProxyDispatcher() at gateway startup (via the new bootstrapGatewayNetworkEnvironment wrapper) before any provider or plugin code runs, ensuring OpenAI SDK traffic is routed through the configured HTTP(S) proxy. The implementation is straightforward and the idempotency logic in undici-global-dispatcher.ts handles repeated calls correctly.

Confidence Score: 4/5

Safe to merge — the fix is minimal and well-scoped; only P2 style observations remain.

P2-only findings: the test covers the extracted function but not the call site in startGatewayServer, and bootstrapGatewayNetworkEnvironment is exposed redundantly in both the public API and __testing. Neither affects correctness or the intended behavior of the fix.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/gateway/server-startup.test.ts
Line: 58-64

Comment:
**Test only covers the function, not the call site**

`bootstrapGatewayNetworkEnvironment` is verified in isolation here, but the actual regression being fixed (the function not being called at gateway startup) lives in `server.impl.ts`. If `bootstrapGatewayNetworkEnvironment()` is removed from `startGatewayServer`, this test would still pass. A test that mocks `bootstrapGatewayNetworkEnvironment` and verifies it is called during `startGatewayServer` would fully close the regression loop.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/gateway/server-startup.ts
Line: 231-235

Comment:
**`__testing` entry is redundant with the public export**

`bootstrapGatewayNetworkEnvironment` is already a named `export` on line 39, so exposing it again via `__testing` is redundant. The other entry in `__testing` (`prewarmConfiguredPrimaryModel`) is a private function not exported at module scope, which is why it needs `__testing`. Consider either removing the `__testing` entry for `bootstrapGatewayNetworkEnvironment`, or keeping the function private and only exporting it through `__testing` for consistency.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(gateway): bootstrap env proxy dispat..." | Re-trigger Greptile

Comment on lines +58 to +64
it("installs the env proxy dispatcher for gateway-side provider requests", async () => {
const { __testing } = await import("./server-startup.js");

__testing.bootstrapGatewayNetworkEnvironment();

expect(ensureGlobalUndiciEnvProxyDispatcherMock).toHaveBeenCalledTimes(1);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test only covers the function, not the call site

bootstrapGatewayNetworkEnvironment is verified in isolation here, but the actual regression being fixed (the function not being called at gateway startup) lives in server.impl.ts. If bootstrapGatewayNetworkEnvironment() is removed from startGatewayServer, this test would still pass. A test that mocks bootstrapGatewayNetworkEnvironment and verifies it is called during startGatewayServer would fully close the regression loop.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/server-startup.test.ts
Line: 58-64

Comment:
**Test only covers the function, not the call site**

`bootstrapGatewayNetworkEnvironment` is verified in isolation here, but the actual regression being fixed (the function not being called at gateway startup) lives in `server.impl.ts`. If `bootstrapGatewayNetworkEnvironment()` is removed from `startGatewayServer`, this test would still pass. A test that mocks `bootstrapGatewayNetworkEnvironment` and verifies it is called during `startGatewayServer` would fully close the regression loop.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines 231 to 235
}

export const __testing = {
bootstrapGatewayNetworkEnvironment,
prewarmConfiguredPrimaryModel,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 __testing entry is redundant with the public export

bootstrapGatewayNetworkEnvironment is already a named export on line 39, so exposing it again via __testing is redundant. The other entry in __testing (prewarmConfiguredPrimaryModel) is a private function not exported at module scope, which is why it needs __testing. Consider either removing the __testing entry for bootstrapGatewayNetworkEnvironment, or keeping the function private and only exporting it through __testing for consistency.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/server-startup.ts
Line: 231-235

Comment:
**`__testing` entry is redundant with the public export**

`bootstrapGatewayNetworkEnvironment` is already a named `export` on line 39, so exposing it again via `__testing` is redundant. The other entry in `__testing` (`prewarmConfiguredPrimaryModel`) is a private function not exported at module scope, which is why it needs `__testing`. Consider either removing the `__testing` entry for `bootstrapGatewayNetworkEnvironment`, or keeping the function private and only exporting it through `__testing` for consistency.

How can I resolve this? If you propose a fix, please make it concise.

@steipete

Copy link
Copy Markdown
Contributor

Thanks for the focused fix. I rewrote this on current main because the Gateway startup modules had moved since this branch was opened: src/gateway/server-startup.ts is now only a barrel, so the original patch no longer applied cleanly.

Landed equivalent fix in d513dc7:

  • installs the env proxy bootstrap at the startGatewayServer() boundary, before Gateway-owned provider/plugin network work can run
  • keeps the existing defensive ensureGlobalUndiciEnvProxyDispatcher() behavior: no-op without proxy env, idempotent, and does not override unsupported custom dispatchers
  • adds direct Gateway startup regression coverage proving HTTPS_PROXY turns the process-wide Undici dispatcher into EnvHttpProxyAgent
  • preserves your contribution with changelog credit and a co-author trailer

Closing this PR as superseded by the landed main commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants