Skip to content

Commit 041520b

Browse files
authored
Don't set up more than 1 abort signal listener (#503)
1 parent a7a5894 commit 041520b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/completion-listener.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getEventListeners, getMaxListeners } from 'events';
12
import { TestScheduler } from 'rxjs/testing';
23

34
import { CloseEvent } from './command';
@@ -56,6 +57,16 @@ describe('listen', () => {
5657
await expect(result).resolves.toHaveLength(0);
5758
});
5859

60+
it('does not leak memory when listening for abort signals', () => {
61+
const abortCtrl = new AbortController();
62+
const maxListeners = getMaxListeners(abortCtrl.signal);
63+
createController().listen(
64+
Array.from({ length: maxListeners + 1 }, () => new FakeCommand()),
65+
abortCtrl.signal,
66+
);
67+
expect(getEventListeners(abortCtrl.signal, 'abort')).toHaveLength(1);
68+
});
69+
5970
it('check for success once all commands have emitted at least a single close event', async () => {
6071
const finallyCallback = jest.fn();
6172
const result = createController().listen(commands).finally(finallyCallback);

src/completion-listener.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Rx from 'rxjs';
2-
import { delay, filter, map, switchMap, take } from 'rxjs/operators';
2+
import { delay, filter, map, share, switchMap, take } from 'rxjs/operators';
33

44
import { CloseEvent, Command } from './command';
55

@@ -101,6 +101,9 @@ export class CompletionListener {
101101
// without an immediate delay
102102
delay(0, this.scheduler),
103103
map(() => undefined),
104+
// #502 - node might warn of too many active listeners on this object if it isn't shared,
105+
// as each command subscribes to abort event over and over
106+
share(),
104107
);
105108

106109
const closeStreams = commands.map((command) =>

0 commit comments

Comments
 (0)