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: use close event on response instead of socket #1892

Merged
merged 1 commit into from
Feb 9, 2022

Conversation

josephharrington
Copy link
Contributor

@josephharrington josephharrington commented Jan 31, 2022

In #1880, we switched from using the close event on req to close on
req.socket. This addressed the intended issue but can trigger frequent
warnings when keep-alive is used due to a listener being added for each
request on the same socket.

By using the close event on res instead, we address both the issue of
event ordering in Node.js >= 16 that the original change was targeting
and the event emitter warning leak.

Fixes #1883.

In #1880, we switched from using the close event on `req` to close on
`req.socket`. This addressed the intended issue but can trigger frequent
warnings when keep-alive is used due to a listener being added for each
request on the same socket.

By using the close event on `res` instead, we address both the issue of
event ordering in Node.js >= 16 that the original change was targeting
and the event emitter warning leak.
@wesleytodd
Copy link
Member

wesleytodd commented Jan 31, 2022

Am I missing something here? IIUC a client stopping in the middle of a POST (as the comment mentions), it could be that the res isn't closed, thus this might re-introduce the leak?

EDIT: I wonder if keeping both the socket and res close events would work?

@josephharrington
Copy link
Contributor Author

Am I missing something here? IIUC a client stopping in the middle of a POST (as the comment mentions), it could be that the res isn't closed, thus this might re-introduce the leak?

I believe res is closed whenever req.socket is closed. The reason it's preferable to req's close event is that it is not affected by the autodestroy behavior change in Node.js 16.

I validated that the res close event reliably fires by manual testing of malformed and hanging requests, but if there's a specific flow you have in mind that would leave res open I am happy to try it (and add a test for it).

EDIT: I wonder if keeping both the socket and res close events would work?

Then we'd still have the event emitter leak warning, yes?

Copy link
Contributor

@mmarchini mmarchini left a comment

Choose a reason for hiding this comment

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

this should be fine and unless someone can come up with a scenario where this would leak memory/resources we should merge it (since it's better than the current state of things)

@josephharrington josephharrington merged commit 5c7eb95 into master Feb 9, 2022
@josephharrington josephharrington deleted the fix-listeners-issue branch February 9, 2022 19:49
@svedbg
Copy link

svedbg commented Feb 10, 2022

Please release it in npm too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MaxListenerExceededWarning with restify 8.6.0
4 participants