Skip to content

Commit c9909b1

Browse files
watsonrochdev
authored andcommitted
[DI] Ensure the tracer doesn't block instrumented app from exiting (#4993)
The `MessagePort` objects should be unref'ed (has to be after any message handler has been attached). Otherwise their handle will keep the instrumented app running. Technically there's no need to unref `port1`, but let's just unref everything show the intent.
1 parent c208d68 commit c9909b1

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
require('dd-trace/init')
4+
const http = require('http')
5+
6+
const server = http.createServer((req, res) => {
7+
res.end('hello world') // BREAKPOINT
8+
setImmediate(() => {
9+
server.close()
10+
})
11+
})
12+
13+
server.listen(process.env.APP_PORT, () => {
14+
process.send({ port: process.env.APP_PORT })
15+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const { assert } = require('chai')
4+
const { setup } = require('./utils')
5+
6+
describe('Dynamic Instrumentation', function () {
7+
const t = setup()
8+
9+
it('should not hinder the program from exiting', function (done) {
10+
// Expect the instrumented app to exit after receiving an HTTP request. Will time out otherwise.
11+
t.proc.on('exit', (code) => {
12+
assert.strictEqual(code, 0)
13+
done()
14+
})
15+
t.axios.get('/')
16+
})
17+
})

integration-tests/debugger/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
}
1919

2020
function setup () {
21-
let sandbox, cwd, appPort, proc
21+
let sandbox, cwd, appPort
2222
const breakpoint = getBreakpointInfo(1) // `1` to disregard the `setup` function
2323
const t = {
2424
breakpoint,
@@ -68,7 +68,7 @@ function setup () {
6868
}
6969

7070
before(async function () {
71-
sandbox = await createSandbox(['fastify'])
71+
sandbox = await createSandbox(['fastify']) // TODO: Make this dynamic
7272
cwd = sandbox.folder
7373
t.appFile = join(cwd, ...breakpoint.file.split('/'))
7474
})
@@ -81,7 +81,7 @@ function setup () {
8181
t.rcConfig = generateRemoteConfig(breakpoint)
8282
appPort = await getPort()
8383
t.agent = await new FakeAgent().start()
84-
proc = await spawnProc(t.appFile, {
84+
t.proc = await spawnProc(t.appFile, {
8585
cwd,
8686
env: {
8787
APP_PORT: appPort,
@@ -97,7 +97,7 @@ function setup () {
9797
})
9898

9999
afterEach(async function () {
100-
proc.kill()
100+
t.proc.kill()
101101
await t.agent.stop()
102102
})
103103

packages/dd-trace/src/debugger/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ function start (config, rc) {
5757
}
5858
)
5959

60-
worker.unref()
61-
6260
worker.on('online', () => {
6361
log.debug(`Dynamic Instrumentation worker thread started successfully (thread id: ${worker.threadId})`)
6462
})
@@ -80,6 +78,12 @@ function start (config, rc) {
8078
rcAckCallbacks.delete(ackId)
8179
}
8280
})
81+
82+
worker.unref()
83+
rcChannel.port1.unref()
84+
rcChannel.port2.unref()
85+
configChannel.port1.unref()
86+
configChannel.port2.unref()
8387
}
8488

8589
function configure (config) {

0 commit comments

Comments
 (0)