Skip to content

telegram: ingress worker exit code 1 on stop wedges channel during config hot reload #83008

Closed

Description

@zqchris

Summary

Any channels.telegram.* config change triggers gateway hot reload → telegram channel reload → ingress worker exits with code 1 (instead of 0) on graceful stop → channel supervisor leaves the account in running: true state and the subsequent startChannel is skipped. The channel is wedged silently (no inbound, no outbound, healthState: "stale-socket" later) and only recovers after a full launchctl bootout+bootstrap of the gateway.

Reproduced on v2026.5.16-beta.4 (release cf10f1ec64) with extensions/telegram isolated polling ingress enabled. Likely present back to whenever telegram-ingress-worker.runtime.ts was introduced.

Reproduction

  1. Run the gateway with channels.telegram.accounts.*.botToken set and isolated polling ingress active (default).
  2. Have at least one in-flight tool call / embedded run on the telegram account so the reload waits to drain.
  3. Trigger any channels.telegram.* config change. The easiest is the message action's config.patch, e.g. flipping channels.telegram.replyToMode between quote and none from an agent — but any path that lands channels.telegram.* does it.
  4. Observe in gateway.log:
    [reload] config change detected; evaluating reload (channels.telegram.replyToMode, …)
    [reload] channel reload still deferred after 30448ms with 2 operation(s), …
    [reload] active operations and replies completed; reloading channels now
    [gateway/channels] restarting telegram channel
    [telegram] [default] released stopped Telegram polling lease
    [telegram] [ivy]     released stopped Telegram polling lease
    [telegram] [default] channel stop exceeded 5000ms after abort; continuing shutdown
    [telegram] [ivy]     channel stop exceeded 5000ms after abort; continuing shutdown
    [reload] config hot reload applied (channels.telegram.replyToMode, …)
    [telegram] [ivy]     channel exited: Telegram ingress worker exited with code 1
    [telegram] [default] channel exited: Telegram ingress worker exited with code 1
    
  5. From this point on, telegram channel is dead: no inbound, no outbound. pnpm openclaw gateway call health --json returns running: false, healthState: "not-running" for the affected accounts. No auto-restart, no further log lines from the telegram subsystem.
  6. Only fix: restart the whole gateway process.

Root cause

Three pieces interact:

1. Ingress worker exits with code 1 on graceful stop

extensions/telegram/src/telegram-ingress-worker.runtime.ts:

main()
  .then(() => undefined)
  .catch((err) => {
    post({ type: "poll-error", message: formatErrorMessage(err), finishedAt: Date.now() });
    process.exitCode = 1;
  });

When parentPort receives { type: "stop" }, the handler sets stopped = true and activeController.abort(...). The in-flight fetch rejects with the abort error; the inner catch (err) block's if (stopped) break; exits the loop cleanly. So far so good.

But the cleanup finally { await transport.close(); } can throw (most often when undici dispatcher pools are torn down mid-request — common under abort). That throw escapes through the finally, main() rejects, the top-level catch fires, and process.exitCode = 1. This is treated as a crash by the worker host:

extensions/telegram/src/telegram-ingress-worker.ts:55-61:

worker.once("exit", (code) => {
  if (code === 0) {
    resolve();
    return;
  }
  reject(new Error(`Telegram ingress worker exited with code ${code}`));
});

2. runIsolatedIngressCycle does not catch the rejected worker.task()

extensions/telegram/src/polling-session.ts #runIsolatedIngressCycle:

try {
  await worker.task();   // ← rejects with "Telegram ingress worker exited with code 1"
  if (this.opts.abortSignal?.aborted) {
    return "exit";
  }
  …
  return shouldRestart ? "continue" : "exit";
} finally {
  clearInterval(drainTimer);
  …
}

The try has no catch — the rejection bubbles up through runUntilAbort's while loop (also no catch) and out of the polling-session task promise. The reject value: Error: Telegram ingress worker exited with code 1.

3. Supervisor stop helper times out, then startChannel no-ops

src/gateway/server-channels.ts:720-738 in the stopAllAccounts / per-account stop helper:

