Skip to content

Commit

Permalink
fix: wrap 'fs.realpath.native' when configured with asyncHooks=false (#…
Browse files Browse the repository at this point in the history
…2403)

Before this `fs.realpath.native` would be undefined after wrapping
of `fs.realpath`.

Fixes: #2401
  • Loading branch information
trentm authored Oct 29, 2021
1 parent 2b98287 commit 9084f08
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Notes:
[float]
===== Bug fixes
* Wrap `fs.realpath.native` when configured with `asyncHooks=false`. This
fixes using that function (which was undefined before this fix) and a
crash when importing fs-extra@10. ({issues}2401[#2401])
* A significant change was made to internal run context tracking (a.k.a. async
context tracking). There are no configuration changes or API changes for
custom instrumentation. ({pull}2181[#2181])
Expand Down
9 changes: 9 additions & 0 deletions lib/instrumentation/patch-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ module.exports = function (ins) {
if (dns.resolveNaptr) wrap(dns, 'resolveNaptr', activator)

var fs = require('fs')
var wrappedFsRealpathNative
if (fs.realpath.native) {
wrappedFsRealpathNative = wrap(fs.realpath, 'native', activator)
}

massWrap(
fs,
[
Expand Down Expand Up @@ -287,6 +292,10 @@ module.exports = function (ins) {
activator
)

if (wrappedFsRealpathNative) {
fs.realpath.native = wrappedFsRealpathNative
}

// only wrap lchown and lchmod on systems that have them.
if (fs.lchown) wrap(fs, 'lchown', activator) // eslint-disable-line node/no-deprecated-api
if (fs.lchmod) wrap(fs, 'lchmod', activator) // eslint-disable-line node/no-deprecated-api
Expand Down
33 changes: 33 additions & 0 deletions test/instrumentation/fs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

// Some testing of run-context tracking through 'fs' methods.

const apm = require('../..').start({
serviceName: 'test-fs',
captureExceptions: false,
metricsInterval: '0s',
centralConfig: false,
cloudProvider: 'none',
disableSend: true,
asyncHooks: false
})

const fs = require('fs')

const tape = require('tape')

if (typeof fs.realpath.native === 'function') {
// Before https://github.com/elastic/apm-agent-nodejs/issues/2401 this test
// would crash with asyncHooks=false
tape.test('fs.realpath.native', function (t) {
var trans = apm.startTransaction('t0')
var span = apm.startSpan('s1')
fs.realpath.native(__filename, function (err, resolvedPath) {
t.error(err, 'no error from fs.realpath.native')
t.equal(apm.currentSpan, span, 'apm.currentSpan is as expected')
span.end()
trans.end()
t.end()
})
})
}

0 comments on commit 9084f08

Please sign in to comment.