Skip to content

Fix Telegram polling lease cleanup on restart - #81890

Merged
joshavant merged 2 commits into
mainfrom
fix/telegram-polling-lease-restart
May 14, 2026
Merged

joshavant merged 2 commits into
mainfrom
fix/telegram-polling-lease-restart

Conversation

@joshavant

Copy link
Copy Markdown
Collaborator

Summary

  • Release stale Telegram polling leases from the Telegram gateway stop hook once the stopped poller has had its normal shutdown grace window.
  • Keep duplicate-poller protection intact for active, no-signal, and non-aborted leases.
  • Add focused lease and Telegram gateway tests for stopped-lease cleanup.

Fixes #81507.

Verification

  • node scripts/run-vitest.mjs extensions/telegram/src/polling-lease.test.ts extensions/telegram/src/channel.gateway.test.ts -- --reporter=verbose
  • codex review --uncommitted
  • Crabbox AWS run_615353431110 against the synced dirty branch: reproduced duplicate before cleanup, then CLEANUP released=true, then acquired the same token lease after cleanup.

Real behavior proof

Behavior addressed: Telegram polling leases leaked across in-process gateway restarts and caused duplicate-poller refusal for a stopped account using the same bot token.
Real environment tested: Crabbox AWS Linux, source checkout synced from fix/telegram-polling-lease-restart at base head 1f45b37fe17cf4fa292437f3c586183ccde3e421 plus the local Telegram polling lease diff.
Exact steps or command run after this patch: Created an aborted Telegram polling lease for account default, confirmed a second acquire with the same token failed with the duplicate-poller error, called stopped-lease cleanup, then acquired the same token lease again.
Evidence after fix: Crabbox run run_615353431110 printed BEFORE_ABORT duplicate-error, CLEANUP released=true, and AFTER_CLEANUP acquired fingerprint=cc06801bc84c3cb4, exit 0.
Observed result after fix: The stale stopped lease is cleared only after the stopped poller path is considered stopped; active/no-signal/non-aborted leases remain protected.
What was not tested: A full live Telegram Desktop or Bot API end-to-end message roundtrip was not rerun for this patch; the original Convex-leased Telegram credential reproduction established the real credential shape supports the issue reproduction.

@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: M maintainer Maintainer-authored PR labels May 14, 2026
@joshavant
joshavant force-pushed the fix/telegram-polling-lease-restart branch from ec044c5 to cc5a506 Compare May 14, 2026 18:51
@clawsweeper

clawsweeper Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The PR adds a Telegram gateway stop hook that releases aborted same-account polling leases after the stop grace, adds focused lease/gateway tests, and updates the changelog.

Reproducibility: yes. Current main has a source-level reproduction path through the process-global lease map and duplicate guard, and the PR body reports Crabbox AWS output showing duplicate refusal before cleanup and successful same-token acquire after cleanup.

Real behavior proof
Sufficient (live_output): The PR body includes after-fix Crabbox AWS live output for the lease path, showing duplicate refusal before cleanup, cleanup release, and successful reacquire afterward.

Next step before merge
No repair lane is needed: this is an active implementation PR with no narrow automated repair finding; normal PR review and merge checks should handle it.

Security
Cleared: The diff touches Telegram plugin TypeScript, focused tests, and a changelog entry without adding dependencies, workflow changes, secret handling, lifecycle scripts, downloads, or package-resolution changes.

Review details

Best possible solution:

Land this narrow Telegram-owned cleanup after normal checks, then close #81507 and decide whether #81533 should be superseded.

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

Yes. Current main has a source-level reproduction path through the process-global lease map and duplicate guard, and the PR body reports Crabbox AWS output showing duplicate refusal before cleanup and successful same-token acquire after cleanup.

Is this the best way to solve the issue?

Yes. The PR fixes the Telegram-owned lifecycle leak at the existing channel stop hook, which is narrower than adding a new public restart-hook SDK surface and still preserves active duplicate-poller protection.

What I checked:

  • Current main lease registry and duplicate guard: pollingLeaseRegistry() stores Telegram polling leases on a process-global symbol, and acquireTelegramPollingLease() still rejects a same-token lease when the existing abort signal is not aborted. (extensions/telegram/src/polling-lease.ts:3, 386d321634b3)
  • Current main releases only on normal monitor completion: The polling lease is acquired before TelegramPollingSession runs and released in the monitor finally, so a dropped or timed-out lifecycle can leave the process-global entry until another cleanup path runs. (extensions/telegram/src/monitor.ts:198, 386d321634b3)
  • Gateway stop hook surface exists: The gateway calls a channel plugin stopAccount hook after aborting the account and before the normal channel stop grace wait, which is the hook the PR uses for Telegram-owned cleanup. (src/gateway/server-channels.ts:692, 386d321634b3)
  • PR adds Telegram stop cleanup: The diff imports releaseStoppedTelegramPollingLease and adds telegramPlugin.gateway.stopAccount, which trims the account token and releases only a stopped same-account lease before logging the cleanup. (extensions/telegram/src/channel.ts:954, cc5a5060291e)
  • PR preserves active duplicate-poller protection: The new helper returns false for missing, different-account, no-signal, and non-aborted leases, waits for the normal lease grace, and deletes only the same entry if it is still stale. (extensions/telegram/src/polling-lease.ts:193, cc5a5060291e)
  • PR adds regression coverage: The diff adds lease tests for no-signal active, non-aborted active, delayed stopped cleanup, immediate zero-wait cleanup, and a gateway-level stopped stale lease cleanup path. (extensions/telegram/src/polling-lease.test.ts:100, cc5a5060291e)

Likely related people:

  • @steipete: Commit history shows @steipete introduced the duplicate Telegram polling lease guard and has repeated recent Telegram/gateway lifecycle work adjacent to this path. (role: introduced behavior and adjacent owner; confidence: high; commits: 3169886a21bb, bf2711b40ef7, 204ef7f1c45b; files: extensions/telegram/src/polling-lease.ts, extensions/telegram/src/monitor.ts, src/gateway/server-channels.ts)
  • @joshavant: Beyond authoring this PR, @joshavant has recent merged current-main work on Telegram polling ingress in monitor.ts, which is directly adjacent to the lease lifecycle. (role: recent area contributor; confidence: medium; commits: fd244fd76de3; files: extensions/telegram/src/monitor.ts)
  • @obviyus: Recent Telegram polling/offset commits touching polling-lease.ts and monitor.ts were committed or merged by @obviyus, so they are a useful routing candidate for adjacent Telegram polling behavior. (role: recent adjacent contributor and merger; confidence: medium; commits: 290d3879eb83, 306b51011fd4, cb93c0f8f59d; files: extensions/telegram/src/polling-lease.ts, extensions/telegram/src/monitor.ts)

Remaining risk / open question:

  • I did not independently rerun the focused Vitest command or Crabbox proof in this read-only review; the verdict relies on source inspection plus the PR body's reported live output.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 386d321634b3.

@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 14, 2026
@joshavant
joshavant force-pushed the fix/telegram-polling-lease-restart branch from cc5a506 to 8b67d23 Compare May 14, 2026 19:03
@joshavant
joshavant merged commit 130c2d5 into main May 14, 2026
84 of 85 checks passed
@joshavant
joshavant deleted the fix/telegram-polling-lease-restart branch May 14, 2026 19:04
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* fix(telegram): release stopped polling leases

* docs: add Telegram polling lease changelog
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
* fix(telegram): release stopped polling leases

* docs: add Telegram polling lease changelog
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
* fix(telegram): release stopped polling leases

* docs: add Telegram polling lease changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram maintainer Maintainer-authored PR proof: sufficient ClawSweeper judged the real behavior proof convincing. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant