Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Don't set up more than 1 abort signal listener #503

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/completion-listener.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getEventListeners, getMaxListeners } from 'events';
import { TestScheduler } from 'rxjs/testing';

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

it('does not leak memory when listening for abort signals', () => {
const abortCtrl = new AbortController();
const maxListeners = getMaxListeners(abortCtrl.signal);
createController().listen(
Array.from({ length: maxListeners + 1 }, () => new FakeCommand()),
abortCtrl.signal,
);
expect(getEventListeners(abortCtrl.signal, 'abort')).toHaveLength(1);
});

it('check for success once all commands have emitted at least a single close event', async () => {
const finallyCallback = jest.fn();
const result = createController().listen(commands).finally(finallyCallback);
Expand Down
5 changes: 4 additions & 1 deletion src/completion-listener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Rx from 'rxjs';
import { delay, filter, map, switchMap, take } from 'rxjs/operators';
import { delay, filter, map, share, switchMap, take } from 'rxjs/operators';

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

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

const closeStreams = commands.map((command) =>
Expand Down
Loading