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: Re-add support for clientError listeners #1897

Merged
merged 2 commits into from Mar 24, 2022

Conversation

josephharrington
Copy link
Contributor

In the recent fix #1895, the clientError event was removed from the
list of events proxied from the underlying server object to the restify
Server. That fix was needed because merely adding a listener to the
underlying server for the clientError event prevents the default
behavior of returning an appropriate error code and closing the socket.
So by setting up a listener to proxy the event we were preventing that
default behavior, leading to hanging/leaked requests.

But! We should still support proxying clientError events if restify
users have added a listener for it. This enables logging or other custom
behavior. Restify users adding a clientError listener take on the
responsibility of sending an appropriate error and closing the socket,
exactly as noted in the Node.js docs for the clientError event.
https://nodejs.org/docs/latest-v16.x/api/http.html#event-clienterror

This change ensures we add a listener for clientError to the
underlying server object if and only if a listener for clientError has
been added to the restify server.

In the recent fix #1895, the `clientError` event was removed from the
list of events proxied from the underlying server object to the restify
Server. That fix was needed because merely adding a listener to the
underlying server for the `clientError` event prevents the default
behavior of returning an appropriate error code and closing the socket.
So by setting up a listener to proxy the event we were preventing that
default behavior, leading to hanging/leaked requests.

But! We should still support proxying `clientError` events if restify
users have added a listener for it. This enables logging or other custom
behavior. Restify users adding a `clientError` listener take on the
responsibility of sending an appropriate error and closing the socket,
exactly as noted in the Node.js docs for the `clientError` event.
https://nodejs.org/docs/latest-v16.x/api/http.html#event-clienterror

This change ensures we add a listener for `clientError` to the
underlying server object if and only if a listener for `clientError` has
been added to the restify server.
}).end();
});

test('Server correctly handles multiple clientError listeners', function(t) {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about this. So it is an event emitter, so obviously two handlers is technically valid, but the use case here is almost certainly to be sending a response in some cases, which shifts the burden onto each handler to make sure it properly checks before sending the response and closing the socket. If a user does this in multiple handlers they could get some pretty unexpected error results if they are not careful.

Could a better user experience from restify be to error or warn when they try to add a second handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wesleytodd, I agree generally. This is more of a contrived test rather than a valid usage example. I added this test to make sure that the proxying is done correctly / doesn't emit the proxied event multiple times if multiple listeners are added.

My goal with this change was to restore the ability to add a clientError listener after #1895 removed it, so am matching the original behavior here. I don't think folks would do this very often if at all. One actual use case might be to do the response in one handler and log in another. I'm not opposed to adding a warning but feels beyond the scope of this change.

Copy link
Member

Choose a reason for hiding this comment

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

cool, yeah makes sense. I just wanted to bring up this as I have thought before that sometimes these kind of api's from node core have foot guns, and I think frameworks are generally good places to shore up those gaps. I for sure don't think this one is a common issue worth spending much time on. So 👍 to the change!

@DonutEspresso
Copy link
Member

Naive Q - would it make sense for restify to replicate the default behavior when you attach a listener? Or is this a pretty standard Node.js expectation? It just seems strange that adding a listener would actually change behavior.

@josephharrington
Copy link
Contributor Author

Naive Q - would it make sense for restify to replicate the default behavior when you attach a listener? Or is this a pretty standard Node.js expectation? It just seems strange that adding a listener would actually change behavior.

@DonutEspresso Here is the default handler logic that is executed when no listeners are registered for clientError. I agree it feels strange that adding a listener changes behavior. In my opinion the default behavior should be disabled via an explicit option instead. But anyway. 😅

We could copy that default behavior into restify, but that feels like coupling more tightly with the underlying implementation than just conditionally proxying the event. If that default implementation in Node.js ever changed, we'd likely need to change it in restify as well anyway.

@josephharrington josephharrington merged commit 05f12a6 into master Mar 24, 2022
@josephharrington josephharrington deleted the fix-clienterror-listener branch March 24, 2022 21:14
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.

None yet

3 participants