Skip to content

fix: Replace request.abort with response.close in HTTP instrumentation #1510

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

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/instrumentation/core/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function wrapEmitWithTransaction(agent, emit, isHTTPS) {
function instrumentedFinish() {
// Remove listeners so this doesn't get called twice.
response.removeListener('finish', instrumentedFinish)
request.removeListener('aborted', instrumentedFinish)
response.removeListener('close', instrumentedFinish)

// Naming must happen before the segment and transaction are ended,
// because metrics recording depends on naming's side effects.
Expand Down Expand Up @@ -188,7 +188,8 @@ function wrapEmitWithTransaction(agent, emit, isHTTPS) {
transaction.end()
}
response.once('finish', instrumentedFinish)
request.once('aborted', instrumentedFinish)
// Used to detect if client has prematurely disconnected, which works in all Node.js versions currently supported (14/16/18)
response.once('close', instrumentedFinish)

return tracer.bindFunction(emit, segment).apply(this, arguments)
})
Expand Down
72 changes: 72 additions & 0 deletions test/versioned/express/client-disconnect.tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const fork = require('child_process').fork
const tap = require('tap')
const helper = require('../../lib/agent_helper')
const metrics = require('../../lib/metrics_helper')

function generateApp() {
const express = require('express')
const bodyParser = require('body-parser')

const app = express()
app.use(bodyParser.json())

app.post('/test', async function controller(req, res) {
try {
await new Promise((resolve) =>
setTimeout(() => {
resolve()
}, req.body.timeout)
)

res.status(200).send('OK')
} catch (err) {
res.status(500).send('Err')
}
})

return app
}

tap.test('Client Premature Disconnection', (t) => {
t.setTimeout(3000)
const agent = helper.instrumentMockedAgent()
const server = generateApp().listen(0)
const { port } = server.address()

t.teardown(() => {
server.close()
helper.unloadAgent(agent)
})

agent.on('transactionFinished', (transaction) => {
t.doesNotThrow(function () {
metrics.assertSegments(
transaction.trace.root,
[
'WebTransaction/Expressjs/POST//test',
[
'Nodejs/Middleware/Expressjs/query',
'Nodejs/Middleware/Expressjs/expressInit',
'Nodejs/Middleware/Expressjs/jsonParser',
'Expressjs/Route Path: /test',
['Nodejs/Middleware/Expressjs/controller', ['timers.setTimeout']]
]
],
{ exact: true }
)
}, 'should have expected segments')

t.equal(agent.getTransaction(), null, 'should have ended the transaction')
t.end()
})

const forkedRequest = fork(`${__dirname}/helpers/request.js`)
forkedRequest.send(port)
})
24 changes: 24 additions & 0 deletions test/versioned/express/helpers/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

// Polyfill till we drop support for Node 14
Copy link
Member

Choose a reason for hiding this comment

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

Should we cut a ticket to remove this when we drop 14?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually replaced the abort controller with request.destroy, so no need to use the abort controller anymore

const { AbortController } = require('node-abort-controller')
const axios = require('axios')

process.on('message', (port) => {
const controller = new AbortController()
axios
.post(`http://localhost:${port}/test`, { timeout: 1500 }, { signal: controller.signal })
.catch(() => {
// eslint-disable-next-line no-process-exit
process.exit()
})

setTimeout(() => {
controller.abort()
}, 100)
})
5 changes: 4 additions & 1 deletion test/versioned/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
"dependencies": {
"express": ">=4.6.0",
"express-enrouten": "1.1",
"ejs": "2.5.9"
"ejs": "2.5.9",
"axios": "^1.3.3",
"node-abort-controller": "^3.1.1"
},
"files": [
"app-use.tap.js",
"async-error.tap.js",
"bare-router.tap.js",
"captures-params.tap.js",
"client-disconnect.tap.js",
"errors.tap.js",
"express-enrouten.tap.js",
"ignoring.tap.js",
Expand Down