Skip to content

fix(telegram): avoid overriding undici dispatcher when proxy env is set#30406

Closed
edoublezh wants to merge 2 commits intoopenclaw:mainfrom
edoublezh:fix/telegram-proxy-dispatcher
Closed

fix(telegram): avoid overriding undici dispatcher when proxy env is set#30406
edoublezh wants to merge 2 commits intoopenclaw:mainfrom
edoublezh:fix/telegram-proxy-dispatcher

Conversation

@edoublezh
Copy link
Copy Markdown

Summary

In proxy-enabled environments, Telegram startup currently overrides the global undici dispatcher to apply autoSelectFamily.
This can unintentionally bypass or conflict with existing proxy dispatcher settings and lead to request failures like fetch failed in embedded runs.

This PR changes Telegram network workaround behavior:

  • Detect proxy environment variables (HTTPS_PROXY, HTTP_PROXY, ALL_PROXY, including lowercase variants).
  • If proxy env is configured, skip setGlobalDispatcher(new Agent(...)).
  • Keep existing autoSelectFamily and DNS result-order handling.
  • Add tests to ensure dispatcher override is skipped when proxy env is present.

Why

When users rely on a proxy, replacing the global dispatcher in Telegram path can break other request paths that depend on proxy-aware dispatch behavior.

Changes

  • src/telegram/fetch.ts
    • Added proxy-env detection.
    • Guarded global dispatcher override behind proxy check.
  • src/telegram/fetch.test.ts
    • Added test coverage for proxy-env behavior.
    • Kept existing dispatcher behavior tests for non-proxy cases.

Verification

  • pnpm build
  • pnpm vitest run src/telegram/fetch.test.ts (12 passed)
  • Local gateway runtime check with copied real config:
    • Telegram startup log shows:
      • skip global undici dispatcher autoSelectFamily override because proxy env is configured
    • chat.send run completed with isError=false

Impact

  • Proxy setups: safer behavior, avoids clobbering global proxy dispatcher.
  • Non-proxy setups: existing workaround remains unchanged.

@openclaw-barnacle openclaw-barnacle bot added channel: telegram Channel integration: telegram size: S labels Mar 1, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3461734fb8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 59 to 62
if (hasProxyEnvConfigured()) {
log.info(
"skip global undici dispatcher autoSelectFamily override because proxy env is configured",
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve autoSelectFamily override for explicit disable paths

This unconditional proxy-env guard skips setGlobalDispatcher(...) even when autoSelectFamily is explicitly resolved to false (WSL2 default, config, or env override). In processes that already touched globalThis.fetch before Telegram initialization, the file itself notes that net.setDefaultAutoSelectFamily() no longer affects fetch’s existing undici agent, so the disable decision is silently ignored whenever proxy env vars are present. That reintroduces the stale-agent behavior this workaround was added to prevent for those environments.

Useful? React with 👍 / 👎.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 1, 2026

Greptile Summary

Added proxy environment detection to prevent overriding the global undici dispatcher when proxy is configured. The implementation correctly checks for common proxy env variables (HTTPS_PROXY, HTTP_PROXY, ALL_PROXY with case variants) and skips the autoSelectFamily dispatcher override when detected.

Key Changes:

  • Added hasProxyEnvConfigured() helper that checks for proxy environment variables
  • Modified applyTelegramNetworkWorkarounds() to skip global dispatcher override when proxy is detected
  • Added test coverage for proxy detection behavior
  • Updated existing tests to clear proxy env for consistent test state

Issue Found:

  • State tracking variable not updated when skipping dispatcher override, causing repeated log messages

Confidence Score: 4/5

  • Safe to merge with minor logging inefficiency that should be fixed
  • The core functionality is correct and addresses a real issue with proxy configurations. The implementation correctly detects proxy environment variables and prevents dispatcher override. Test coverage is adequate. The identified issue is a minor logging inefficiency, not a functional bug - the dispatcher override is still correctly skipped when proxy is configured.
  • Pay attention to src/telegram/fetch.ts line 59-62 for the state tracking fix

Last reviewed commit: 3461734

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines 59 to 62
if (hasProxyEnvConfigured()) {
log.info(
"skip global undici dispatcher autoSelectFamily override because proxy env is configured",
);
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.

appliedGlobalDispatcherAutoSelectFamily never updated when proxy is detected, causing the condition on line 56 to remain true and this log to repeat on every resolveTelegramFetch() call

Suggested change
if (hasProxyEnvConfigured()) {
log.info(
"skip global undici dispatcher autoSelectFamily override because proxy env is configured",
);
if (hasProxyEnvConfigured()) {
log.info(
"skip global undici dispatcher autoSelectFamily override because proxy env is configured",
);
appliedGlobalDispatcherAutoSelectFamily = autoSelectDecision.value;
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/fetch.ts
Line: 59-62

Comment:
`appliedGlobalDispatcherAutoSelectFamily` never updated when proxy is detected, causing the condition on line 56 to remain true and this log to repeat on every `resolveTelegramFetch()` call

```suggestion
    if (hasProxyEnvConfigured()) {
      log.info(
        "skip global undici dispatcher autoSelectFamily override because proxy env is configured",
      );
      appliedGlobalDispatcherAutoSelectFamily = autoSelectDecision.value;
```

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review. I pushed a follow-up commit (930f8f1) to address the state tracking issue when proxy env is present and added test coverage for the proxy -> no-proxy transition.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 930f8f16a0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +60 to +61
if (hasProxyEnvConfigured()) {
if (skippedGlobalDispatcherAutoSelectFamily !== autoSelectDecision.value) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor explicit autoSelectFamily disable under proxy env

applyTelegramNetworkWorkarounds now unconditionally skips setGlobalDispatcher(...) whenever a proxy env var is set, even when autoSelectDecision.value is false. That regresses explicit disable paths (OPENCLAW_TELEGRAM_DISABLE_AUTO_SELECT_FAMILY, network.autoSelectFamily=false, or WSL2 default): only net.setDefaultAutoSelectFamily(false) is applied, but this same file notes that an already-created globalThis.fetch agent ignores that setter, so processes that touched fetch before Telegram init can keep the old dispatcher behavior and still hit the IPv6 failure mode this workaround was meant to avoid.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review. I pushed a follow-up commit (930f8f16a).

I intentionally kept the proxy guard behavior unchanged (still skipping the global dispatcher override when proxy env is configured), to avoid clobbering proxy-aware dispatcher behavior.

I also added follow-up coverage around the proxy -> no-proxy transition.

@steipete
Copy link
Copy Markdown
Contributor

steipete commented Mar 2, 2026

Closing as duplicate of #30367, which is already merged.

Keeping discussion and fix history in #30367 to avoid split tracking.

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

Labels

channel: telegram Channel integration: telegram size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants