|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { runInProcessRestartHooks } from "openclaw/plugin-sdk/lifecycle-restart-hooks"; |
2 | 3 | import { |
3 | 4 | acquireTelegramPollingLease, |
| 5 | + releaseTelegramPollingLeasesForLifecycleReset, |
4 | 6 | resetTelegramPollingLeasesForTests, |
5 | 7 | } from "./polling-lease.js"; |
6 | 8 |
|
@@ -99,4 +101,91 @@ describe("Telegram polling lease", () => { |
99 | 101 | vi.useRealTimers(); |
100 | 102 | } |
101 | 103 | }); |
| 104 | + |
| 105 | + // Regression: openclaw/openclaw#81507 |
| 106 | + // The polling lease registry lives on a process-global Symbol, so a stale |
| 107 | + // entry left by a dropped previous-lifecycle monitor task blocks every new |
| 108 | + // acquire after an in-process gateway restart. The Telegram extension |
| 109 | + // registers a lifecycle reset hook that drains the registry at the restart |
| 110 | + // boundary; after that runs, a fresh acquire for the same token must |
| 111 | + // succeed even though the old lease was never explicitly released. |
| 112 | + it("clears stale same-token leases at the in-process restart boundary", async () => { |
| 113 | + const oldAbort = new AbortController(); |
| 114 | + const stale = await acquireTelegramPollingLease({ |
| 115 | + token: "123:abc", |
| 116 | + accountId: "old-lifecycle", |
| 117 | + abortSignal: oldAbort.signal, |
| 118 | + }); |
| 119 | + |
| 120 | + // Simulate the previous lifecycle dying without ever calling release() |
| 121 | + // (its `finally` block was interrupted by the in-process restart). |
| 122 | + await expect( |
| 123 | + acquireTelegramPollingLease({ |
| 124 | + token: "123:abc", |
| 125 | + accountId: "new-lifecycle", |
| 126 | + waitMs: 0, |
| 127 | + }), |
| 128 | + ).rejects.toThrow('refusing duplicate poller for account "new-lifecycle"'); |
| 129 | + |
| 130 | + // The gateway run-loop drains plugin-registered subsystem hooks at the |
| 131 | + // restart boundary. The Telegram extension registers itself on first |
| 132 | + // module load, so this drain frees the stale lease. |
| 133 | + const drained = releaseTelegramPollingLeasesForLifecycleReset(); |
| 134 | + expect(drained).toBe(1); |
| 135 | + |
| 136 | + const fresh = await acquireTelegramPollingLease({ |
| 137 | + token: "123:abc", |
| 138 | + accountId: "new-lifecycle", |
| 139 | + }); |
| 140 | + expect(fresh.tokenFingerprint).toBe(stale.tokenFingerprint); |
| 141 | + |
| 142 | + // A late `release()` from the dropped previous-lifecycle task must NOT |
| 143 | + // delete the fresh lease (owner-checked release). |
| 144 | + stale.release(); |
| 145 | + await expect( |
| 146 | + acquireTelegramPollingLease({ |
| 147 | + token: "123:abc", |
| 148 | + accountId: "third", |
| 149 | + }), |
| 150 | + ).rejects.toThrow('refusing duplicate poller for account "new-lifecycle"'); |
| 151 | + |
| 152 | + fresh.release(); |
| 153 | + }); |
| 154 | + |
| 155 | + // Regression: openclaw/openclaw#81507 |
| 156 | + // The cleanup is lifecycle-owned, not steady-state. Two concurrent live |
| 157 | + // pollers within the same lifecycle (no restart in between) must still be |
| 158 | + // rejected by the same-token guard. |
| 159 | + it("keeps the same-token live duplicate guard outside the restart boundary", async () => { |
| 160 | + const first = await acquireTelegramPollingLease({ |
| 161 | + token: "123:abc", |
| 162 | + accountId: "a", |
| 163 | + }); |
| 164 | + await expect( |
| 165 | + acquireTelegramPollingLease({ |
| 166 | + token: "123:abc", |
| 167 | + accountId: "b", |
| 168 | + }), |
| 169 | + ).rejects.toThrow('refusing duplicate poller for account "b"'); |
| 170 | + first.release(); |
| 171 | + }); |
| 172 | + |
| 173 | + // The Telegram extension wires its cleanup through the shared |
| 174 | + // in-process-restart-hooks registry on first module load. Running the |
| 175 | + // registry must cascade through to the Telegram lease registry without |
| 176 | + // any callers having to know about the Telegram-specific helper. |
| 177 | + it("is wired into the shared in-process restart hook registry", async () => { |
| 178 | + const lease = await acquireTelegramPollingLease({ |
| 179 | + token: "123:abc", |
| 180 | + accountId: "hook-test", |
| 181 | + }); |
| 182 | + expect(runInProcessRestartHooks()).toBeGreaterThanOrEqual(1); |
| 183 | + const fresh = await acquireTelegramPollingLease({ |
| 184 | + token: "123:abc", |
| 185 | + accountId: "hook-test-2", |
| 186 | + }); |
| 187 | + fresh.release(); |
| 188 | + // Late stale release from the dropped previous-lifecycle task is safe. |
| 189 | + lease.release(); |
| 190 | + }); |
102 | 191 | }); |
0 commit comments