hapijs / hapijs/hapi

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

Open
#4,244 6 comments 6 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
14.8k
Forks
1.4k
Avg merge
22d 3h
Merged PRs (30d)
1

Description

#### Support plan

* *is this issue currently blocking your project?* (yes/no): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: 12.13.1, 14.15.1
* *module version with issue*: 19.2.0, 20.0.1, 20.1.2 (maybe others)
* *last module version without issue*: unsure
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi-application, another framework
* *any other relevant information*: similar to https://github.com/hapijs/hapi/issues/4072

#### What are you trying to achieve or the steps to reproduce?

```js
const Hapi = require('@hapi/hapi');

const server = Hapi.server({
host: 'localhost',
port: 8080,
});

server.route({
method: 'GET',
path: '/abort-test',
handler: async function() {
return new Promise(function (resolve) {
setTimeout(function () {
resolve('OK');
}, 4000);
});
},
})

server.events.on('request', (request, event) => {
console.log(`Event: ${JSON.stringify(event)}`);
});

server.events.on('response', (request) => {
console.log(`Response Status Code: ${request.response.statusCode}`);
});

server.start();
console.log(`Server running at: ${server.info.uri}`);
```

```
curl --max-time 1 http://localhost:8080/abort-test
```

#### What was the result you got?
```
Server running at: http://localhost:8080
Event: {"request":"","timestamp":1584125752386,"tags":["request","error","abort"],"channel":"internal"}
Response Status Code: 200
```
[Native node.js response.end](https://nodejs.org/docs/latest-v14.x/api/http.html#http_response_end_data_encoding_callback) isn't called.

#### What result did you expect?
`response.end` called, response status code to be 499.

#### Additional info
We're using [opentelemetry.js](https://github.com/open-telemetry/opentelemetry-js) to collect metrics from our application, it has [a module](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation-http) that automatically collects HTTP metrics by wrapping native node's `http` and `https` modules.
It works fine with usual HTTP requests to our hapi-app, but when a request is aborted on a client side - it doesn't collect metrics.
I assume that it's because Hapi.js doesn't call native `response.end` for aborted requests, [which is wrapped ](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-instrumentation-http/src/http.ts#L428) with opentelemetry module. It works fine with native node.js HTTP service (because it calls `response.end` even for aborted requests). So it would be nice to get some help and understand if there a bug in Hapi request abort mechanism or should we deal some specific way with aborted requests to be able to collect metrics in this case.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.