const stoppedCleanly = await waitForChannelStopGracefully(task, CHANNEL_STOP_ABORT_TIMEOUT_MS); // 5_000
if (!stoppedCleanly) {
  log.warn?.(`[${id}] channel stop exceeded ${CHANNEL_STOP_ABORT_TIMEOUT_MS}ms after abort; continuing shutdown`);
  setRuntime(channelId, id, {
    accountId: id,
    running: manual,       // ← stays true on manual stop
    restartPending: !manual,
    lastError: `channel stop timed out after ${CHANNEL_STOP_ABORT_TIMEOUT_MS}ms`,
  });
  …
  return;  // store.tasks / store.aborts NOT cleared
}
// only clean stop reaches here:
store.aborts.delete(id);
store.tasks.delete(id);
setRuntime(channelId, id, { running: false, … });

The hot-reload supervisor calls stopChannel with manual = true. Because worker.stop() gives the worker up to 15 s grace (telegram-ingress-worker.ts setTimeout(() => worker.terminate(), 15_000)), the worker frequently doesn't finish in 5 s — and even when it does, the eventual non-zero exit shows up as a rejected task promise after the supervisor has already returned with running: true and stale store.tasks / store.aborts.

Then in src/gateway/server-reload-handlers.ts:419-423:

params.logChannels.info(`restarting ${name} channel`);
if (!channelsStoppedBeforePluginReload.has(name)) {
  await params.stopChannel(name);
}
await params.startChannel(name);

startChannel runs against state that says running: true and finds an existing task in store.tasks. Effective no-op. Channel is wedged.

The auto-restart path (server-channels.ts:567+ .then(restart) → MAX_RESTART_ATTEMPTS) cannot save us either, because hot reload sets manuallyStopped.has(rKey) === true and the auto-restart block returns early on that condition.

Impact

  • Severity: high in practice. Any user (or agent calling config.patch for the user) toggling a channels.telegram.* setting silently breaks their own telegram channel until they notice and restart the gateway. We hit this when an agent flipped replyToMode from quote to none per user request.
  • Detection: weak. Gateway health shows healthState: "stale-socket" after a while, but the supervisor has no actor that bounces a stale-socket channel — the existing polling stall detector (polling-liveness.ts) only fires when the polling loop is still ticking. Here the loop is gone.
  • Recovery: gateway-wide bounce only. No per-channel CLI restart is currently exposed.

Proposed fix

Smallest, most targeted change — fix the worker exit code so cleanup drift after stop doesn't masquerade as a crash:

--- a/extensions/telegram/src/telegram-ingress-worker.runtime.ts
+++ b/extensions/telegram/src/telegram-ingress-worker.runtime.ts
@@ main()
   .then(() => undefined)
   .catch((err) => {
     post({ type: "poll-error", message: formatErrorMessage(err), finishedAt: Date.now() });
-    process.exitCode = 1;
+    // If a stop was requested, surface the error to the parent but exit
+    // cleanly: shutdown drift (e.g. transport.close throwing in finally)
+    // is not a crash, and exit code 1 makes the channel supervisor treat
+    // the worker as crashed, which on hot reload leaves the supervisor in
+    // a "still running" state and skips startChannel — wedging telegram
+    // until the gateway is bounced.
+    process.exitCode = stopped ? 0 : 1;
   });

With this fix, worker.task() resolves cleanly under requested-stop, runIsolatedIngressCycle reaches the abortSignal.aborted → return "exit" branch, the supervisor's stopHelper sees a settled task in time, clears store.tasks/store.aborts and sets running: false, then startChannel starts a fresh ingress worker.

Optional follow-ups (defence in depth, not required for this regression)

  1. Wrap await worker.task() in #runIsolatedIngressCycle with a try/catch that treats abortSignal.aborted as clean and otherwise logs + #waitBeforeRestart (so a real worker crash also triggers the restart cycle instead of throwing out of runUntilAbort).
  2. In src/gateway/server-channels.ts, when the per-account stop times out with manual: true, still clear store.tasks/store.aborts after a longer hard deadline so a subsequent startChannel is guaranteed to start from a clean slate.
  3. Add a supervisor that reacts to a sustained healthState: "stale-socket" (e.g. > 60 s without transport activity) by bouncing the affected account, similar to the polling stall detector but covering the cycle-not-running case.

Tests

The worker runtime currently has no direct unit tests under extensions/telegram/src/. A small test that asserts process.exitCode === 0 when main() rejects after stopped === true would catch any regression. I'm happy to add it in a PR if the fix above looks right.

