Skip to content

fix(telegram): release polling leases at in-process gateway restart boundary - #81533

Closed
SymbolStar wants to merge 1 commit into
openclaw:mainfrom
SymbolStar:fix/issue-81507
Closed

SymbolStar wants to merge 1 commit into
openclaw:mainfrom
SymbolStar:fix/issue-81507

Conversation

@SymbolStar

Copy link
Copy Markdown
Contributor

Fixes #81507

Root cause

Telegram polling leases live on a process-global Symbol.for("openclaw.telegram.pollingLeases") Map. The registry survives any gateway restart that recycles the lifecycle without exiting the Node process (SIGUSR1 in-process restart, OPENCLAW_NO_RESPAWN reload, etc.).

When the previous lifecycle's monitorTelegramProvider task is dropped before its finally pollingLease.release() runs, the entry stays. The next lifecycle then calls acquireTelegramPollingLease(), finds a same-token entry whose abort signal hasn't observed an abort yet, and throws the duplicate-poller error indefinitely. This matches the reporter's "855 errors/day" symptom.

Fix

Lifecycle-owned cleanup at the in-process restart boundary, per Codex review.

  1. New src/infra/in-process-restart-hooks.ts — small process-global registry of synchronous, idempotent reset hooks. Hooks are best-effort (errors swallowed) and the registry is stored on a Symbol.for(...) key so it deduplicates across module reloads.
  2. New openclaw/plugin-sdk/lifecycle-restart-hooks re-export so plugins can register without importing internal infra.
  3. extensions/telegram/src/polling-lease.ts — exports releaseTelegramPollingLeasesForLifecycleReset() and self-registers it on first module load (process-symbol-guarded so duplicate ESM loads don't double-register).
  4. src/cli/gateway-cli/run-loop.ts — invokes runInProcessRestartHooks() from the existing restart iteration hook, alongside resetAllLanes() / resetGatewayRestartStateForInProcessRestart() / reloadTaskRegistryFromStore().

The cleanup is only invoked at the in-process restart boundary. Steady-state acquireTelegramPollingLease() still rejects two live pollers for the same bot token within one lifecycle. Late release() calls from dropped previous-lifecycle tasks are safe — release() is owner-checked and idempotent, so they won't delete a fresh lease that the new lifecycle just acquired.

Tests

  • extensions/telegram/src/polling-lease.test.ts — three new regressions:
    • stale same-token leases get cleared at the restart boundary, fresh acquire succeeds, and a late stale release() does NOT delete the fresh lease;
    • two concurrent live pollers within one lifecycle still rejected (same-token guard preserved);
    • the cleanup is wired into the shared in-process restart hook registry (no Telegram-specific knowledge needed at the call site).
  • src/cli/gateway-cli/run-loop.test.ts — adds runInProcessRestartHooks mock + assertion that it's called once per restart iteration alongside the existing lane/registry resets.

Verification

  • pnpm test extensions/telegram/src/polling-lease.test.ts
  • pnpm test src/cli/gateway-cli/run-loop.test.ts

(Local pnpm not available — relying on CI for the full run.)

…oundary

The Telegram polling lease registry lives on a process-global Symbol so it
survives gateway restarts that recycle the lifecycle without exiting Node
(SIGUSR1 in-process restart, OPENCLAW_NO_RESPAWN reload). When the previous
lifecycle's monitor task is dropped before its `finally` releases the lease,
the next lifecycle's `acquireTelegramPollingLease()` sees a stale same-token
entry and rejects every Telegram account with the duplicate-poller error
indefinitely.

Add a generic `in-process-restart-hooks` registry on `process` and run it
from the gateway run-loop's restart iteration hook, alongside the existing
`resetAllLanes` / `resetGatewayRestartStateForInProcessRestart` /
`reloadTaskRegistryFromStore` calls. Telegram's polling-lease module
self-registers a hook on first load that drains the lease registry. Late
`release()` calls from dropped previous-lifecycle tasks remain safe (release
is owner-checked and idempotent), and the same-lifecycle live duplicate guard
is unchanged.

Regression tests cover both the cleanup behavior and the same-lifecycle guard,
plus the run-loop wiring.

Fixes openclaw#81507
@github-actions github-actions Bot added the dependencies-changed PR changes dependency-related files label May 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Dependency Changes Detected

This PR changes dependency-related files. Maintainers should confirm these changes are intentional.

Changed files:

  • package.json

Maintainer follow-up:

  • Review whether the dependency changes are intentional.
  • Inspect resolved package deltas when lockfile or workspace dependency policy changes are present.
  • Run pnpm deps:changes:report -- --base-ref origin/main --markdown /tmp/dependency-changes.md --json /tmp/dependency-changes.json locally for detailed release-style evidence.

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram cli CLI command changes size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 13, 2026
@clawsweeper

clawsweeper Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Summary
The branch adds a generic in-process restart hook registry, exposes it through openclaw/plugin-sdk/lifecycle-restart-hooks, runs it from the gateway restart loop, and registers Telegram polling leases for restart-boundary cleanup with regression tests.

Reproducibility: Do we have a high-confidence way to reproduce the issue? Partly: the package/SDK review finding is source-reproducible from current-main contracts, and the Telegram lease leak has a clear source path, but I did not establish a live in-process gateway restart reproduction in this read-only review.

Real behavior proof
Needs real behavior proof before merge: The PR body lists tests and says local pnpm was unavailable, but it needs redacted after-fix gateway/Telegram restart logs, terminal output, recording, or linked artifact; updating the PR body should trigger re-review, otherwise ask @clawsweeper re-review. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, ask a maintainer to comment @clawsweeper re-review.

Next step before merge
Contributor or maintainer action is needed for SDK contract fixes plus real behavior proof; ClawSweeper should not queue an automated repair while the contributor proof gate is missing.

Security
Cleared: No dependency, workflow, secret, lifecycle-script, or downloaded-artifact change was found; the package.json change is an export-surface issue covered by the functional findings.

Review findings

  • [P1] Add the SDK subpath to the entrypoint manifest — package.json:343-346
  • [P2] Expose only registration through the SDK — src/plugin-sdk/lifecycle-restart-hooks.ts:6-10
Review details

Best possible solution:

Land a revised fix that keeps the restart drain core-owned, exposes only the minimal plugin registration seam if a public SDK subpath is needed, aligns the SDK entrypoint manifest/package exports/API contracts, and includes redacted real restart proof.

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

Do we have a high-confidence way to reproduce the issue? Partly: the package/SDK review finding is source-reproducible from current-main contracts, and the Telegram lease leak has a clear source path, but I did not establish a live in-process gateway restart reproduction in this read-only review.

Is this the best way to solve the issue?

Is this the best way to solve the issue? Not yet: restart-boundary cleanup is the right general shape, but the PR should use the existing SDK manifest/build contracts and avoid making global drain/test helpers public plugin APIs.

Full review comments:

  • [P1] Add the SDK subpath to the entrypoint manifest — package.json:343-346
    The new openclaw/plugin-sdk/lifecycle-restart-hooks subpath is exported from package.json and imported by Telegram, but the plugin SDK build and contract tests derive public entrypoints from scripts/lib/plugin-sdk-entrypoints.json. Without adding the subpath there, the package export sync check fails and packaged builds can point at a dist/plugin-sdk/lifecycle-restart-hooks.js artifact that was never generated.
    Confidence: 0.93
  • [P2] Expose only registration through the SDK — src/plugin-sdk/lifecycle-restart-hooks.ts:6-10
    This public SDK facade lets plugin code call runInProcessRestartHooks() or clearInProcessRestartHooksForTests(), even though only the gateway restart boundary should drain global lifecycle cleanup and tests should not be a production plugin API. Please keep the run/clear helpers core-internal or test-only and export only the registration capability plugins need.
    Confidence: 0.84

Overall correctness: patch is incorrect
Overall confidence: 0.91

Acceptance criteria:

  • pnpm test extensions/telegram/src/polling-lease.test.ts
  • pnpm test src/cli/gateway-cli/run-loop.test.ts
  • pnpm test src/plugins/contracts/plugin-sdk-index.test.ts
  • pnpm test src/plugins/contracts/plugin-sdk-package-contract-guardrails.test.ts
  • pnpm build

What I checked:

Likely related people:

  • Shakker: Current line blame for the Telegram polling lease registry and gateway restart reset block points to 6e5042cd62e7b54d57ad4dddb65a32f5d52d54b0. (role: recent area contributor; confidence: medium; commits: 6e5042cd62e7; files: extensions/telegram/src/polling-lease.ts, src/cli/gateway-cli/run-loop.ts)
  • Peter Steinberger: Available history and shortlog show heavy recent work on package exports, release/package checks, and plugin SDK baseline/alias surfaces relevant to the missing entrypoint-manifest wiring. (role: SDK/package adjacent owner; confidence: medium; commits: 4862d349257a, 45586058e439, 922c90e9fa2d; files: package.json, scripts/lib/plugin-sdk-entrypoints.json, src/plugin-sdk/entrypoints.ts)
  • Ayaan Zaidi: Recent available history includes a plugin SDK subpath export fix, which is adjacent to the PR's missing SDK entrypoint metadata. (role: adjacent SDK export contributor; confidence: low; commits: 28074eeea30a; files: scripts/lib/plugin-sdk-entrypoints.json, package.json, src/plugin-sdk/entrypoints.ts)

Remaining risk / open question:

  • The PR body has no redacted real gateway or Telegram restart proof; unit tests and CI are supplemental but do not prove the live restart path.
  • The new SDK surface shape needs owner review if exposing global run or test-only clear helpers publicly was intentional.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 6602884b0635.

@joshavant

Copy link
Copy Markdown
Collaborator

Thanks @SymbolStar for the PR and for the root-cause writeup. Your analysis of the process-global Symbol.for("openclaw.telegram.pollingLeases") registry matched the issue we reproduced.

We landed the fix via #81890 instead of this PR. The final patch keeps the solution inside the Telegram plugin lifecycle: Telegram stopAccount now releases only aborted same-account stopped polling leases after the normal stop grace, and the lease helper keeps active/no-signal/non-aborted duplicate-poller protection intact. That avoided adding a new public restart-hook SDK surface or changing package exports for this bug.

The merged PR includes focused regression coverage and remote Linux proof for the stale-lease reproduction. Closing this as superseded by #81890.

@joshavant joshavant closed this May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram cli CLI command changes dependencies-changed PR changes dependency-related files size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram polling leases leak across in-process gateway restarts

2 participants