diff --git a/src/workflow/local-executor.test.ts b/src/workflow/local-executor.test.ts index cdbe2d2b..63c3a5bd 100644 --- a/src/workflow/local-executor.test.ts +++ b/src/workflow/local-executor.test.ts @@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest' import { prisma } from '@/db' import { setupTestDbHooks } from '@/db-test-hooks' import { workflowService } from './workflow' -import { LocalExecutor } from './local-executor' +import { LocalExecutor, ConcurrencyLimiter } from './local-executor' import { WorkflowTaskStatus, WorkflowTaskType } from '@/generated/prisma/client' // Mock the individual workflows @@ -196,4 +196,77 @@ describe('LocalExecutor Integration Tests', () => { await new Promise((resolve) => setTimeout(resolve, 50)) }) }) + + describe('ConcurrencyLimiter Unit Tests', () => { + it('should not allow concurrency limit violation during microtask race conditions', async () => { + const limiter = new ConcurrencyLimiter(1) + + // Task A starts and runs + let resolveA: () => void = () => {} + const promiseA = limiter.run( + () => + new Promise((resolve) => { + resolveA = resolve + }), + ) + + // Task B enters and is queued + let resolveB: () => void = () => {} + const promiseB = limiter.run( + () => + new Promise((resolve) => { + resolveB = resolve + }), + ) + + // Wait briefly for Task B to queue up + await new Promise((resolve) => setTimeout(resolve, 10)) + + expect(limiter.getActiveCount()).toBe(1) + expect(limiter.getQueueLength()).toBe(1) + + // Complete Task A (schedules Task A's body to resume) + resolveA() + + // Wait exactly one microtask tick to let Task A exit and trigger finally block (resolving Task B) + // This positions us in the microtask gap before Task B resumes execution + await Promise.resolve() + + // Task C immediately enters the limiter synchronously + let resolveC: () => void = () => {} + const promiseC = limiter.run( + () => + new Promise((resolve) => { + resolveC = resolve + }), + ) + + // Under the buggy implementation: + // - Task A's finally block decremented activeCount to 0. + // - Task C sees activeCount = 0 and runs immediately instead of queueing. + // - So queue length would be 0, and Task C is active. + // + // Under the fixed implementation: + // - Task A's finally block transferred the slot to Task B without decrementing activeCount. + // - So activeCount remains 1. + // - Task C sees activeCount = 1 and is correctly queued. + // - So queue length is 1, and Task C is NOT active. + expect(limiter.getActiveCount()).toBe(1) + expect(limiter.getQueueLength()).toBe(1) + + // Clean up by resolving the rest in order + // 1. Let Task B resume and run + await new Promise((resolve) => setTimeout(resolve, 10)) + resolveB() + + // 2. Let Task C resume and run + await new Promise((resolve) => setTimeout(resolve, 10)) + resolveC() + + await Promise.all([promiseA, promiseB, promiseC]) + + expect(limiter.getActiveCount()).toBe(0) + expect(limiter.getQueueLength()).toBe(0) + }) + }) }) diff --git a/src/workflow/local-executor.ts b/src/workflow/local-executor.ts index 575c1b07..20a76faa 100644 --- a/src/workflow/local-executor.ts +++ b/src/workflow/local-executor.ts @@ -15,7 +15,7 @@ import { logger } from '@/logger' // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(globalThis as any).__localLogger = logger -class ConcurrencyLimiter { +export class ConcurrencyLimiter { private activeCount = 0 private queue: (() => void)[] = [] @@ -24,15 +24,18 @@ class ConcurrencyLimiter { async run(fn: () => Promise): Promise { if (this.activeCount >= this.limit) { await new Promise((resolve) => this.queue.push(resolve)) + } else { + this.activeCount++ } - this.activeCount++ + try { return await fn() } finally { - this.activeCount-- const next = this.queue.shift() if (next) { next() + } else { + this.activeCount-- } } }