Skip to content

Commit 751eb96

Browse files
authored
fix: Added defensive code in shim to prevent crashing when checking parent segment (#2898)
1 parent dce9deb commit 751eb96

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/shim/shim.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ function recordWrapper({ shim, fn, name, recordNamer }) {
677677
return fnApply.call(fn, this, args)
678678
}
679679

680-
// middleweare recorders pass in parent segment
680+
// middleware recorders pass in parent segment
681681
// we need to destructure this as it is not needed past this function
682682
// and will overwhelm trace level loggers with logging the entire spec
683683
const { parent: specParent, ...segDesc } = spec
@@ -686,7 +686,7 @@ function recordWrapper({ shim, fn, name, recordNamer }) {
686686
const transaction = context.transaction
687687
const parent = transaction?.isActive() && specParent ? specParent : context.segment
688688

689-
if (!transaction?.isActive()) {
689+
if (!transaction?.isActive() || !parent) {
690690
shim.logger.debug('Not recording function %s, not in a transaction.', name)
691691
return fnApply.call(fn, this, arguments)
692692
}

test/unit/shim/shim.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,7 @@ test('Shim', async function (t) {
16091609
})
16101610
})
16111611

1612-
await t.test('#record wrapper when called with an inactive transaction', async function (t) {
1612+
await t.test('#record wrapper should not create a new segment when called with an inactive transaction', async function (t) {
16131613
t.beforeEach(beforeEach)
16141614
t.afterEach(afterEach)
16151615
await t.test('should not create a segment', function (t, end) {
@@ -1627,6 +1627,22 @@ test('Shim', async function (t) {
16271627
})
16281628
})
16291629

1630+
await t.test('#record wrapper should not create a new segment when there is no current active segment', function (t, end) {
1631+
const { agent, shim, wrappable } = t.nr
1632+
shim.record(wrappable, 'getActiveSegment', function () {
1633+
return new RecorderSpec({ name: 'test segment' })
1634+
})
1635+
1636+
helper.runInTransaction(agent, function (tx) {
1637+
shim.setActiveSegment(null)
1638+
const segment = wrappable.getActiveSegment()
1639+
assert.equal(segment, null)
1640+
tx.end()
1641+
assert.equal(tx.isActive(), false)
1642+
end()
1643+
})
1644+
})
1645+
16301646
await t.test('should execute the wrapped function', function (t, end) {
16311647
const { agent, shim } = t.nr
16321648
let executed = false

0 commit comments

Comments
 (0)