Skip to content

applyTelegramNetworkWorkarounds overwrites proxy-aware undici dispatcher, breaking LLM requests and Telegram API calls through HTTP proxy #30338

@C3F

Description

@C3F

Bug Description

When running OpenClaw Gateway with an HTTP proxy (e.g., on WSL2, macOS, or any system behind a regional/corporate proxy), all LLM requests and Telegram API calls fail after the Telegram channel initializes.

Root cause: At startup, a proxy-aware ProxyAgent is set as the global undici dispatcher (either via EnvHttpProxyAgent in http-proxy.js, or via a user-provided --require script using NODE_OPTIONS). About 10 seconds later, the Telegram channel calls applyTelegramNetworkWorkarounds (defined in src/telegram/fetch.ts, compiled into multiple dist chunks), which unconditionally calls:

```js
setGlobalDispatcher(new Agent({ connect: { autoSelectFamily: ..., ... } }));
```

This replaces the proxy-aware dispatcher with a plain Agent, causing all subsequent fetch calls to bypass the proxy. Since globalThis.fetch in Node.js 18+ respects the undici global dispatcher, this affects both:

  • LLM requests to providers like Anthropic — they time out because the API is unreachable without the proxy
  • Telegram's own API calls (deleteWebhook, setMyCommands, getUpdates, etc.) — they fail if api.telegram.org also requires a proxy (e.g., in mainland China)

Environment

  • OpenClaw version: 2026.2.26
  • Platform: WSL2 (Ubuntu 24.04) with mirrored networking / macOS — any platform where HTTPS_PROXY is set
  • Proxy: HTTP proxy set via HTTPS_PROXY / HTTP_PROXY env var

Steps to Reproduce

  1. Set HTTPS_PROXY=http://... in the Gateway environment
  2. Enable Telegram channel
  3. Start Gateway — LLM requests succeed initially
  4. After Telegram initializes (~10s), send an LLM request — it times out
  5. (On systems where api.telegram.org requires a proxy): Telegram channel also fails entirely with repeated Network request for 'deleteWebhook' failed! / Network request for 'setMyCommands' failed! errors

Expected Behavior

The setGlobalDispatcher call in the Telegram workaround should preserve proxy settings when HTTPS_PROXY/HTTP_PROXY is set.

Additional Notes: Bug Exists in Multiple Compiled Chunks

applyTelegramNetworkWorkarounds is compiled into at least 5 separate dist files:

dist/send-C4dK3vop.js
dist/send-k3kiiQd1.js
dist/send-CVJNwTGe.js
dist/send-6vB8BriV.js
dist/plugin-sdk/send-Wiujuiup.js

The fix must be applied in the source (src/telegram/fetch.ts) so all output chunks are patched consistently. Patching only one dist file will not fix the issue.

Suggested Fix

```js
// Before
import { Agent, setGlobalDispatcher } from "undici"
// ...
setGlobalDispatcher(new Agent({ connect: { autoSelectFamily: ..., autoSelectFamilyAttemptTimeout: 300 } }));

// After
import { Agent, ProxyAgent, setGlobalDispatcher } from "undici"
// ...
const _proxyUrl = process.env.HTTPS_PROXY || process.env.https_proxy
|| process.env.HTTP_PROXY || process.env.http_proxy;
if (_proxyUrl) {
setGlobalDispatcher(new ProxyAgent({ uri: _proxyUrl, connect: { autoSelectFamily: ..., autoSelectFamilyAttemptTimeout: 300 } }));
} else {
setGlobalDispatcher(new Agent({ connect: { autoSelectFamily: ..., autoSelectFamilyAttemptTimeout: 300 } }));
}
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions