From b11b9cfb9f549d72abe334f148c74f28c7c0c2ab Mon Sep 17 00:00:00 2001 From: kwy404 Date: Tue, 27 Oct 2026 13:09:55 -0300 Subject: [PATCH 01/11] Add outgoing and incoming message hooks to MessageStrategy A message strategy can now implement handleOutgoingMessage to see or change every message before the connection writes it, and handleIncomingMessage to handle received messages instead of handleMessage. This lets callers add properties like a W3C trace context to the messages they send and read them on the other side. handleMessage keeps handling received messages only, so existing strategies behave as before, and is now optional. Closes #1757 --- jsonrpc/README.md | 16 +++++ jsonrpc/src/common/connection.ts | 51 +++++++++++--- jsonrpc/src/node/test/connection.test.ts | 86 ++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 8 deletions(-) diff --git a/jsonrpc/README.md b/jsonrpc/README.md index 4ffbb87d1..2c0293fbf 100644 --- a/jsonrpc/README.md +++ b/jsonrpc/README.md @@ -50,6 +50,22 @@ connection.onNotification(notification, (param: string) => { connection.listen(); ``` +Both sides can pass a `messageStrategy` in the connection options to work with the messages of the connection. `handleOutgoingMessage` gets every message before it is written, and `handleIncomingMessage` gets every received message before it is dispatched. For example, to send a trace context along with each message and restore it while the other side handles the message (`getTraceparent` and `runWithTraceparent` stand for your own tracing code): + +```ts +let connection = rpc.createMessageConnection(reader, writer, undefined, { + messageStrategy: { + handleOutgoingMessage(message, next) { + return next({ ...message, traceparent: getTraceparent() } as rpc.Message); + }, + handleIncomingMessage(message, next) { + const traceparent = (message as { traceparent?: string }).traceparent; + return runWithTraceparent(traceparent, () => next(message)); + } + } +}); +``` + # History For the history please see the [main repository](https://github.com/Microsoft/vscode-languageserver-node/blob/master/README.md) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index 06bdd5d6c..78cabde6c 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -474,13 +474,38 @@ export namespace CancellationStrategy { } export interface MessageStrategy { - handleMessage(message: Message, next: (message: Message) => NotificationResult): NotificationResult; + /** + * Handles a received message before it is dispatched. Not called if + * `handleIncomingMessage` is provided. + * + * @param message The received message. + * @param next Dispatches the message. + */ + handleMessage?(message: Message, next: (message: Message) => NotificationResult): NotificationResult; + + /** + * Handles a received message before it is dispatched. If provided, it is + * used instead of `handleMessage`. + * + * @param message The received message. + * @param next Dispatches the message. + */ + handleIncomingMessage?(message: Message, next: (message: Message) => NotificationResult): NotificationResult; + + /** + * Handles a message before it is written, for example to add a property + * like a trace context to it. + * + * @param message The message to send. + * @param next Writes the message. Pass it the message to write. + */ + handleOutgoingMessage?(message: Message, next: (message: Message) => Promise): Promise; } export namespace MessageStrategy { export function is(value: any): value is MessageStrategy { const candidate: MessageStrategy = value; - return candidate && Is.func(candidate.handleMessage); + return candidate && (Is.func(candidate.handleMessage) || Is.func(candidate.handleIncomingMessage) || Is.func(candidate.handleOutgoingMessage)); } } @@ -735,7 +760,9 @@ export function createMessageConnection(messageReader: MessageReader, messageWri try { inFlight++; const messageStrategy = options?.messageStrategy; - if (MessageStrategy.is(messageStrategy)) { + if (messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage)) { + result = messageStrategy.handleIncomingMessage(message, handleMessage); + } else if (messageStrategy !== undefined && Is.func(messageStrategy.handleMessage)) { result = messageStrategy.handleMessage(message, handleMessage); } else { result = handleMessage(message); @@ -775,6 +802,14 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } } + function writeMessage(message: Message): Promise { + const messageStrategy = options?.messageStrategy; + if (messageStrategy !== undefined && Is.func(messageStrategy.handleOutgoingMessage)) { + return messageStrategy.handleOutgoingMessage(message, (message) => messageWriter.write(message)); + } + return messageWriter.write(message); + } + const callback: DataCallback = (message) => { try { // We have received a cancellation message. Check if the message is still in the queue @@ -791,7 +826,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri requestTokens.delete(cancelId); response.id = toCancel.id; traceSendingResponse(response, message.method, Date.now()); - messageWriter.write(response).catch(() => logger.error(`Sending response for canceled message failed.`)); + writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); return; } } @@ -831,7 +866,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri message.result = resultOrError === undefined ? null : resultOrError; } traceSendingResponse(message, method, startTime); - return messageWriter.write(message); + return writeMessage(message); } function replyError(error: ResponseError, method: string, startTime: number): Promise { const message: ResponseMessage = { @@ -840,7 +875,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri error: error.toJson() }; traceSendingResponse(message, method, startTime); - return messageWriter.write(message); + return writeMessage(message); } traceReceivedRequest(requestMessage); @@ -1313,7 +1348,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri params: messageParams }; traceSendingNotification(notificationMessage); - return messageWriter.write(notificationMessage).catch((error) => { + return writeMessage(notificationMessage).catch((error) => { logger.error(`Sending notification failed.`); throw error; }); @@ -1460,7 +1495,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri const responsePromise: ResponsePromise | null = { method: method, timerStart: Date.now(), resolve: resolveWithCleanup, reject: rejectWithCleanup }; try { responsePromises.set(id, responsePromise); - await messageWriter.write(requestMessage); + await writeMessage(requestMessage); if (tokenWasCancelled) { sendCancellation(connection, id); } diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index 3de33d49a..735cacd28 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -885,6 +885,92 @@ suite('Connection', () => { }); }); + test('Message strategy handles outgoing and incoming messages', async () => { + const type = new RequestType('test/handleSingleRequest'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const asyncLocalStorage = new AsyncLocalStorage(); + + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger, { + messageStrategy: { + handleIncomingMessage(message, next) { + const traceparent = (message as { traceparent?: string }).traceparent; + return asyncLocalStorage.run(traceparent, () => next(message)); + } + } + }); + server.onRequest(type, () => asyncLocalStorage.getStore()); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + handleOutgoingMessage(message, next) { + return next({ ...message, traceparent: '00-trace-span-01' } as hostConnection.Message); + } + } + }); + client.listen(); + + assert.strictEqual(await client.sendRequest(type, 0), '00-trace-span-01'); + }); + + test('Message strategy handles outgoing responses and notifications', async () => { + const type = new RequestType('test/handleSingleRequest'); + const notificationType = new NotificationType('test/notification'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const sent: string[] = []; + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger, { + messageStrategy: { + handleOutgoingMessage(message, next) { + sent.push(hostConnection.Message.isNotification(message) ? message.method : 'response'); + return next(message); + } + } + }); + server.onRequest(type, async (param) => { + await server.sendNotification(notificationType, param); + return param + 1; + }); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger); + client.listen(); + + assert.strictEqual(await client.sendRequest(type, 1), 2); + assert.deepStrictEqual(sent, ['test/notification', 'response']); + }); + + test('Message strategy prefers handleIncomingMessage over handleMessage', async () => { + const type = new RequestType('test/handleSingleRequest'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const calls: string[] = []; + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger, { + messageStrategy: { + handleMessage(message, next) { + calls.push('handleMessage'); + return next(message); + }, + handleIncomingMessage(message, next) { + calls.push('handleIncomingMessage'); + return next(message); + } + } + }); + server.onRequest(type, (param) => param + 1); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger); + client.listen(); + + assert.strictEqual(await client.sendRequest(type, 1), 2); + assert.deepStrictEqual(calls, ['handleIncomingMessage']); + }); + test('Message ports', async () => { const type = new RequestType('test/handleSingleRequest'); const messageChannel = new MessageChannel(); From ce58efa03c483a6a62e2268abf2539b6c425d011 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Tue, 27 Oct 2026 15:32:17 -0300 Subject: [PATCH 02/11] Keep outgoing order and pass unqueued messages to handleIncomingMessage handleOutgoingMessage now gets one message at a time in send order. A hook that awaited before calling next let a later message reach the writer first, so for example didChange could be written before didOpen. Connections without the hook still write directly. handleIncomingMessage now also gets the messages that skip the queue: responses when maxParallelism is set and $/cancelRequest notifications handled as soon as they are read. They still skip the queue, and handleMessage still does not get them. --- jsonrpc/README.md | 2 +- jsonrpc/src/common/connection.ts | 43 +++++++--- jsonrpc/src/node/test/connection.test.ts | 105 +++++++++++++++++++++++ 3 files changed, 139 insertions(+), 11 deletions(-) diff --git a/jsonrpc/README.md b/jsonrpc/README.md index 2c0293fbf..ba6160b0a 100644 --- a/jsonrpc/README.md +++ b/jsonrpc/README.md @@ -50,7 +50,7 @@ connection.onNotification(notification, (param: string) => { connection.listen(); ``` -Both sides can pass a `messageStrategy` in the connection options to work with the messages of the connection. `handleOutgoingMessage` gets every message before it is written, and `handleIncomingMessage` gets every received message before it is dispatched. For example, to send a trace context along with each message and restore it while the other side handles the message (`getTraceparent` and `runWithTraceparent` stand for your own tracing code): +Both sides can pass a `messageStrategy` in the connection options to work with the messages of the connection. `handleOutgoingMessage` gets every message before it is written, and `handleIncomingMessage` gets every received message before it is dispatched. Outgoing messages are passed one at a time in send order, so a `handleOutgoingMessage` that awaits before calling `next` delays the messages after it instead of reordering them. For example, to send a trace context along with each message and restore it while the other side handles the message (`getTraceparent` and `runWithTraceparent` stand for your own tracing code): ```ts let connection = rpc.createMessageConnection(reader, writer, undefined, { diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index 78cabde6c..d7fc98d7a 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -485,7 +485,9 @@ export interface MessageStrategy { /** * Handles a received message before it is dispatched. If provided, it is - * used instead of `handleMessage`. + * used instead of `handleMessage`. Unlike `handleMessage`, it also gets the + * messages that skip the message queue: responses when `maxParallelism` is + * set and `$/cancelRequest` notifications handled as soon as they are read. * * @param message The received message. * @param next Dispatches the message. @@ -494,7 +496,9 @@ export interface MessageStrategy { /** * Handles a message before it is written, for example to add a property - * like a trace context to it. + * like a trace context to it. Messages are passed one at a time in send + * order: the next one is passed when the promise returned for the previous + * one settles. * * @param message The message to send. * @param next Writes the message. Pass it the message to write. @@ -733,7 +737,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } else { // If we have limited parallelism we resolve responses to avoid // dead locks. - handleResponse(message); + handleUnqueuedMessage(message, handleMessage); } } else { queue.set(createNotificationQueueKey(), message); @@ -802,12 +806,27 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } } + // Messages that skip the queue go through `handleIncomingMessage`. + // `handleMessage` keeps getting queued messages only. + function handleUnqueuedMessage(message: Message, handle: (message: Message) => NotificationResult): void { + const messageStrategy = options?.messageStrategy; + const result = messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage) ? messageStrategy.handleIncomingMessage(message, handle) : handle(message); + if (result instanceof Promise) { + result.catch((error) => logger.error(`Processing message failed: ${error.toString()}`)); + } + } + + let lastOutgoingMessage: Promise = Promise.resolve(); function writeMessage(message: Message): Promise { const messageStrategy = options?.messageStrategy; - if (messageStrategy !== undefined && Is.func(messageStrategy.handleOutgoingMessage)) { - return messageStrategy.handleOutgoingMessage(message, (message) => messageWriter.write(message)); + if (messageStrategy === undefined || !Is.func(messageStrategy.handleOutgoingMessage)) { + return messageWriter.write(message); } - return messageWriter.write(message); + // Pass one message at a time to the hook. Otherwise a hook that awaits + // before calling next lets a later message reach the writer first. + const result = lastOutgoingMessage.then(() => messageStrategy.handleOutgoingMessage!(message, (message) => messageWriter.write(message))); + lastOutgoingMessage = result.catch(() => undefined); + return result; } const callback: DataCallback = (message) => { @@ -825,16 +844,20 @@ export function createMessageConnection(messageReader: MessageReader, messageWri messageQueue.delete(key); requestTokens.delete(cancelId); response.id = toCancel.id; - traceSendingResponse(response, message.method, Date.now()); - writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); + handleUnqueuedMessage(message, () => { + traceSendingResponse(response, message.method, Date.now()); + return writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); + }); return; } } const cancellationToken = requestTokens.get(cancelId); // The request is already running. Cancel the token if (cancellationToken !== undefined) { - cancellationToken.cancel(); - traceReceivedNotification(message); + handleUnqueuedMessage(message, () => { + cancellationToken.cancel(); + traceReceivedNotification(message); + }); return; } else { // Remember the cancel but still queue the message to diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index 735cacd28..a8c74d94a 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -971,6 +971,111 @@ suite('Connection', () => { assert.deepStrictEqual(calls, ['handleIncomingMessage']); }); + test('Message strategy keeps the send order when handleOutgoingMessage awaits', async () => { + const didOpen = new NotificationType('test/didOpen'); + const didChange = new NotificationType('test/didChange'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const received: string[] = []; + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); + const bothReceived = new Promise((resolve) => { + server.onNotification((method) => { + received.push(method); + if (received.length === 2) { + resolve(); + } + }); + }); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + if (hostConnection.Message.isNotification(message) && message.method === didOpen.method) { + await new Promise(resolve => setTimeout(resolve, 10)); + } + return next(message); + } + } + }); + client.listen(); + + void client.sendNotification(didOpen, 1); + void client.sendNotification(didChange, 2); + await bothReceived; + assert.deepStrictEqual(received, [didOpen.method, didChange.method]); + }); + + test('Message strategy handles responses that skip the queue', async () => { + const type = new RequestType('test/handleSingleRequest'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); + server.onRequest(type, (param) => param + 1); + server.listen(); + + // With maxParallelism set, responses are handled as soon as they are read. + const received: hostConnection.Message[] = []; + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + maxParallelism: 1, + messageStrategy: { + handleIncomingMessage(message, next) { + received.push(message); + return next(message); + } + } + }); + client.listen(); + + assert.strictEqual(await client.sendRequest(type, 1), 2); + assert.deepStrictEqual(received, [{ jsonrpc: '2.0', id: 0, result: 2 }]); + }); + + test('Message strategy handles cancellations that skip the queue', async () => { + const type = new RequestType('test/handleSingleRequest'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const received: string[] = []; + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger, { + maxParallelism: 1, + connectionStrategy: { + cancelUndispatched: (message) => ({ jsonrpc: '2.0', id: (message as hostConnection.RequestMessage).id, result: 0 }) + }, + messageStrategy: { + handleIncomingMessage(message, next) { + received.push((message as hostConnection.RequestMessage).method); + return next(message); + } + } + }); + let started!: () => void; + const firstStarted = new Promise(resolve => started = resolve); + server.onRequest(type, (param, token) => { + started(); + return new Promise(resolve => token.onCancellationRequested(() => resolve(param))); + }); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger); + client.listen(); + + // The first request runs while the second one waits in the queue. Both + // cancellations are handled as soon as they are read. + const firstSource = new CancellationTokenSource(); + const first = client.sendRequest(type, 1, firstSource.token); + await firstStarted; + const secondSource = new CancellationTokenSource(); + const second = client.sendRequest(type, 2, secondSource.token); + secondSource.cancel(); + assert.strictEqual(await second, 0); + firstSource.cancel(); + assert.strictEqual(await first, 1); + assert.deepStrictEqual(received, [type.method, '$/cancelRequest', '$/cancelRequest']); + }); + test('Message ports', async () => { const type = new RequestType('test/handleSingleRequest'); const messageChannel = new MessageChannel(); From efce8f7925a26572c2f60d3ad29c4d907d0e0c7a Mon Sep 17 00:00:00 2001 From: kwy404 Date: Tue, 27 Oct 2026 15:46:41 -0300 Subject: [PATCH 03/11] Catch hook errors and defer queued cancellations until next is called A synchronous exception from handleIncomingMessage on a message that skips the queue escaped to the reader. The IPC reader calls the connection from the process message event, so this could crash the process. Such errors are now logged like asynchronous rejections. When cancelUndispatched answered a queued request, the request was removed from the queue before the hook ran, so a hook that never called next left it unanswered. The request is now removed when next is called, and only if it is still queued then. If it already started, its token is cancelled instead, so it is still answered once. --- jsonrpc/src/common/connection.ts | 25 ++++++-- jsonrpc/src/node/test/connection.test.ts | 74 ++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index d7fc98d7a..0cf32fa75 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -807,12 +807,18 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } // Messages that skip the queue go through `handleIncomingMessage`. - // `handleMessage` keeps getting queued messages only. + // `handleMessage` keeps getting queued messages only. Errors are logged, + // since the reader can call us from an event handler. function handleUnqueuedMessage(message: Message, handle: (message: Message) => NotificationResult): void { const messageStrategy = options?.messageStrategy; - const result = messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage) ? messageStrategy.handleIncomingMessage(message, handle) : handle(message); - if (result instanceof Promise) { - result.catch((error) => logger.error(`Processing message failed: ${error.toString()}`)); + const logError = (error: any) => logger.error(`Processing message failed: ${error}`); + try { + const result = messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage) ? messageStrategy.handleIncomingMessage(message, handle) : handle(message); + if (result instanceof Promise) { + result.catch(logError); + } + } catch (error) { + logError(error); } } @@ -841,10 +847,17 @@ export function createMessageConnection(messageReader: MessageReader, messageWri const strategy = options?.connectionStrategy; const response = (strategy && strategy.cancelUndispatched) ? strategy.cancelUndispatched(toCancel, cancelUndispatched) : cancelUndispatched(toCancel); if (response && (response.error !== undefined || response.result !== undefined)) { - messageQueue.delete(key); - requestTokens.delete(cancelId); response.id = toCancel.id; handleUnqueuedMessage(message, () => { + // The hook can call next later or not at all. Only drop the + // request if it is still queued then, otherwise cancel it. + if (messageQueue.get(key) !== toCancel) { + requestTokens.get(cancelId)?.cancel(); + traceReceivedNotification(message); + return; + } + messageQueue.delete(key); + requestTokens.delete(cancelId); traceSendingResponse(response, message.method, Date.now()); return writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); }); diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index a8c74d94a..2b9b27a70 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -1076,6 +1076,80 @@ suite('Connection', () => { assert.deepStrictEqual(received, [type.method, '$/cancelRequest', '$/cancelRequest']); }); + test('Message strategy can ignore the cancellation of a queued request', async () => { + const type = new RequestType('test/handleSingleRequest'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + let dropped!: () => void; + const cancelDropped = new Promise(resolve => dropped = resolve); + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger, { + maxParallelism: 1, + connectionStrategy: { + cancelUndispatched: (message) => ({ jsonrpc: '2.0', id: (message as hostConnection.RequestMessage).id, result: 0 }) + }, + messageStrategy: { + handleIncomingMessage(message, next) { + // Cancellations are dropped without calling next. + if (hostConnection.Message.isNotification(message) && message.method === '$/cancelRequest') { + dropped(); + return; + } + return next(message); + } + } + }); + let started!: () => void; + const firstStarted = new Promise(resolve => started = resolve); + let release!: () => void; + const released = new Promise(resolve => release = resolve); + server.onRequest(type, async (param) => { + if (param === 1) { + started(); + await released; + } + return param; + }); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger); + client.listen(); + + // The second request waits in the queue while the first one runs. + const first = client.sendRequest(type, 1); + await firstStarted; + const source = new CancellationTokenSource(); + const second = client.sendRequest(type, 2, source.token); + source.cancel(); + await cancelDropped; + release(); + assert.strictEqual(await first, 1); + assert.strictEqual(await second, 2); + }); + + test('Message strategy errors on messages that skip the queue are logged', () => { + const proc = new EventEmitter(); + const errors: string[] = []; + const logger: hostConnection.Logger = { + ...hostConnection.NullLogger, + error: (message: string) => errors.push(message) + }; + const connection = hostConnection.createMessageConnection(new hostConnection.IPCMessageReader(proc as NodeJS.Process), new hostConnection.IPCMessageWriter(proc as NodeJS.Process), logger, { + maxParallelism: 1, + messageStrategy: { + handleIncomingMessage() { + throw new Error('hook failed'); + } + } + }); + connection.listen(); + + // The IPC reader calls the connection from the process message event, + // so an error escaping there would be an uncaught exception. + assert.doesNotThrow(() => proc.emit('message', { jsonrpc: '2.0', id: 0, result: 1 })); + assert.deepStrictEqual(errors, ['Processing message failed: Error: hook failed']); + }); + test('Message ports', async () => { const type = new RequestType('test/handleSingleRequest'); const messageChannel = new MessageChannel(); From 4acc14364b66f62a9979b97eedb0c21e3538ae66 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 29 Oct 2026 13:17:26 -0300 Subject: [PATCH 04/11] Defer all cancellation bookkeeping until handleIncomingMessage calls next With the default connection strategy, a $/cancelRequest for a queued request was added to knownCanceledRequests before the hook saw it, so the request got a canceled token even when the hook dropped the cancellation. The hook now gets every $/cancelRequest as soon as it is read, and the existing cancellation code only runs when it calls next. The copy that stays queued to clean up is not passed to the hook again. --- jsonrpc/src/common/connection.ts | 48 +++++++++++++----------- jsonrpc/src/node/test/connection.test.ts | 45 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 21 deletions(-) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index 0cf32fa75..14b496560 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -487,7 +487,8 @@ export interface MessageStrategy { * Handles a received message before it is dispatched. If provided, it is * used instead of `handleMessage`. Unlike `handleMessage`, it also gets the * messages that skip the message queue: responses when `maxParallelism` is - * set and `$/cancelRequest` notifications handled as soon as they are read. + * set, and `$/cancelRequest` notifications as soon as they are read. A + * request is only canceled once `next` is called for its `$/cancelRequest`. * * @param message The received message. * @param next Dispatches the message. @@ -765,7 +766,8 @@ export function createMessageConnection(messageReader: MessageReader, messageWri inFlight++; const messageStrategy = options?.messageStrategy; if (messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage)) { - result = messageStrategy.handleIncomingMessage(message, handleMessage); + // A queued cancellation already went through the hook when it was read. + result = isCancelNotification(message) ? handleMessage(message) : messageStrategy.handleIncomingMessage(message, handleMessage); } else if (messageStrategy !== undefined && Is.func(messageStrategy.handleMessage)) { result = messageStrategy.handleMessage(message, handleMessage); } else { @@ -836,10 +838,25 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } const callback: DataCallback = (message) => { + const messageStrategy = options?.messageStrategy; + // The hook gets cancellations as soon as they are read and can drop them, + // so nothing is canceled or remembered before it calls next. + if (isCancelNotification(message) && messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage)) { + handleUnqueuedMessage(message, receiveMessage); + } else { + receiveMessage(message); + } + }; + + function isCancelNotification(message: Message): message is NotificationMessage { + return Message.isNotification(message) && message.method === CancelNotification.type.method; + } + + function receiveMessage(message: Message): void { try { // We have received a cancellation message. Check if the message is still in the queue // and cancel it if allowed to do so. - if (Message.isNotification(message) && message.method === CancelNotification.type.method) { + if (isCancelNotification(message)) { const cancelId = (message.params as CancelParams).id; const key = createRequestQueueKey(cancelId); const toCancel = messageQueue.get(key); @@ -847,30 +864,19 @@ export function createMessageConnection(messageReader: MessageReader, messageWri const strategy = options?.connectionStrategy; const response = (strategy && strategy.cancelUndispatched) ? strategy.cancelUndispatched(toCancel, cancelUndispatched) : cancelUndispatched(toCancel); if (response && (response.error !== undefined || response.result !== undefined)) { + messageQueue.delete(key); + requestTokens.delete(cancelId); response.id = toCancel.id; - handleUnqueuedMessage(message, () => { - // The hook can call next later or not at all. Only drop the - // request if it is still queued then, otherwise cancel it. - if (messageQueue.get(key) !== toCancel) { - requestTokens.get(cancelId)?.cancel(); - traceReceivedNotification(message); - return; - } - messageQueue.delete(key); - requestTokens.delete(cancelId); - traceSendingResponse(response, message.method, Date.now()); - return writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); - }); + traceSendingResponse(response, message.method, Date.now()); + writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); return; } } const cancellationToken = requestTokens.get(cancelId); // The request is already running. Cancel the token if (cancellationToken !== undefined) { - handleUnqueuedMessage(message, () => { - cancellationToken.cancel(); - traceReceivedNotification(message); - }); + cancellationToken.cancel(); + traceReceivedNotification(message); return; } else { // Remember the cancel but still queue the message to @@ -882,7 +888,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } finally { triggerMessageQueue(); } - }; + } async function handleRequest(requestMessage: RequestMessage): Promise { if (isDisposed()) { diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index 2b9b27a70..219ccaf3d 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -1127,6 +1127,51 @@ suite('Connection', () => { assert.strictEqual(await second, 2); }); + test('Message strategy can ignore the cancellation of a queued request without cancelUndispatched', async () => { + const type = new RequestType('test/handleSingleRequest'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger, { + maxParallelism: 1, + messageStrategy: { + handleIncomingMessage(message, next) { + // The cancellation of the queued request (id 1) is dropped. + if (hostConnection.Message.isNotification(message) && message.method === '$/cancelRequest' && (message.params as { id: number }).id === 1) { + return; + } + return next(message); + } + } + }); + let started!: () => void; + const firstStarted = new Promise(resolve => started = resolve); + server.onRequest(type, (param, token) => { + if (param === 1) { + started(); + return new Promise(resolve => token.onCancellationRequested(() => resolve(true))); + } + return token.isCancellationRequested; + }); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger); + client.listen(); + + // The second request waits in the queue while the first one runs. The + // first one ends on its own cancellation, which is read after the + // dropped one. + const firstSource = new CancellationTokenSource(); + const first = client.sendRequest(type, 1, firstSource.token); + await firstStarted; + const secondSource = new CancellationTokenSource(); + const second = client.sendRequest(type, 2, secondSource.token); + secondSource.cancel(); + firstSource.cancel(); + assert.strictEqual(await first, true); + assert.strictEqual(await second, false); + }); + test('Message strategy errors on messages that skip the queue are logged', () => { const proc = new EventEmitter(); const errors: string[] = []; From 8575cc1fbdb9715e836f2e9b0015b868628c38d6 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 5 Nov 2026 14:09:01 -0300 Subject: [PATCH 05/11] Handle hooks that misuse next and deprecate handleMessage next in handleOutgoingMessage now only works once. If the hook settles without calling it, writeMessage rejects, so a request fails with MessageWriteError instead of waiting forever and a notification rejects. Queued messages no longer reach the hook or the writer after dispose. handleMessage is deprecated in favor of handleIncomingMessage, the requirement to call next for $/cancelRequest is documented, and the check for handleIncomingMessage moved to an incomingHook helper. --- README.md | 6 + jsonrpc/README.md | 6 + jsonrpc/src/common/connection.ts | 62 ++++++++-- jsonrpc/src/node/test/connection.test.ts | 138 ++++++++++++++++++++++- 4 files changed, 202 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5ea87fcb3..5729b65c7 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,12 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for the repository map, focused build a ## History +## Next + +### Compatibility + +- JSON-RPC: `handleMessage` is now optional on `MessageStrategy` and deprecated in favor of `handleIncomingMessage`. Code that calls `strategy.handleMessage(...)` directly needs a check for `undefined` when `strictNullChecks` is on. + ## 3.18.5 Protocol, 10.0.0 JSON-RPC, 11.0.0 Client and 11.0.0 Server. [Fixed issues](https://github.com/microsoft/vscode-languageserver-node/issues?q=is%3Aissue%20is%3Aclosed%20reason%3Acompleted%20milestone%3A%223.18.5%22%20sort%3Aupdated-desc) | [Merged pull requests](https://github.com/microsoft/vscode-languageserver-node/pulls?q=is%3Apr%20is%3Amerged%20milestone%3A%223.18.5%22%20sort%3Aupdated-desc) diff --git a/jsonrpc/README.md b/jsonrpc/README.md index ba6160b0a..77e05cd4b 100644 --- a/jsonrpc/README.md +++ b/jsonrpc/README.md @@ -66,6 +66,12 @@ let connection = rpc.createMessageConnection(reader, writer, undefined, { }); ``` +A `handleIncomingMessage` hook must call `next` for `$/cancelRequest`, or the request is not canceled. + +A `handleOutgoingMessage` hook must call `next` exactly once before its promise settles. If it does not call `next`, the message is not written and sending it fails: a request is rejected with `ErrorCodes.MessageWriteError` and the promise of a notification is rejected. Any other call to `next` logs a warning and writes nothing. + +Properties added at the top level of a message reach the writer unchanged. `Message` does not declare them, which is why the example casts the object passed to `next`. + # History For the history please see the [main repository](https://github.com/Microsoft/vscode-languageserver-node/blob/master/README.md) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index 14b496560..927cd0f26 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -478,6 +478,8 @@ export interface MessageStrategy { * Handles a received message before it is dispatched. Not called if * `handleIncomingMessage` is provided. * + * @deprecated Use `handleIncomingMessage` instead. + * * @param message The received message. * @param next Dispatches the message. */ @@ -487,8 +489,10 @@ export interface MessageStrategy { * Handles a received message before it is dispatched. If provided, it is * used instead of `handleMessage`. Unlike `handleMessage`, it also gets the * messages that skip the message queue: responses when `maxParallelism` is - * set, and `$/cancelRequest` notifications as soon as they are read. A - * request is only canceled once `next` is called for its `$/cancelRequest`. + * set, and `$/cancelRequest` notifications as soon as they are read. + * + * You must call `next` for `$/cancelRequest`, or the request is not + * canceled. * * @param message The received message. * @param next Dispatches the message. @@ -501,6 +505,16 @@ export interface MessageStrategy { * order: the next one is passed when the promise returned for the previous * one settles. * + * Properties added at the top level of a message reach the writer + * unchanged. `Message` does not declare them, so the object passed to + * `next` has to be cast to `Message`. + * + * You must call `next` exactly once before the returned promise settles. + * If it is not called, the message is not written and sending it fails: a + * request is rejected with `ErrorCodes.MessageWriteError` and the promise + * of a notification is rejected. Any other call to `next` logs a warning + * and writes nothing. + * * @param message The message to send. * @param next Writes the message. Pass it the message to write. */ @@ -765,9 +779,10 @@ export function createMessageConnection(messageReader: MessageReader, messageWri try { inFlight++; const messageStrategy = options?.messageStrategy; - if (messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage)) { + const handleIncoming = incomingHook(); + if (handleIncoming !== undefined) { // A queued cancellation already went through the hook when it was read. - result = isCancelNotification(message) ? handleMessage(message) : messageStrategy.handleIncomingMessage(message, handleMessage); + result = isCancelNotification(message) ? handleMessage(message) : handleIncoming(message, handleMessage); } else if (messageStrategy !== undefined && Is.func(messageStrategy.handleMessage)) { result = messageStrategy.handleMessage(message, handleMessage); } else { @@ -808,14 +823,22 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } } + function incomingHook(): MessageStrategy['handleIncomingMessage'] { + const messageStrategy = options?.messageStrategy; + if (messageStrategy === undefined || !Is.func(messageStrategy.handleIncomingMessage)) { + return undefined; + } + return (message, next) => messageStrategy.handleIncomingMessage!(message, next); + } + // Messages that skip the queue go through `handleIncomingMessage`. // `handleMessage` keeps getting queued messages only. Errors are logged, // since the reader can call us from an event handler. function handleUnqueuedMessage(message: Message, handle: (message: Message) => NotificationResult): void { - const messageStrategy = options?.messageStrategy; + const handleIncoming = incomingHook(); const logError = (error: any) => logger.error(`Processing message failed: ${error}`); try { - const result = messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage) ? messageStrategy.handleIncomingMessage(message, handle) : handle(message); + const result = handleIncoming !== undefined ? handleIncoming(message, handle) : handle(message); if (result instanceof Promise) { result.catch(logError); } @@ -832,16 +855,37 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } // Pass one message at a time to the hook. Otherwise a hook that awaits // before calling next lets a later message reach the writer first. - const result = lastOutgoingMessage.then(() => messageStrategy.handleOutgoingMessage!(message, (message) => messageWriter.write(message))); + const result = lastOutgoingMessage.then(async () => { + if (isDisposed()) { + throw new ConnectionError(ConnectionErrors.Disposed, 'Connection is disposed.'); + } + let written: Promise | undefined; + let settled = false; + const next = (outgoing: Message): Promise => { + if (written !== undefined || settled) { + logger.warn(`handleOutgoingMessage must call next exactly once before it settles.`); + return written ?? Promise.resolve(); + } + written = messageWriter.write(outgoing); + return written; + }; + try { + await messageStrategy.handleOutgoingMessage!(message, next); + } finally { + settled = true; + } + if (written === undefined) { + throw new Error(`handleOutgoingMessage did not call next, so the message was not written.`); + } + }); lastOutgoingMessage = result.catch(() => undefined); return result; } const callback: DataCallback = (message) => { - const messageStrategy = options?.messageStrategy; // The hook gets cancellations as soon as they are read and can drop them, // so nothing is canceled or remembered before it calls next. - if (isCancelNotification(message) && messageStrategy !== undefined && Is.func(messageStrategy.handleIncomingMessage)) { + if (isCancelNotification(message) && incomingHook() !== undefined) { handleUnqueuedMessage(message, receiveMessage); } else { receiveMessage(message); diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index 219ccaf3d..ffd8d1885 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -12,7 +12,7 @@ import { inherits } from 'util'; import { AsyncLocalStorage } from 'async_hooks'; import { EventEmitter } from 'events'; -import { CancellationTokenSource, RequestType, RequestType3, ResponseError, NotificationType, NotificationType2, ErrorCodes, Trace, Tracer } from '../main'; +import { CancellationTokenSource, RequestType, RequestType0, RequestType3, ResponseError, NotificationType, NotificationType2, ErrorCodes, Trace, Tracer } from '../main'; import * as hostConnection from '../main'; import { getCustomCancellationStrategy } from './customCancellationStrategy'; @@ -1007,6 +1007,142 @@ suite('Connection', () => { assert.deepStrictEqual(received, [didOpen.method, didChange.method]); }); + test('Message strategy fails a request when handleOutgoingMessage does not call next', async () => { + const type = new RequestType0('test/request'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage() { + } + } + }); + client.listen(); + + await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); + }); + + test('Message strategy fails a notification when handleOutgoingMessage does not call next', async () => { + const type = new NotificationType('test/notification'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage() { + } + } + }); + client.listen(); + + await assert.rejects(client.sendNotification(type, 1)); + }); + + test('Message strategy writes a message once when handleOutgoingMessage calls next twice', async () => { + const first = new NotificationType('test/first'); + const second = new NotificationType('test/second'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const received: string[] = []; + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); + const secondReceived = new Promise((resolve) => { + server.onNotification((method) => { + received.push(method); + if (method === second.method) { + resolve(); + } + }); + }); + server.listen(); + + const warnings: string[] = []; + const logger: hostConnection.Logger = { ...hostConnection.NullLogger, warn: (message) => warnings.push(message) }; + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, logger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + await next(message); + if (hostConnection.Message.isNotification(message) && message.method === first.method) { + await next(message); + } + } + } + }); + client.listen(); + + void client.sendNotification(first, 1); + void client.sendNotification(second, 2); + await secondReceived; + assert.deepStrictEqual(received, [first.method, second.method]); + assert.strictEqual(warnings.length, 1); + }); + + for (const failure of ['throws', 'rejects']) { + test(`Message strategy keeps writing after handleOutgoingMessage ${failure}`, async () => { + const failing = new NotificationType('test/failing'); + const after = new NotificationType('test/after'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); + const received = new Promise((resolve) => { + server.onNotification((method) => resolve(method)); + }); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + handleOutgoingMessage(message, next) { + if (hostConnection.Message.isNotification(message) && message.method === failing.method) { + if (failure === 'throws') { + throw new Error('hook failed'); + } + return Promise.reject(new Error('hook failed')); + } + return next(message); + } + } + }); + client.listen(); + + await assert.rejects(client.sendNotification(failing, 1), /hook failed/); + await client.sendNotification(after, 2); + assert.strictEqual(await received, after.method); + }); + } + + test('Message strategy does not handle queued outgoing messages after dispose', async () => { + const first = new NotificationType('test/first'); + const second = new NotificationType('test/second'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + let release!: () => void; + const released = new Promise((resolve) => release = resolve); + const handled: string[] = []; + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + handled.push((message as hostConnection.NotificationMessage).method); + await released; + return next(message); + } + } + }); + client.listen(); + + const firstSent = client.sendNotification(first, 1); + const secondSent = client.sendNotification(second, 2); + await new Promise(resolve => setTimeout(resolve, 0)); + client.dispose(); + release(); + + await firstSent.catch(() => undefined); + await assert.rejects(secondSent, (error: hostConnection.ConnectionError) => error.code === hostConnection.ConnectionErrors.Disposed); + assert.deepStrictEqual(handled, [first.method]); + }); + test('Message strategy handles responses that skip the queue', async () => { const type = new RequestType('test/handleSingleRequest'); const duplexStream1 = new TestDuplex('ds1'); From fbf0378a6d45c08965585920301e821bd1a0ed7a Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 5 Nov 2026 14:26:47 -0300 Subject: [PATCH 06/11] Report a failed write when the hook does not return next writeMessage only waited for the hook, so with a hook that called next without returning its promise a failed write was lost and the request waited forever. --- jsonrpc/src/common/connection.ts | 3 +++ jsonrpc/src/node/test/connection.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index 927cd0f26..bea456235 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -877,6 +877,9 @@ export function createMessageConnection(messageReader: MessageReader, messageWri if (written === undefined) { throw new Error(`handleOutgoingMessage did not call next, so the message was not written.`); } + // A hook may not return the promise of next, so a failed write has to + // be reported here or the request would wait forever. + await written; }); lastOutgoingMessage = result.catch(() => undefined); return result; diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index ffd8d1885..8e0898c89 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -1039,6 +1039,29 @@ suite('Connection', () => { await assert.rejects(client.sendNotification(type, 1)); }); + test('Message strategy fails a request when a write fails and the hook does not return next', async () => { + class FailingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(): Promise { + return Promise.reject(new Error('write failed')); + } + public end(): void { + } + } + const type = new RequestType0('test/request'); + const duplexStream = new TestDuplex('ds1'); + + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new FailingWriter(), hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + void next(message); + } + } + }); + client.listen(); + + await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); + }); + test('Message strategy writes a message once when handleOutgoingMessage calls next twice', async () => { const first = new NotificationType('test/first'); const second = new NotificationType('test/second'); From 8a7946e9248c6daec0d5eba85d5da306aac3b0f5 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Fri, 6 Nov 2026 21:03:37 -0300 Subject: [PATCH 07/11] Do not leave a failed write unhandled while the hook runs A hook that calls next without awaiting it and is still running when the write fails left the rejection without a handler, which can end the Node.js process even though the caller handles the send failure. Mark the promise as handled when it is created. The failure is still reported when the hook settles. --- jsonrpc/src/common/connection.ts | 3 +++ jsonrpc/src/node/test/connection.test.ts | 34 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index bea456235..e7d0b495f 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -867,6 +867,9 @@ export function createMessageConnection(messageReader: MessageReader, messageWri return written ?? Promise.resolve(); } written = messageWriter.write(outgoing); + // The rejection is reported once the hook settles. Until then the + // hook may not be listening, which would be an unhandled rejection. + written.catch(() => undefined); return written; }; try { diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index 8e0898c89..d017a4414 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -1062,6 +1062,40 @@ suite('Connection', () => { await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); }); + test('Message strategy does not leave a failed write unhandled while the hook runs', async () => { + class FailingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(): Promise { + return Promise.reject(new Error('write failed')); + } + public end(): void { + } + } + const type = new RequestType0('test/request'); + const duplexStream = new TestDuplex('ds1'); + + const unhandled: unknown[] = []; + const listeners = process.listeners('unhandledRejection'); + process.removeAllListeners('unhandledRejection'); + process.on('unhandledRejection', (reason) => unhandled.push(reason)); + try { + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new FailingWriter(), hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + void next(message); + await new Promise(resolve => setTimeout(resolve, 10)); + } + } + }); + client.listen(); + + await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); + assert.deepStrictEqual(unhandled, []); + } finally { + process.removeAllListeners('unhandledRejection'); + listeners.forEach(listener => process.on('unhandledRejection', listener)); + } + }); + test('Message strategy writes a message once when handleOutgoingMessage calls next twice', async () => { const first = new NotificationType('test/first'); const second = new NotificationType('test/second'); From 46c7ab1a8278ba1844433937e75f590d1cef7ce7 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 26 Nov 2026 18:34:15 -0300 Subject: [PATCH 08/11] Do not write after end or dispose while the outgoing hook runs With handleOutgoingMessage, end() now ends the writer only after the messages sent before it are written, and a message whose hook is still running when the connection is disposed is not written. --- jsonrpc/src/common/connection.ts | 16 +++++- jsonrpc/src/node/test/connection.test.ts | 66 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index e7d0b495f..4f89e372c 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -509,6 +509,10 @@ export interface MessageStrategy { * unchanged. `Message` does not declare them, so the object passed to * `next` has to be cast to `Message`. * + * Messages sent before `end` is called are written before the writer is + * ended. Messages not written yet when the connection is disposed are not + * written, and sending them fails. + * * You must call `next` exactly once before the returned promise settles. * If it is not called, the message is not written and sending it fails: a * request is rejected with `ErrorCodes.MessageWriteError` and the promise @@ -866,7 +870,10 @@ export function createMessageConnection(messageReader: MessageReader, messageWri logger.warn(`handleOutgoingMessage must call next exactly once before it settles.`); return written ?? Promise.resolve(); } - written = messageWriter.write(outgoing); + // The connection can be disposed while the hook runs. + written = isDisposed() + ? Promise.reject(new ConnectionError(ConnectionErrors.Disposed, 'Connection is disposed.')) + : messageWriter.write(outgoing); // The rejection is reported once the hook settles. Until then the // hook may not be listening, which would be an unhandled rejection. written.catch(() => undefined); @@ -1666,7 +1673,12 @@ export function createMessageConnection(messageReader: MessageReader, messageWri onUnhandledNotification: unhandledNotificationEmitter.event, onDispose: disposeEmitter.event, end: () => { - messageWriter.end(); + if (options?.messageStrategy !== undefined && Is.func(options.messageStrategy.handleOutgoingMessage)) { + // Messages sent before end may still be in handleOutgoingMessage. + lastOutgoingMessage.then(() => messageWriter.end()).catch((error) => logger.error(`Ending the writer failed: ${error}`)); + } else { + messageWriter.end(); + } }, dispose: () => { if (isDisposed()) { diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index d017a4414..ca3584911 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -1200,6 +1200,72 @@ suite('Connection', () => { assert.deepStrictEqual(handled, [first.method]); }); + test('Message strategy does not write a message when the connection is disposed while the hook runs', async () => { + const events: string[] = []; + class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(message: hostConnection.Message): Promise { + events.push(`write ${(message as hostConnection.NotificationMessage).method}`); + return Promise.resolve(); + } + public end(): void { + events.push('end'); + } + } + const type = new NotificationType('test/notification'); + const duplexStream = new TestDuplex('ds1'); + + let release!: () => void; + const released = new Promise((resolve) => release = resolve); + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new RecordingWriter(), hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + await released; + return next(message); + } + } + }); + client.listen(); + + const sent = client.sendNotification(type, 1); + await new Promise(resolve => setTimeout(resolve, 0)); + client.dispose(); + release(); + + await assert.rejects(sent, (error: hostConnection.ConnectionError) => error.code === hostConnection.ConnectionErrors.Disposed); + assert.deepStrictEqual(events, []); + }); + + test('Message strategy writes messages sent before end before ending the writer', async () => { + const events: string[] = []; + class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(message: hostConnection.Message): Promise { + events.push(`write ${(message as hostConnection.NotificationMessage).method}`); + return Promise.resolve(); + } + public end(): void { + events.push('end'); + } + } + const type = new NotificationType('test/notification'); + const duplexStream = new TestDuplex('ds1'); + + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new RecordingWriter(), hostConnection.NullLogger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + await new Promise(resolve => setTimeout(resolve, 10)); + return next(message); + } + } + }); + client.listen(); + + const sent = client.sendNotification(type, 1); + client.end(); + await sent; + await new Promise(resolve => setTimeout(resolve, 0)); + assert.deepStrictEqual(events, [`write ${type.method}`, 'end']); + }); + test('Message strategy handles responses that skip the queue', async () => { const type = new RequestType('test/handleSingleRequest'); const duplexStream1 = new TestDuplex('ds1'); From 67fea13f37fe1f3742b76d6ee5d40780e2788721 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 26 Nov 2026 18:56:11 -0300 Subject: [PATCH 09/11] Log outgoing hook errors after the message was written A hook that throws after next resolved made handleRequest send a second response for the same request. Also cover failed writes with guard tests that check for unhandled rejections. --- jsonrpc/README.md | 2 +- jsonrpc/src/common/connection.ts | 11 +++ jsonrpc/src/node/test/connection.test.ts | 106 ++++++++++++++++------- 3 files changed, 88 insertions(+), 31 deletions(-) diff --git a/jsonrpc/README.md b/jsonrpc/README.md index 77e05cd4b..08f234ef8 100644 --- a/jsonrpc/README.md +++ b/jsonrpc/README.md @@ -68,7 +68,7 @@ let connection = rpc.createMessageConnection(reader, writer, undefined, { A `handleIncomingMessage` hook must call `next` for `$/cancelRequest`, or the request is not canceled. -A `handleOutgoingMessage` hook must call `next` exactly once before its promise settles. If it does not call `next`, the message is not written and sending it fails: a request is rejected with `ErrorCodes.MessageWriteError` and the promise of a notification is rejected. Any other call to `next` logs a warning and writes nothing. +A `handleOutgoingMessage` hook must call `next` exactly once before its promise settles. If it does not call `next`, the message is not written and sending it fails: a request is rejected with `ErrorCodes.MessageWriteError` and the promise of a notification is rejected. Any other call to `next` logs a warning and writes nothing. If the hook fails after the message was written, the error is logged and sending still succeeds, so a request never gets a second response. Properties added at the top level of a message reach the writer unchanged. `Message` does not declare them, which is why the example casts the object passed to `next`. diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index 4f89e372c..c313f648c 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -879,17 +879,28 @@ export function createMessageConnection(messageReader: MessageReader, messageWri written.catch(() => undefined); return written; }; + let hookError: { error: unknown } | undefined; try { await messageStrategy.handleOutgoingMessage!(message, next); + } catch (error) { + hookError = { error }; } finally { settled = true; } if (written === undefined) { + if (hookError !== undefined) { + throw hookError.error; + } throw new Error(`handleOutgoingMessage did not call next, so the message was not written.`); } // A hook may not return the promise of next, so a failed write has to // be reported here or the request would wait forever. await written; + // The message was written. Failing here would make handleRequest send + // a second response for the same request, so the hook error is logged. + if (hookError !== undefined) { + logger.error(`handleOutgoingMessage failed after the message was written: ${hookError.error}`); + } }); lastOutgoingMessage = result.catch(() => undefined); return result; diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index ca3584911..0cb70e9c3 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -1062,39 +1062,47 @@ suite('Connection', () => { await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); }); - test('Message strategy does not leave a failed write unhandled while the hook runs', async () => { - class FailingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { - public write(): Promise { - return Promise.reject(new Error('write failed')); - } - public end(): void { - } - } - const type = new RequestType0('test/request'); - const duplexStream = new TestDuplex('ds1'); - - const unhandled: unknown[] = []; - const listeners = process.listeners('unhandledRejection'); - process.removeAllListeners('unhandledRejection'); - process.on('unhandledRejection', (reason) => unhandled.push(reason)); - try { - const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new FailingWriter(), hostConnection.NullLogger, { - messageStrategy: { - async handleOutgoingMessage(message, next) { - void next(message); - await new Promise(resolve => setTimeout(resolve, 10)); - } + const failingHooks: { name: string; hook: (message: hostConnection.Message, next: (message: hostConnection.Message) => Promise) => Promise }[] = [ + { name: 'returns next', hook: (message, next) => next(message) }, + { name: 'awaits next', hook: async (message, next) => { await next(message); } }, + { name: 'does not return next', hook: async (message, next) => { void next(message); await new Promise(resolve => setTimeout(resolve, 10)); } }, + { name: 'throws after next', hook: async (message, next) => { void next(message); await new Promise(resolve => setTimeout(resolve, 10)); throw new Error('hook failed'); } }, + ]; + for (const { name, hook } of failingHooks) { + test(`Message strategy does not leave a failed write unhandled when the hook ${name}`, async () => { + class FailingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(): Promise { + return Promise.reject(new Error('write failed')); } - }); - client.listen(); + public end(): void { + } + } + const request = new RequestType0('test/request'); + const notification = new NotificationType('test/notification'); + const duplexStream = new TestDuplex('ds1'); - await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); - assert.deepStrictEqual(unhandled, []); - } finally { + const unhandled: unknown[] = []; + const listeners = process.listeners('unhandledRejection'); process.removeAllListeners('unhandledRejection'); - listeners.forEach(listener => process.on('unhandledRejection', listener)); - } - }); + process.on('unhandledRejection', (reason) => unhandled.push(reason)); + try { + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new FailingWriter(), hostConnection.NullLogger, { + messageStrategy: { handleOutgoingMessage: hook } + }); + client.listen(); + + await assert.rejects(client.sendRequest(request), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); + await assert.rejects(client.sendNotification(notification, 1)); + client.end(); + // Unhandled rejections are reported after the microtask queue drains. + await new Promise(resolve => setTimeout(resolve, 20)); + assert.deepStrictEqual(unhandled, []); + } finally { + process.removeAllListeners('unhandledRejection'); + listeners.forEach(listener => process.on('unhandledRejection', listener)); + } + }); + } test('Message strategy writes a message once when handleOutgoingMessage calls next twice', async () => { const first = new NotificationType('test/first'); @@ -1235,6 +1243,44 @@ suite('Connection', () => { assert.deepStrictEqual(events, []); }); + test('Message strategy sends one response when handleOutgoingMessage throws after the response is written', async () => { + const type = new RequestType('test/request'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const written: hostConnection.Message[] = []; + const streamWriter = new hostConnection.StreamMessageWriter(duplexStream1); + class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(message: hostConnection.Message): Promise { + written.push(message); + return streamWriter.write(message); + } + public end(): void { + } + } + const errors: string[] = []; + const logger: hostConnection.Logger = { ...hostConnection.NullLogger, error: (message) => errors.push(message) }; + const server = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream2), new RecordingWriter(), logger, { + messageStrategy: { + async handleOutgoingMessage(message, next) { + await next(message); + throw new Error('cleanup failed'); + } + } + }); + server.onRequest(type, (param) => param + 1); + server.listen(); + + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger); + client.listen(); + + assert.strictEqual(await client.sendRequest(type, 1), 2); + await new Promise(resolve => setTimeout(resolve, 10)); + assert.deepStrictEqual(written, [{ jsonrpc: '2.0', id: 0, result: 2 }]); + assert.strictEqual(errors.length, 1); + assert.ok(errors[0].includes('cleanup failed')); + }); + test('Message strategy writes messages sent before end before ending the writer', async () => { const events: string[] = []; class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { From 64a8d96d42782c979f0d3e96b54e4ff4f3d4c82b Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 3 Dec 2026 11:22:53 -0300 Subject: [PATCH 10/11] Make handleOutgoingMessage synchronous The hook is now called synchronously when a message is sent and gets a synchronous next, so the message reaches the writer in the same call as without the hook. This keeps the send order without a promise chain, and removes the handling that only existed because the hook could await: waiting for pending writes in end(), dropping queued messages after dispose, and settling next after the hook. A hook still has to call next exactly once before it returns. A later call logs a warning and writes nothing, and an error thrown after next is logged so a request does not get a second response. --- jsonrpc/README.md | 6 +- jsonrpc/src/common/connection.ts | 90 +++----- jsonrpc/src/node/test/connection.test.ts | 259 +++++++---------------- 3 files changed, 111 insertions(+), 244 deletions(-) diff --git a/jsonrpc/README.md b/jsonrpc/README.md index 08f234ef8..6c22fbef8 100644 --- a/jsonrpc/README.md +++ b/jsonrpc/README.md @@ -50,13 +50,13 @@ connection.onNotification(notification, (param: string) => { connection.listen(); ``` -Both sides can pass a `messageStrategy` in the connection options to work with the messages of the connection. `handleOutgoingMessage` gets every message before it is written, and `handleIncomingMessage` gets every received message before it is dispatched. Outgoing messages are passed one at a time in send order, so a `handleOutgoingMessage` that awaits before calling `next` delays the messages after it instead of reordering them. For example, to send a trace context along with each message and restore it while the other side handles the message (`getTraceparent` and `runWithTraceparent` stand for your own tracing code): +Both sides can pass a `messageStrategy` in the connection options to work with the messages of the connection. `handleOutgoingMessage` gets every message before it is written, and `handleIncomingMessage` gets every received message before it is dispatched. `handleOutgoingMessage` is synchronous, so messages are written in send order. For example, to send a trace context along with each message and restore it while the other side handles the message (`getTraceparent` and `runWithTraceparent` stand for your own tracing code): ```ts let connection = rpc.createMessageConnection(reader, writer, undefined, { messageStrategy: { handleOutgoingMessage(message, next) { - return next({ ...message, traceparent: getTraceparent() } as rpc.Message); + next({ ...message, traceparent: getTraceparent() } as rpc.Message); }, handleIncomingMessage(message, next) { const traceparent = (message as { traceparent?: string }).traceparent; @@ -68,7 +68,7 @@ let connection = rpc.createMessageConnection(reader, writer, undefined, { A `handleIncomingMessage` hook must call `next` for `$/cancelRequest`, or the request is not canceled. -A `handleOutgoingMessage` hook must call `next` exactly once before its promise settles. If it does not call `next`, the message is not written and sending it fails: a request is rejected with `ErrorCodes.MessageWriteError` and the promise of a notification is rejected. Any other call to `next` logs a warning and writes nothing. If the hook fails after the message was written, the error is logged and sending still succeeds, so a request never gets a second response. +A `handleOutgoingMessage` hook must call `next` exactly once before it returns. If it does not call `next`, the message is not written and sending it fails: a request is rejected with `ErrorCodes.MessageWriteError` and the promise of a notification is rejected. Any other call to `next`, including one made after the hook returned, logs a warning and writes nothing. If the hook throws after it called `next`, the error is logged and sending still succeeds, so a request never gets a second response. Properties added at the top level of a message reach the writer unchanged. `Message` does not declare them, which is why the example casts the object passed to `next`. diff --git a/jsonrpc/src/common/connection.ts b/jsonrpc/src/common/connection.ts index c313f648c..af91a3995 100644 --- a/jsonrpc/src/common/connection.ts +++ b/jsonrpc/src/common/connection.ts @@ -501,28 +501,23 @@ export interface MessageStrategy { /** * Handles a message before it is written, for example to add a property - * like a trace context to it. Messages are passed one at a time in send - * order: the next one is passed when the promise returned for the previous - * one settles. + * like a trace context to it. The hook is called synchronously when the + * message is sent, so messages keep their send order. * * Properties added at the top level of a message reach the writer * unchanged. `Message` does not declare them, so the object passed to * `next` has to be cast to `Message`. * - * Messages sent before `end` is called are written before the writer is - * ended. Messages not written yet when the connection is disposed are not - * written, and sending them fails. - * - * You must call `next` exactly once before the returned promise settles. - * If it is not called, the message is not written and sending it fails: a - * request is rejected with `ErrorCodes.MessageWriteError` and the promise - * of a notification is rejected. Any other call to `next` logs a warning - * and writes nothing. + * You must call `next` exactly once before the hook returns. If it is not + * called, the message is not written and sending it fails: a request is + * rejected with `ErrorCodes.MessageWriteError` and the promise of a + * notification is rejected. Any other call to `next`, including one made + * after the hook returned, logs a warning and writes nothing. * * @param message The message to send. * @param next Writes the message. Pass it the message to write. */ - handleOutgoingMessage?(message: Message, next: (message: Message) => Promise): Promise; + handleOutgoingMessage?(message: Message, next: (message: Message) => void): void; } export namespace MessageStrategy { @@ -851,59 +846,33 @@ export function createMessageConnection(messageReader: MessageReader, messageWri } } - let lastOutgoingMessage: Promise = Promise.resolve(); function writeMessage(message: Message): Promise { const messageStrategy = options?.messageStrategy; if (messageStrategy === undefined || !Is.func(messageStrategy.handleOutgoingMessage)) { return messageWriter.write(message); } - // Pass one message at a time to the hook. Otherwise a hook that awaits - // before calling next lets a later message reach the writer first. - const result = lastOutgoingMessage.then(async () => { - if (isDisposed()) { - throw new ConnectionError(ConnectionErrors.Disposed, 'Connection is disposed.'); - } - let written: Promise | undefined; - let settled = false; - const next = (outgoing: Message): Promise => { - if (written !== undefined || settled) { - logger.warn(`handleOutgoingMessage must call next exactly once before it settles.`); - return written ?? Promise.resolve(); - } - // The connection can be disposed while the hook runs. - written = isDisposed() - ? Promise.reject(new ConnectionError(ConnectionErrors.Disposed, 'Connection is disposed.')) - : messageWriter.write(outgoing); - // The rejection is reported once the hook settles. Until then the - // hook may not be listening, which would be an unhandled rejection. - written.catch(() => undefined); - return written; - }; - let hookError: { error: unknown } | undefined; - try { - await messageStrategy.handleOutgoingMessage!(message, next); - } catch (error) { - hookError = { error }; - } finally { - settled = true; + let written: Promise | undefined; + let returned = false; + const next = (outgoing: Message): void => { + if (written !== undefined || returned) { + logger.warn(`handleOutgoingMessage must call next exactly once before it returns.`); + return; } + written = messageWriter.write(outgoing); + }; + try { + messageStrategy.handleOutgoingMessage(message, next); + } catch (error) { if (written === undefined) { - if (hookError !== undefined) { - throw hookError.error; - } - throw new Error(`handleOutgoingMessage did not call next, so the message was not written.`); + return Promise.reject(error); } - // A hook may not return the promise of next, so a failed write has to - // be reported here or the request would wait forever. - await written; // The message was written. Failing here would make handleRequest send - // a second response for the same request, so the hook error is logged. - if (hookError !== undefined) { - logger.error(`handleOutgoingMessage failed after the message was written: ${hookError.error}`); - } - }); - lastOutgoingMessage = result.catch(() => undefined); - return result; + // a second response for the same request, so the error is logged. + logger.error(`handleOutgoingMessage failed after the message was written: ${error}`); + } finally { + returned = true; + } + return written ?? Promise.reject(new Error(`handleOutgoingMessage did not call next, so the message was not written.`)); } const callback: DataCallback = (message) => { @@ -1684,12 +1653,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri onUnhandledNotification: unhandledNotificationEmitter.event, onDispose: disposeEmitter.event, end: () => { - if (options?.messageStrategy !== undefined && Is.func(options.messageStrategy.handleOutgoingMessage)) { - // Messages sent before end may still be in handleOutgoingMessage. - lastOutgoingMessage.then(() => messageWriter.end()).catch((error) => logger.error(`Ending the writer failed: ${error}`)); - } else { - messageWriter.end(); - } + messageWriter.end(); }, dispose: () => { if (isDisposed()) { diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index 0cb70e9c3..a0d4c0e6a 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -971,40 +971,36 @@ suite('Connection', () => { assert.deepStrictEqual(calls, ['handleIncomingMessage']); }); - test('Message strategy keeps the send order when handleOutgoingMessage awaits', async () => { - const didOpen = new NotificationType('test/didOpen'); - const didChange = new NotificationType('test/didChange'); - const duplexStream1 = new TestDuplex('ds1'); - const duplexStream2 = new TestDuplex('ds2'); - - const received: string[] = []; - const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); - const bothReceived = new Promise((resolve) => { - server.onNotification((method) => { - received.push(method); - if (received.length === 2) { - resolve(); - } - }); - }); - server.listen(); + test('Message strategy writes a message before send returns', () => { + const written: string[] = []; + class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { + public write(message: hostConnection.Message): Promise { + written.push((message as hostConnection.RequestMessage).method); + return Promise.resolve(); + } + public end(): void { + } + } + const request = new RequestType0('test/request'); + const notification = new NotificationType('test/notification'); + const duplexStream = new TestDuplex('ds1'); - const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new RecordingWriter(), hostConnection.NullLogger, { messageStrategy: { - async handleOutgoingMessage(message, next) { - if (hostConnection.Message.isNotification(message) && message.method === didOpen.method) { - await new Promise(resolve => setTimeout(resolve, 10)); - } - return next(message); + handleOutgoingMessage(message, next) { + next(message); } } }); client.listen(); - void client.sendNotification(didOpen, 1); - void client.sendNotification(didChange, 2); - await bothReceived; - assert.deepStrictEqual(received, [didOpen.method, didChange.method]); + // Like a connection without the hook, the writer gets the message + // synchronously, so nothing can be scheduled in between. + void client.sendNotification(notification, 1); + assert.deepStrictEqual(written, [notification.method]); + void client.sendRequest(request); + assert.deepStrictEqual(written, [notification.method, request.method]); + client.dispose(); }); test('Message strategy fails a request when handleOutgoingMessage does not call next', async () => { @@ -1014,7 +1010,7 @@ suite('Connection', () => { const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { messageStrategy: { - async handleOutgoingMessage() { + handleOutgoingMessage() { } } }); @@ -1030,7 +1026,7 @@ suite('Connection', () => { const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { messageStrategy: { - async handleOutgoingMessage() { + handleOutgoingMessage() { } } }); @@ -1039,37 +1035,12 @@ suite('Connection', () => { await assert.rejects(client.sendNotification(type, 1)); }); - test('Message strategy fails a request when a write fails and the hook does not return next', async () => { - class FailingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { - public write(): Promise { - return Promise.reject(new Error('write failed')); - } - public end(): void { - } - } - const type = new RequestType0('test/request'); - const duplexStream = new TestDuplex('ds1'); - - const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new FailingWriter(), hostConnection.NullLogger, { - messageStrategy: { - async handleOutgoingMessage(message, next) { - void next(message); - } - } - }); - client.listen(); - - await assert.rejects(client.sendRequest(type), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); - }); - - const failingHooks: { name: string; hook: (message: hostConnection.Message, next: (message: hostConnection.Message) => Promise) => Promise }[] = [ - { name: 'returns next', hook: (message, next) => next(message) }, - { name: 'awaits next', hook: async (message, next) => { await next(message); } }, - { name: 'does not return next', hook: async (message, next) => { void next(message); await new Promise(resolve => setTimeout(resolve, 10)); } }, - { name: 'throws after next', hook: async (message, next) => { void next(message); await new Promise(resolve => setTimeout(resolve, 10)); throw new Error('hook failed'); } }, + const writingHooks: { name: string; hook: (message: hostConnection.Message, next: (message: hostConnection.Message) => void) => void }[] = [ + { name: 'calls next', hook: (message, next) => next(message) }, + { name: 'throws after next', hook: (message, next) => { next(message); throw new Error('hook failed'); } }, ]; - for (const { name, hook } of failingHooks) { - test(`Message strategy does not leave a failed write unhandled when the hook ${name}`, async () => { + for (const { name, hook } of writingHooks) { + test(`Message strategy reports a failed write when the hook ${name}`, async () => { class FailingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { public write(): Promise { return Promise.reject(new Error('write failed')); @@ -1092,8 +1063,7 @@ suite('Connection', () => { client.listen(); await assert.rejects(client.sendRequest(request), (error: ResponseError) => error.code === ErrorCodes.MessageWriteError); - await assert.rejects(client.sendNotification(notification, 1)); - client.end(); + await assert.rejects(client.sendNotification(notification, 1), /write failed/); // Unhandled rejections are reported after the microtask queue drains. await new Promise(resolve => setTimeout(resolve, 20)); assert.deepStrictEqual(unhandled, []); @@ -1126,10 +1096,10 @@ suite('Connection', () => { const logger: hostConnection.Logger = { ...hostConnection.NullLogger, warn: (message) => warnings.push(message) }; const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, logger, { messageStrategy: { - async handleOutgoingMessage(message, next) { - await next(message); + handleOutgoingMessage(message, next) { + next(message); if (hostConnection.Message.isNotification(message) && message.method === first.method) { - await next(message); + next(message); } } } @@ -1143,104 +1113,68 @@ suite('Connection', () => { assert.strictEqual(warnings.length, 1); }); - for (const failure of ['throws', 'rejects']) { - test(`Message strategy keeps writing after handleOutgoingMessage ${failure}`, async () => { - const failing = new NotificationType('test/failing'); - const after = new NotificationType('test/after'); - const duplexStream1 = new TestDuplex('ds1'); - const duplexStream2 = new TestDuplex('ds2'); - - const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); - const received = new Promise((resolve) => { - server.onNotification((method) => resolve(method)); - }); - server.listen(); - - const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { - messageStrategy: { - handleOutgoingMessage(message, next) { - if (hostConnection.Message.isNotification(message) && message.method === failing.method) { - if (failure === 'throws') { - throw new Error('hook failed'); - } - return Promise.reject(new Error('hook failed')); - } - return next(message); - } - } - }); - client.listen(); - - await assert.rejects(client.sendNotification(failing, 1), /hook failed/); - await client.sendNotification(after, 2); - assert.strictEqual(await received, after.method); - }); - } - - test('Message strategy does not handle queued outgoing messages after dispose', async () => { - const first = new NotificationType('test/first'); - const second = new NotificationType('test/second'); - const duplexStream1 = new TestDuplex('ds1'); - const duplexStream2 = new TestDuplex('ds2'); - - let release!: () => void; - const released = new Promise((resolve) => release = resolve); - const handled: string[] = []; - const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { - messageStrategy: { - async handleOutgoingMessage(message, next) { - handled.push((message as hostConnection.NotificationMessage).method); - await released; - return next(message); - } - } - }); - client.listen(); - - const firstSent = client.sendNotification(first, 1); - const secondSent = client.sendNotification(second, 2); - await new Promise(resolve => setTimeout(resolve, 0)); - client.dispose(); - release(); - - await firstSent.catch(() => undefined); - await assert.rejects(secondSent, (error: hostConnection.ConnectionError) => error.code === hostConnection.ConnectionErrors.Disposed); - assert.deepStrictEqual(handled, [first.method]); - }); - - test('Message strategy does not write a message when the connection is disposed while the hook runs', async () => { - const events: string[] = []; + test('Message strategy does not write a message when next is called after handleOutgoingMessage returned', async () => { + const written: hostConnection.Message[] = []; class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { public write(message: hostConnection.Message): Promise { - events.push(`write ${(message as hostConnection.NotificationMessage).method}`); + written.push(message); return Promise.resolve(); } public end(): void { - events.push('end'); } } const type = new NotificationType('test/notification'); const duplexStream = new TestDuplex('ds1'); - let release!: () => void; - const released = new Promise((resolve) => release = resolve); - const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new RecordingWriter(), hostConnection.NullLogger, { + let calledLater!: () => void; + const nextCalledLater = new Promise(resolve => calledLater = resolve); + const warnings: string[] = []; + const logger: hostConnection.Logger = { ...hostConnection.NullLogger, warn: (message) => warnings.push(message) }; + const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new RecordingWriter(), logger, { messageStrategy: { - async handleOutgoingMessage(message, next) { - await released; - return next(message); + handleOutgoingMessage(message, next) { + setTimeout(() => { + next(message); + calledLater(); + }, 0); } } }); client.listen(); - const sent = client.sendNotification(type, 1); - await new Promise(resolve => setTimeout(resolve, 0)); - client.dispose(); - release(); + await assert.rejects(client.sendNotification(type, 1), /did not call next/); + await nextCalledLater; + assert.deepStrictEqual(written, []); + assert.strictEqual(warnings.length, 1); + }); + + test('Message strategy keeps writing after handleOutgoingMessage throws', async () => { + const failing = new NotificationType('test/failing'); + const after = new NotificationType('test/after'); + const duplexStream1 = new TestDuplex('ds1'); + const duplexStream2 = new TestDuplex('ds2'); + + const server = hostConnection.createMessageConnection(duplexStream2, duplexStream1, hostConnection.NullLogger); + const received = new Promise((resolve) => { + server.onNotification((method) => resolve(method)); + }); + server.listen(); - await assert.rejects(sent, (error: hostConnection.ConnectionError) => error.code === hostConnection.ConnectionErrors.Disposed); - assert.deepStrictEqual(events, []); + const client = hostConnection.createMessageConnection(duplexStream1, duplexStream2, hostConnection.NullLogger, { + messageStrategy: { + handleOutgoingMessage(message, next) { + if (hostConnection.Message.isNotification(message) && message.method === failing.method) { + throw new Error('hook failed'); + } + next(message); + } + } + }); + client.listen(); + + await assert.rejects(client.sendNotification(failing, 1), /hook failed/); + await client.sendNotification(after, 2); + assert.strictEqual(await received, after.method); }); test('Message strategy sends one response when handleOutgoingMessage throws after the response is written', async () => { @@ -1262,8 +1196,8 @@ suite('Connection', () => { const logger: hostConnection.Logger = { ...hostConnection.NullLogger, error: (message) => errors.push(message) }; const server = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream2), new RecordingWriter(), logger, { messageStrategy: { - async handleOutgoingMessage(message, next) { - await next(message); + handleOutgoingMessage(message, next) { + next(message); throw new Error('cleanup failed'); } } @@ -1281,37 +1215,6 @@ suite('Connection', () => { assert.ok(errors[0].includes('cleanup failed')); }); - test('Message strategy writes messages sent before end before ending the writer', async () => { - const events: string[] = []; - class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { - public write(message: hostConnection.Message): Promise { - events.push(`write ${(message as hostConnection.NotificationMessage).method}`); - return Promise.resolve(); - } - public end(): void { - events.push('end'); - } - } - const type = new NotificationType('test/notification'); - const duplexStream = new TestDuplex('ds1'); - - const client = hostConnection.createMessageConnection(new hostConnection.StreamMessageReader(duplexStream), new RecordingWriter(), hostConnection.NullLogger, { - messageStrategy: { - async handleOutgoingMessage(message, next) { - await new Promise(resolve => setTimeout(resolve, 10)); - return next(message); - } - } - }); - client.listen(); - - const sent = client.sendNotification(type, 1); - client.end(); - await sent; - await new Promise(resolve => setTimeout(resolve, 0)); - assert.deepStrictEqual(events, [`write ${type.method}`, 'end']); - }); - test('Message strategy handles responses that skip the queue', async () => { const type = new RequestType('test/handleSingleRequest'); const duplexStream1 = new TestDuplex('ds1'); From 887ffde3d651dcd527109b0d05f214f47bc50c63 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 3 Dec 2026 11:41:45 -0300 Subject: [PATCH 11/11] Handle the rejected request in the synchronous write test The writer never answers, so dispose rejects the pending request. The test now asserts that rejection instead of leaving it unhandled. --- jsonrpc/src/node/test/connection.test.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/jsonrpc/src/node/test/connection.test.ts b/jsonrpc/src/node/test/connection.test.ts index a0d4c0e6a..d8ddc0e47 100644 --- a/jsonrpc/src/node/test/connection.test.ts +++ b/jsonrpc/src/node/test/connection.test.ts @@ -971,7 +971,7 @@ suite('Connection', () => { assert.deepStrictEqual(calls, ['handleIncomingMessage']); }); - test('Message strategy writes a message before send returns', () => { + test('Message strategy writes a message before send returns', async () => { const written: string[] = []; class RecordingWriter extends hostConnection.AbstractMessageWriter implements hostConnection.MessageWriter { public write(message: hostConnection.Message): Promise { @@ -998,9 +998,12 @@ suite('Connection', () => { // synchronously, so nothing can be scheduled in between. void client.sendNotification(notification, 1); assert.deepStrictEqual(written, [notification.method]); - void client.sendRequest(request); + // The writer never answers, so dispose rejects the pending request. + const pending = client.sendRequest(request); + const rejected = assert.rejects(pending, /Pending response rejected since connection got disposed/); assert.deepStrictEqual(written, [notification.method, request.method]); client.dispose(); + await rejected; }); test('Message strategy fails a request when handleOutgoingMessage does not call next', async () => {