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

emit res close event #138

Closed
wants to merge 1 commit into from
Closed

emit res close event #138

wants to merge 1 commit into from

Conversation

cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Jan 11, 2021

This commit updates the response logic to emit the 'close' event if the request is closed.

Refs: hapijs/hapi#4208

I tested these changes in hapi's master branch (combined with the change mentioned here) on Node 15.4.0, 15.5.1 (the currently broken release), 14.15.0, and 12.18.4. 🤷

This commit updates the response logic to emit the 'close' event
if the request is closed.

Refs: hapijs/hapi#4208
Copy link
Contributor

@kanongil kanongil left a comment

Choose a reason for hiding this comment

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

This seems to do the trick but you could make it simpler, and avoid tracking the state, if you just condition the req.once('close', …) on req._shot.simulate.close.

Also, a test would be nice to ensure we don't regress.

@kanongil kanongil added the bug Bug or defect label Jan 11, 2021
@cjihrig
Copy link
Contributor Author

cjihrig commented Jan 11, 2021

This seems to do the trick but you could make it simpler, and avoid tracking the state, if you just condition the req.once('close', …) on req._shot.simulate.close.

I was hoping to detect any naturally occurring 'close' events too, and not just the simulated ones.

I'll look into adding a test.

@kanongil
Copy link
Contributor

I am inclined to perform fixes on this with surgical precision to avoid any unexpected surprises. The implementation is quite brittle.

@devinivy
Copy link
Member

devinivy commented Jan 12, 2021

It's a hard call for me. On one hand, this fix by Colin clearly enforces that when the request is closed that the response will follow, and there is value in ensuring that is always taken care of no matter the circumstance, since this is definitely what one can typically expect to observe with a real node http server. On the other hand, it is a little bit of a blunt instrument and doesn't a. give much intuition for why this is correct behavior or b. provide room for additional nuance or edge-cases if we find this isn't 100% correct. For example, I am not convinced that the nextTick() between the request close and response close occurs in practice when the underlying socket closes prematurely, but the nextTick() may be necessary for this one-size-fits-all solution (perhaps Colin will be able to confirm/deny that suspicion).

My understanding is that what really happens in the node http server is that the underlying socket closing (a la simulate.close) triggers 1. the request to be effectively destroyed (this is managed by the http Server) and 2. the response to emit a close if it hasn't already finished writing (this is managed by the HttpResponse itself). The only other way the response will close is if it finishes writing. I think it would be ideal if our solution reinforced this by either:

  • using the current solution and documenting what we know about this including any caveats by adding code comments,
  • using Gil's solution,
  • or using a similar mechanism to node, actually making the source of the simulated 'close' event the mock socket and leaning more on node's internal handling of this situation to bubble it up to the response.

@kanongil
Copy link
Contributor

My point is to keep the fix simple, since it is a whole mess to untangle otherwise. This is further complicated by the fact that the exact node behavior can change between node releases.

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

Successfully merging this pull request may close these issues.

3 participants