Skip to content

Commit 073fbd9

Browse files
fix(ws): add missing ctx.span guard in bindAsyncStart and asyncStart (#8002)
1 parent c9fcca9 commit 073fbd9

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

packages/datadog-plugin-ws/src/close.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ class WSClosePlugin extends TracingPlugin {
5757
}
5858

5959
bindAsyncStart (ctx) {
60+
if (!ctx.span) return ctx.parentStore
6061
if (!ctx.isPeerClose) ctx.span.finish()
6162
return ctx.parentStore
6263
}
6364

6465
asyncStart (ctx) {
66+
if (!ctx.span) return
6567
ctx.span.finish()
6668
}
6769

packages/datadog-plugin-ws/src/producer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ class WSProducerPlugin extends TracingPlugin {
4646
}
4747

4848
bindAsyncStart (ctx) {
49+
if (!ctx.span) return ctx.parentStore
4950
ctx.span.finish()
5051
return ctx.parentStore
5152
}
5253

5354
asyncStart (ctx) {
55+
if (!ctx.span) return
5456
ctx.span.finish()
5557
}
5658

packages/datadog-plugin-ws/src/receiver.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class WSReceiverPlugin extends TracingPlugin {
6262
}
6363

6464
asyncStart (ctx) {
65+
if (!ctx.span) return
6566
ctx.span.finish()
6667
}
6768

packages/datadog-plugin-ws/test/index.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ describe('Plugin', () => {
3131
WebSocket = require(`../../../versions/ws@${version}`).get()
3232
})
3333

34+
it('should not crash when sending on a socket without spanContext', async () => {
35+
const server = new WebSocket.Server({ port: 16015 })
36+
const connectionPromise = once(server, 'connection')
37+
38+
const socket = new WebSocket('ws://localhost:16015')
39+
const [serverSocket] = await connectionPromise
40+
await once(socket, 'open')
41+
42+
assert.strictEqual(socket.spanContext, undefined)
43+
44+
const messagePromise = once(serverSocket, 'message')
45+
await new Promise((resolve, reject) => {
46+
socket.send('test message', {}, (err) => err ? reject(err) : resolve())
47+
})
48+
await messagePromise
49+
50+
socket.close()
51+
await once(socket, 'close')
52+
server.close()
53+
})
54+
3455
it('should emit original error in case close is called before connection is established', async () => {
3556
const socket = new WebSocket('wss://localhost:12345')
3657

0 commit comments

Comments
 (0)