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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: make sure register handler when ipc emitter add listener
     When the last listener is disposed, we will remove the handler from the map.
     However, we will not add the handler back to the map when re-subscribing to the emitter,
     so the event will not be emitted anymore. It is better register handler in onWillAddFirstListener and
     delete it in onDidRemoveLastListener.
  • Loading branch information
theanarkh committed Jul 4, 2026
commit 4f1cf3b85f2ed96dd375150e761c647e63189530
5 changes: 2 additions & 3 deletions src/vs/base/parts/ipc/common/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ export class ChannelClient implements IChannelClient, IDisposable {

const emitter = new Emitter<any>({
onWillAddFirstListener: () => {
const handler: IHandler = (res: IRawResponse) => emitter.fire((res as IRawEventFireResponse).data);
this.handlers.set(id, handler);
const doRequest = () => {
this.activeRequests.add(emitter);
this.sendRequest(request);
Expand All @@ -706,9 +708,6 @@ export class ChannelClient implements IChannelClient, IDisposable {
}
});

const handler: IHandler = (res: IRawResponse) => emitter.fire((res as IRawEventFireResponse).data);
this.handlers.set(id, handler);

return emitter.event;
}

Expand Down
42 changes: 42 additions & 0 deletions src/vs/base/parts/ipc/test/common/ipc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ suite('Base IPC', function () {
assert.deepStrictEqual(messages, ['hello', 'world']);
});

test('listen to events (resubscribe)', async function () {
const onPong = ipcService.onPong;
const messages: string[] = [];

const disposable1 = onPong(msg => messages.push(msg));
await timeout(0);
assert.deepStrictEqual(messages, []);
service.ping('hello');
await timeout(0);
assert.deepStrictEqual(messages, ['hello']);
disposable1.dispose();

const disposable2 = onPong(msg => (messages as string[]).push(msg));
await timeout(0);
assert.deepStrictEqual(messages, ['hello']);
service.ping('world');
await timeout(0);
assert.deepStrictEqual(messages, ['hello', 'world']);
disposable2.dispose();
});

test('buffers in arrays', async function () {
const r = await ipcService.buffersLength([VSBuffer.alloc(2), VSBuffer.alloc(3)]);
return assert.strictEqual(r, 5);
Expand Down Expand Up @@ -443,6 +464,27 @@ suite('Base IPC', function () {
assert.deepStrictEqual(messages, ['hello', 'world']);
});

test('listen to events (resubscribe)', async function () {
const onPong = ipcService.onPong;
const messages: string[] = [];

const disposable1 = onPong(msg => messages.push(msg));
await timeout(0);
assert.deepStrictEqual(messages, []);
service.ping('hello');
await timeout(0);
assert.deepStrictEqual(messages, ['hello']);
disposable1.dispose();

const disposable2 = onPong(msg => (messages as string[]).push(msg));
await timeout(0);
assert.deepStrictEqual(messages, ['hello']);
service.ping('world');
await timeout(0);
assert.deepStrictEqual(messages, ['hello', 'world']);
disposable2.dispose();
});

test('marshalling uri', async function () {
const uri = URI.file('foobar');
const r = await ipcService.marshall(uri);
Expand Down
Loading