Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
Ensure that cancellation token handling throws VSCode compatible cancel error.
  • Loading branch information
tortmayr committed Jun 9, 2022
1 parent 3f65644 commit 0d32810
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/core/src/common/message-rpc/rpc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { CancellationToken, CancellationTokenSource } from '../cancellation';
import { DisposableCollection } from '../disposable';
import { Disposable, DisposableCollection } from '../disposable';
import { Emitter, Event } from '../event';
import { Deferred } from '../promise-util';
import { Channel } from './channel';
import { RpcMessage, RpcMessageDecoder, RpcMessageEncoder, RpcMessageType } from './rpc-message-encoder';
import { Uint8ArrayWriteBuffer } from './uint8-array-message-buffer';

/**
* Handles request messages received by the {@link RPCProtocol}.
Expand Down Expand Up @@ -145,21 +144,20 @@ export class RpcProtocol {
}

sendRequest<T>(method: string, args: any[]): Promise<T> {
const id = this.nextMessageId++;
const reply = new Deferred<T>();

// The last element of the request args might be a cancellation token. As these tokens are not serializable we have to remove it from the
// args array and the `CANCELLATION_TOKEN_KEY` string instead.
const cancellationToken: CancellationToken | undefined = args.length && CancellationToken.is(args[args.length - 1]) ? args.pop() : undefined;
if (cancellationToken && cancellationToken.isCancellationRequested) {
return Promise.reject(this.cancelError());
}

const id = this.nextMessageId++;
const reply = new Deferred<T>();

if (cancellationToken) {
args.push(RpcProtocol.CANCELLATION_TOKEN_KEY);
cancellationToken.onCancellationRequested(() => {
this.sendCancel(id);
this.pendingRequests.get(id)?.reject(this.cancelError());
}
);
}
Expand Down Expand Up @@ -191,8 +189,8 @@ export class RpcProtocol {
}

cancelError(): Error {
const error = new Error('"Request has already been canceled by the sender"');
error.name = 'Cancel';
const error = new Error('Canceled');
error.name = error.message;
return error;
}

Expand Down Expand Up @@ -224,7 +222,7 @@ export class RpcProtocol {
} catch (err) {
// In case of an error the output buffer might already contains parts of an message.
// => Dispose the current buffer and retrieve a new, clean one for writing the response error.
if (output instanceof Uint8ArrayWriteBuffer) {
if (Disposable.is(output)) {
output.dispose();
}
const errorOutput = this.channel.getWriteBuffer();
Expand Down

0 comments on commit 0d32810

Please sign in to comment.