Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/@aws-cdk/cdk-assets-lib/lib/private/p-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export function pLimit(concurrency: number): PLimit {
let stopped = false;

function dispatch() {
if (stopped) {
return;
}
if (activeCount < concurrency && queue.length > 0) {
const [fac, resolve, reject] = queue.shift()!;
activeCount++;
Expand Down Expand Up @@ -41,6 +44,10 @@ export function pLimit(concurrency: number): PLimit {

const ret = <A>(promiseFactory: PromiseFactory<A>) => {
return new Promise<A>((resolve, reject) => {
if (stopped) {
reject(new Error('Task has been cancelled'));
return;
}
queue.push([promiseFactory, resolve, reject]);
dispatch();
});
Expand Down
17 changes: 17 additions & 0 deletions packages/@aws-cdk/cdk-assets-lib/test/private/p-limit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ test('new jobs arent started after dispose is called', async () => {
expect(started).toBeLessThanOrEqual(3);
});

test('no new job is started once dispose is called, even with a free slot', async () => {
const limit = pLimit(1);

limit.dispose();

let ran = false;
await expect(
limit(async () => {
ran = true;
}),
).rejects.toThrow(/cancelled/);

// activeCount was 0 and concurrency is 1, so before the fix dispatch()
// would start this job immediately despite dispose() having been called.
expect(ran).toBe(false);
});

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Loading