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

fix: Unregister socket timeout listener to prevent MaxListenersExceededWarning #1993

Merged
merged 5 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(socket.getMaxListeners() - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a negative value? If so, should we add Math.max(emitter.getMaxListeners() - 1, 0) to make sure we don't set this to a negative value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense 👍 added Math.max

});
});
}

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