Skip to content

Commit 1a34bc4

Browse files
authored
fix: Unregister socket timeout listener to prevent MaxListenersExceededWarning (#1993)
* fix: Unregister socket timeout listener to prevent MaxListenersExceededWarning * fix: Prevent negative setting max listeners to negative value
1 parent 46d562d commit 1a34bc4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/utils/api-request.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,12 @@ class AsyncHttpCall {
505505
// Listen to timeouts and throw an error.
506506
req.setTimeout(timeout, timeoutCallback);
507507
req.on('socket', (socket) => {
508+
socket.setMaxListeners(socket.getMaxListeners() + 1);
508509
socket.setTimeout(timeout, timeoutCallback);
510+
socket.on('end', () => {
511+
socket.setTimeout(0);
512+
socket.setMaxListeners(Math.max(socket.getMaxListeners() - 1, 0));
513+
});
509514
});
510515
}
511516

test/integration/auth.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ import {
3333
TenantAwareAuth, UpdatePhoneMultiFactorInfoRequest, UpdateTenantRequest, UserImportOptions,
3434
UserImportRecord, UserRecord, getAuth, UpdateProjectConfigRequest, UserMetadata,
3535
} from '../../lib/auth/index';
36+
import * as sinon from 'sinon';
37+
import * as sinonChai from 'sinon-chai';
3638

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

3941
chai.should();
42+
chai.use(sinonChai);
4043
chai.use(chaiAsPromised);
4144

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

105108
let uidFromCreateUserWithoutUid: string;
109+
const processWarningSpy = sinon.spy();
106110

107111
before(() => {
108112
firebase.initializeApp({
@@ -112,10 +116,24 @@ describe('admin.auth', () => {
112116
if (authEmulatorHost) {
113117
(clientAuth() as any).useEmulator('http://' + authEmulatorHost);
114118
}
119+
process.on('warning', processWarningSpy);
115120
return cleanup();
116121
});
117122

123+
afterEach(() => {
124+
expect(
125+
processWarningSpy.neverCalledWith(
126+
sinon.match(
127+
(warning: Error) => warning.name === 'MaxListenersExceededWarning'
128+
)
129+
),
130+
'process.on("warning") was called with an unexpected MaxListenersExceededWarning.'
131+
).to.be.true;
132+
processWarningSpy.resetHistory();
133+
});
134+
118135
after(() => {
136+
process.removeListener('warning', processWarningSpy);
119137
return cleanup();
120138
});
121139

0 commit comments

Comments
 (0)