Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't dispose the CancellationTokenSource until all onCancellationRequested handlers have run #199067

Merged
merged 1 commit into from
Jan 3, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/base/common/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export function createCancelablePromise<T>(callback: (token: CancellationToken)
const promise = new Promise<T>((resolve, reject) => {
const subscription = source.token.onCancellationRequested(() => {
subscription.dispose();
source.dispose();
reject(new CancellationError());
});
Promise.resolve(thenable).then(value => {
Expand All @@ -45,6 +44,7 @@ export function createCancelablePromise<T>(callback: (token: CancellationToken)
return <CancelablePromise<T>>new class {
cancel() {
source.cancel();
source.dispose();
}
then<TResult1 = T, TResult2 = never>(resolve?: ((value: T) => TResult1 | Promise<TResult1>) | undefined | null, reject?: ((reason: any) => TResult2 | Promise<TResult2>) | undefined | null): Promise<TResult1 | TResult2> {
return promise.then(resolve, reject);
Expand Down
21 changes: 21 additions & 0 deletions src/vs/base/test/common/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ suite('Async', () => {
return promise.then(() => assert.deepStrictEqual(order, ['in callback', 'afterCreate', 'cancelled', 'afterCancel', 'finally']));
});

test('execution order (async with late listener)', async function () {
const order: string[] = [];

const cancellablePromise = async.createCancelablePromise(async token => {
order.push('in callback');

await async.timeout(0);
store.add(token.onCancellationRequested(_ => order.push('cancelled')));
cancellablePromise.cancel();
order.push('afterCancel');
});

order.push('afterCreate');

const promise = cancellablePromise
.then(undefined, err => null)
.then(() => order.push('finally'));

return promise.then(() => assert.deepStrictEqual(order, ['in callback', 'afterCreate', 'cancelled', 'afterCancel', 'finally']));
});

test('get inner result', async function () {
const promise = async.createCancelablePromise(token => {
return async.timeout(12).then(_ => 1234);
Expand Down