Open HarikrishnanBalagopal opened 2 years ago
That's probably a duplicate of https://github.com/nodejs/node/issues/38695
That's probably a duplicate of #38695
REPL crashes when doing a dynamic import when the working directory contains a folder with the name src:
In my case there is no folder with the name src
reproduced the same issue with a smaller piece of code
$ node -v
v16.8.0
$ node
Welcome to Node.js v16.8.0.
Type ".help" for more information.
> const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
undefined
> let res = await fetch("https://www.google.com",{method:"get"});
Segmentation fault: 11
$
After some experimentation, I found the following workaround that works in all general use cases, including loading it as a file from repl.
const fetch = (()=>{let m = import("node-fetch");return async (...args)=>await (await m).default(...args);})();
Also, if you are manually importing node-fetch in repl, the following code works as well since repl supports await
on global scope
> const fetch = (await import('node-fetch')).default;
...do something with fetch
Here is some explanation of my workaround: Original idiomatic way of importing node-fetch is the following
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
Since according to #38695, the problem has to do with premature garbage collection, I thought that if I could manually store aside the result of import()
, it could work. And so came my solution.
My workaround stores aside the result of import() as promise inside an anonymous function, and access to it using (await mod)
whenever the fetch function is called
const fetch = (() => {
let mod = import("node-fetch");
return async (...args) => await (await mod).default(...args);
})();
Shortened form, it would look like this
const fetch = (()=>{let m = import("node-fetch");return async (...args)=>await (await m).default(...args);})();
Version
v16.17.0
Platform
Darwin ibm-macbook-pro 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:25:27 PDT 2022; root:xnu-8020.141.5~2/RELEASE_X86_64 x86_64 i386 MacBookPro16,1 Darwin
Subsystem
REPL, dynamic import, abort controller, timeout
What steps will reproduce the bug?
You can copy paste the following code into the REPL to trigger the segmentation fault
How often does it reproduce? Is there a required condition?
Every time.
What is the expected behavior?
The code should not seg fault. It is possible to put the same code in a file (
index.js
) and runnode index.js
and it printstrue
. No seg faults there. Note: you need to put"type": "module"
in the package.json to useawait
at the top level.What do you see instead?
A segmentation fault when the code is run inside the REPL.
Additional information
Node was installed on MacOS Monterey version 12.5.1 using nvm https://github.com/nvm-sh/nvm
Found this segfault while trying out this tutorial on using fetch with a custom timeout https://dmitripavlutin.com/timeout-fetch-request/