Skip to content

Commit

Permalink
fix: Unregister socket timeout listener to prevent MaxListenersExceed…
Browse files Browse the repository at this point in the history
…edWarning (#1993)

* fix: Unregister socket timeout listener to prevent MaxListenersExceededWarning

* fix: Prevent negative setting max listeners to negative value
  • Loading branch information
dwirz committed Dec 13, 2022
1 parent 46d562d commit 1a34bc4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/utils/api-request.ts
Expand Up @@ -505,7 +505,12 @@ class AsyncHttpCall {
// Listen to timeouts and throw an error.
req.setTimeout(timeout, timeoutCallback);
req.on('socket', (socket) => {
socket.setMaxListeners(socket.getMaxListeners() + 1);
socket.setTimeout(timeout, timeoutCallback);
socket.on('end', () => {
socket.setTimeout(0);
socket.setMaxListeners(Math.max(socket.getMaxListeners() - 1, 0));
});
});
}

Expand Down
18 changes: 18 additions & 0 deletions test/integration/auth.spec.ts
Expand Up @@ -33,10 +33,13 @@ import {
TenantAwareAuth, UpdatePhoneMultiFactorInfoRequest, UpdateTenantRequest, UserImportOptions,
UserImportRecord, UserRecord, getAuth, UpdateProjectConfigRequest, UserMetadata,
} from '../../lib/auth/index';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';

const chalk = require('chalk'); // eslint-disable-line @typescript-eslint/no-var-requires

chai.should();
chai.use(sinonChai);
chai.use(chaiAsPromised);

const expect = chai.expect;
Expand Down Expand Up @@ -103,6 +106,7 @@ function clientAuth(): FirebaseAuth {
describe('admin.auth', () => {

let uidFromCreateUserWithoutUid: string;
const processWarningSpy = sinon.spy();

before(() => {
firebase.initializeApp({
Expand All @@ -112,10 +116,24 @@ describe('admin.auth', () => {
if (authEmulatorHost) {
(clientAuth() as any).useEmulator('http://' + authEmulatorHost);
}
process.on('warning', processWarningSpy);
return cleanup();
});

afterEach(() => {
expect(
processWarningSpy.neverCalledWith(
sinon.match(
(warning: Error) => warning.name === 'MaxListenersExceededWarning'
)
),
'process.on("warning") was called with an unexpected MaxListenersExceededWarning.'
).to.be.true;
processWarningSpy.resetHistory();
});

after(() => {
process.removeListener('warning', processWarningSpy);
return cleanup();
});

Expand Down

0 comments on commit 1a34bc4

Please sign in to comment.