Environment

  • Gateway version: v2026.5.16-beta.4
  • Platform: macOS 26.3 (Darwin 25.4.0), Node 22, pnpm 11.1, single-host launchd-managed gateway.
  • Channels: telegram (two accounts default+ivy), feishu, imessage.
  • Repro confirmed with isolated polling ingress (default).

Activity

  1. clawsweeper commented on May 17, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve.

    Summary
    Keep open. Current main still has the reported shutdown/reload ingredients: isolated Telegram ingress worker failures propagate out of the polling session, gateway hot reload stops then immediately starts the channel, and the channel manager intentionally leaves stale task state in place when a manual stop times out.

    Reproducibility: no. live high-confidence reproduction was run in this read-only review. Source inspection and existing tests show a narrow repro path with an injected isolated ingress worker or timed-out channel task during channels.telegram.* hot reload.

    Next step
    The report is fresh, source-backed, and narrow enough for a focused Telegram/gateway lifecycle fix, but the repair should prove the exact failure path before changing behavior.

    Review details

    Best possible solution:

    Add a focused regression around Telegram isolated-ingress reload shutdown, then fix the owner boundary so requested-stop worker failures and manual stop timeouts cannot leave stale channel task state blocking the reload restart.

    Do we have a high-confidence way to reproduce the issue?

    No live high-confidence reproduction was run in this read-only review. Source inspection and existing tests show a narrow repro path with an injected isolated ingress worker or timed-out channel task during channels.telegram.* hot reload.

    Is this the best way to solve the issue?

    Unclear that the proposed one-line worker exit-code change is the whole fix. A safer fix should first add a failing regression, then either treat requested-stop worker rejection as clean, catch abort-time worker failures in #runIsolatedIngressCycle, or make hot-reload stop timeout cleanup restartable without weakening the duplicate-start guard.

    Acceptance criteria:

    • node scripts/run-vitest.mjs extensions/telegram/src/polling-session.test.ts
    • node scripts/run-vitest.mjs src/gateway/server-channels.test.ts
    • node scripts/run-vitest.mjs src/gateway/server.reload.test.ts
    • For broader pre-merge proof after code changes, use Testbox/Crabbox for pnpm check:changed rather than direct local pnpm in a Codex worktree.

    What I checked:

    Likely related people:

    • Peter Steinberger: Blame and local log history for the central Telegram isolated ingress and gateway channel supervisor paths point to the current-main snapshot commit authored by Peter Steinberger. (role: recent area contributor; confidence: medium; commits: a535978352ca; files: extensions/telegram/src/polling-session.ts, extensions/telegram/src/telegram-ingress-worker.runtime.ts, extensions/telegram/src/telegram-ingress-worker.ts)

    Remaining risk / open question:

    • I did not run the live Telegram hot-reload scenario; the source path is credible, but the exact transport.close() failure mechanism in the issue body is not confirmed because the current/stable transport close path swallows dispatcher destroy failures.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against 37806afd2d9f.

  2. added
    P1
    impact:message-loss
    clawsweeper:needs-live-repro
    on May 17, 2026
  3. self-assigned this
    on May 18, 2026
  4. joshavant commented on May 18, 2026

    @joshavant
    Collaborator

    Thanks for the detailed report, @zqchris.

    This is fixed by #83410, which has now been merged as 395bd57.

    We reproduced the reported breakage on current main with a live Telegram setup: a channels.telegram.* hot reload during an in-flight Telegram turn could defer, stop the channel, hit the stop-timeout path, then leave polling wedged after the isolated ingress worker exited with code 1. The fix ended up being broader than only changing the worker exit code: hot-reload channel stops now use restart/recovery semantics instead of marking the channel manually stopped, and Telegram isolated ingress now distinguishes requested shutdown, recoverable worker failure, and fatal Telegram API errors.

    After the patch, AWS Crabbox live proof showed the reload applying, isolated ingress starting again, the delayed in-flight Telegram reply arriving, and a post-reload /status reply working, with no stop-timeout log and no worker exit-code-1 log. Focused regression tests were also added for the gateway reload stop semantics and Telegram isolated ingress worker behavior.

    Closing this as fixed.

  5. locked as resolved and limited conversation to collaborators on May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1clawsweeper:needs-live-reproimpact:message-loss

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions