|
1 | | -import { describe, it, expect, beforeEach } from 'vitest'; |
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
2 | 2 | import { ObjectKernel } from './kernel'; |
3 | 3 | import { ServiceLifecycle, PluginMetadata } from './plugin-loader'; |
4 | 4 | import type { Plugin } from './types'; |
@@ -231,6 +231,133 @@ describe('ObjectKernel', () => { |
231 | 231 | }); |
232 | 232 | }); |
233 | 233 |
|
| 234 | + // #4813 — the guard timer must not outlive the race it guards. |
| 235 | + // |
| 236 | + // These tests are about the *process*, not about the source: asserting |
| 237 | + // "kernel.ts calls clearTimeout" would be a tautology that any refactor |
| 238 | + // could satisfy while still pinning the event loop. What is asserted here |
| 239 | + // is the observable consequence — after the work is done, nothing the |
| 240 | + // guards armed is still holding the loop open. |
| 241 | + describe('Startup timeout guards do not outlive the race (#4813)', () => { |
| 242 | + /** |
| 243 | + * Ref'd `Timeout` handles — `getActiveResourcesInfo()` reports only |
| 244 | + * resources that are *currently keeping the event loop alive*, which |
| 245 | + * is precisely the property that made `os migrate` hang ~120s after |
| 246 | + * printing `✅ Graceful shutdown complete`. |
| 247 | + */ |
| 248 | + const refdTimers = () => |
| 249 | + process.getActiveResourcesInfo().filter((r) => r === 'Timeout').length; |
| 250 | + |
| 251 | + it('leaves no ref\'d timer behind after the plugin wins the race', async () => { |
| 252 | + const plugin: PluginMetadata = { |
| 253 | + name: 'fast-plugin-long-guard', |
| 254 | + version: '1.0.0', |
| 255 | + init: async () => {}, |
| 256 | + start: async () => {}, |
| 257 | + // The real value that hung one-shot CLI processes: ObjectQLPlugin. |
| 258 | + startupTimeout: 120_000, |
| 259 | + }; |
| 260 | + |
| 261 | + await kernel.use(plugin); |
| 262 | + |
| 263 | + const before = refdTimers(); |
| 264 | + await kernel.bootstrap(); |
| 265 | + const after = refdTimers(); |
| 266 | + |
| 267 | + // Two guards were armed (init + start) and both lost their race. |
| 268 | + // While either is still ref'd the process cannot exit for up to |
| 269 | + // `startupTimeout` — 120s of idling after a 3s job. |
| 270 | + expect(after).toBe(before); |
| 271 | + |
| 272 | + await kernel.shutdown(); |
| 273 | + }); |
| 274 | + |
| 275 | + it('reclaims one guard per lifecycle hook, for every plugin', async () => { |
| 276 | + const makePlugin = (n: number): PluginMetadata => ({ |
| 277 | + name: `guarded-plugin-${n}`, |
| 278 | + version: '1.0.0', |
| 279 | + init: async () => {}, |
| 280 | + start: async () => {}, |
| 281 | + startupTimeout: 120_000, |
| 282 | + }); |
| 283 | + |
| 284 | + for (let n = 0; n < 4; n++) { |
| 285 | + await kernel.use(makePlugin(n)); |
| 286 | + } |
| 287 | + |
| 288 | + const before = refdTimers(); |
| 289 | + await kernel.bootstrap(); |
| 290 | + |
| 291 | + // The issue's probe caught exactly this shape: 8 ref'd Timeouts |
| 292 | + // for 4 plugins (4 init + 4 start). The count must not scale with |
| 293 | + // the plugin list — it must not grow at all. |
| 294 | + expect(refdTimers()).toBe(before); |
| 295 | + |
| 296 | + await kernel.shutdown(); |
| 297 | + }); |
| 298 | + |
| 299 | + it('still fires the guard when the plugin loses the race', async () => { |
| 300 | + // The companion assertion to the two above: reclaiming the guard |
| 301 | + // must not disarm it. `unref()` would satisfy "no ref'd timer" by |
| 302 | + // detaching the guard from the loop — and a process with nothing |
| 303 | + // else to run then exits *silently* instead of reporting the |
| 304 | + // timeout. Clearing on settle keeps the guard armed exactly while |
| 305 | + // the race is undecided. |
| 306 | + const plugin: PluginMetadata = { |
| 307 | + name: 'hanging-plugin', |
| 308 | + version: '1.0.0', |
| 309 | + init: async () => { |
| 310 | + await new Promise((resolve) => setTimeout(resolve, 5000)); |
| 311 | + }, |
| 312 | + startupTimeout: 50, |
| 313 | + }; |
| 314 | + |
| 315 | + await kernel.use(plugin); |
| 316 | + |
| 317 | + await expect(kernel.bootstrap()).rejects.toThrow( |
| 318 | + 'Plugin hanging-plugin init timeout after 50ms' |
| 319 | + ); |
| 320 | + }, 1000); |
| 321 | + }); |
| 322 | + |
| 323 | + describe('Startup timeout guards under fake timers (#4813)', () => { |
| 324 | + beforeEach(() => { |
| 325 | + vi.useFakeTimers(); |
| 326 | + }); |
| 327 | + |
| 328 | + afterEach(() => { |
| 329 | + vi.useRealTimers(); |
| 330 | + }); |
| 331 | + |
| 332 | + it('schedules no pending timer once bootstrap has settled', async () => { |
| 333 | + const kernelWithFakeTimers = new ObjectKernel({ |
| 334 | + logger: { level: 'error' }, |
| 335 | + gracefulShutdown: false, |
| 336 | + skipSystemValidation: true, |
| 337 | + }); |
| 338 | + |
| 339 | + const plugin: PluginMetadata = { |
| 340 | + name: 'fake-timer-plugin', |
| 341 | + version: '1.0.0', |
| 342 | + init: async () => {}, |
| 343 | + start: async () => {}, |
| 344 | + startupTimeout: 120_000, |
| 345 | + }; |
| 346 | + |
| 347 | + await kernelWithFakeTimers.use(plugin); |
| 348 | + |
| 349 | + const before = vi.getTimerCount(); |
| 350 | + await kernelWithFakeTimers.bootstrap(); |
| 351 | + |
| 352 | + // Unlike `getActiveResourcesInfo()`, the fake-timer count includes |
| 353 | + // unref'd timers — so this one distinguishes "the guard was |
| 354 | + // reclaimed" from "the guard was merely detached from the loop". |
| 355 | + expect(vi.getTimerCount()).toBe(before); |
| 356 | + |
| 357 | + await kernelWithFakeTimers.shutdown(); |
| 358 | + }); |
| 359 | + }); |
| 360 | + |
234 | 361 | describe('Startup Failure Rollback', () => { |
235 | 362 | it('should rollback started plugins on failure', async () => { |
236 | 363 | let plugin1Destroyed = false; |
|
0 commit comments