Skip to content

Commit ddbe1bb

Browse files
committed
src: fix --diagnostic-dir not working with --heapsnapshot-signal
Fixes: nodejs#47842
1 parent 6a5394e commit ddbe1bb

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/heap_utils.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,18 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
454454
auto options = GetHeapSnapshotOptions(args[1]);
455455

456456
if (filename_v->IsUndefined()) {
457+
std::string dir = env->options()->diagnostic_dir;
458+
if (dir.empty()) {
459+
dir = env->GetCwd();
460+
}
457461
DiagnosticFilename name(env, "Heap", "heapsnapshot");
462+
std::string filename = dir + kPathSeparator + (*name);
458463
THROW_IF_INSUFFICIENT_PERMISSIONS(
459464
env,
460465
permission::PermissionScope::kFileSystemWrite,
461466
Environment::GetCwd(env->exec_path()));
462-
if (WriteSnapshot(env, *name, options).IsNothing()) return;
463-
if (String::NewFromUtf8(isolate, *name).ToLocal(&filename_v)) {
467+
if (WriteSnapshot(env, filename.c_str(), options).IsNothing()) return;
468+
if (String::NewFromUtf8(isolate, filename.c_str()).ToLocal(&filename_v)) {
464469
args.GetReturnValue().Set(filename_v);
465470
}
466471
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
5+
if (common.isWindows)
6+
common.skip('test not supported on Windows');
7+
8+
const assert = require('assert');
9+
10+
if (process.argv[2] === 'child') {
11+
const fs = require('fs');
12+
const path = require('path');
13+
14+
assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
15+
process.kill(process.pid, 'SIGUSR2');
16+
process.kill(process.pid, 'SIGUSR2');
17+
18+
// Asynchronously wait for the snapshot. Use an async loop to be a bit more
19+
// robust in case platform or machine differences throw off the timing.
20+
(function validate() {
21+
const files = fs.readdirSync(tmpdir.path);
22+
23+
if (files.length === 0)
24+
return setImmediate(validate);
25+
26+
assert.strictEqual(files.length, 2);
27+
28+
for (let i = 0; i < files.length; i++) {
29+
assert.match(files[i], /^Heap\..+\.heapsnapshot$/);
30+
JSON.parse(fs.readFileSync(path.join(tmpdir.path, files[i])));
31+
}
32+
})();
33+
} else {
34+
const { spawnSync } = require('child_process');
35+
36+
tmpdir.refresh();
37+
const args = [
38+
'--heapsnapshot-signal',
39+
'SIGUSR2',
40+
'--diagnostic-dir',
41+
tmpdir.path,
42+
__filename,
43+
'child',
44+
];
45+
const child = spawnSync(process.execPath, args);
46+
47+
assert.strictEqual(child.status, 0);
48+
assert.strictEqual(child.signal, null);
49+
}

0 commit comments

Comments
 (0)