Skip to content

End the response with disconnectStatusCode when the client disconnects - #4596

Open
SahinurDEV wants to merge 1 commit into
hapijs:masterfrom
SahinurDEV:fix/4244-disconnect-response
Open

SahinurDEV wants to merge 1 commit into
hapijs:masterfrom
SahinurDEV:fix/4244-disconnect-response

Conversation

@SahinurDEV

Copy link
Copy Markdown

Fixes #4244.

When the client disconnects before hapi replies, two things go wrong:

  • The native res.end() is never called, so http instrumentation (e.g. OpenTelemetry) never sees the request finish. On master, curl --max-time 1 against the route in the issue logs a 499 'response' event but res.end is not called.
  • If the disconnect shows up only as a response 'close' with no request 'aborted' (for example, the payload was fully read, or the client leaves during onPreResponse), the request is still reported with the handler's 200.

Changes in lib/request.js:

  • A response 'close' before the reply is handled the same way as 'aborted': reply with the disconnectStatusCode error and emit 'disconnect'.
  • A disconnect during onPreResponse replaces the response with that error. The original response is kept in data.
  • _abort() now ends the native response with the error's status code. It only skipped this before for h.close, and it still skips it for h.abandon.

API.md already describes disconnectStatusCode as applying when "the request is closed or aborted", so the docs don't change.

Tests: three new cases in test/request.js (abort during handler, close after full payload with a custom disconnectStatusCode, disconnect during onPreResponse). All three fail on master. Payload handles request socket close now expects 499 instead of 500. npm test gives 1009 tests, 100% coverage, lint and types clean.

AI disclosure: I used Grok Bot to help trace the lifecycle and write the tests. The commit has an Assisted-by: trailer. I ran the full suite and the issue's curl repro locally before and after the change.

When the client went away before hapi replied, the native response was
never ended, and a response 'close' without a request 'aborted' (e.g. a
GET whose payload was fully read, or a disconnect during onPreResponse)
was reported with the handler's 200 instead of the disconnect error.

Treat response 'close' like 'aborted', report a disconnect during
onPreResponse as the disconnect error, and end the native response with
that status code so http instrumentation sees the request finish.

Fixes hapijs#4244

Assisted-by: Grok Bot
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.

Response event for aborted requests has status code 200 and doesn't call native response.end

1 participant