Crash occurs in Node20 environment
Thank you for your great job.
The case of calling some specific API in a Node20 environment, the application seems to crash when garbage collection occurs. The issue has been confirmed in the Node20 environment on both Windows 11 and ubuntu 22.04. It does not occur in a Node18 environment. It is also independent of OpenCV version. I confirmed with v4.7.0 and v4.9.0. According to this article, there seems to have been a change in the garbage collection mechanism with Node20, but I don't know the details.
Condition
It seems to occur when operating on copyAsync() memory through map(). This issue does not happen if you use copy() instead of copyAsync() or if you use the original data as is. However, it is possible that there are other APIs besides copyAsync() that are problematic.
const { resolve } = require('node:path');
const cv = require('@u4/opencv4nodejs');
const res = resolve(__dirname, '../res/pptx_colors.png');
const img = cv.imread(res);
const createMask = (src, min, max) => {
return src.inRange(new cv.Vec3(...min), new cv.Vec3(...max));
};
const maskbits = createMask(img, [0, 0, 0], [179, 255, 255]);
const applyMask = (src, mask) => {
const masked = src
.splitChannels()
.map(c => c.bitwiseAnd(mask))
;
return new cv.Mat(masked);
};
async function main() {
while (true) {
const keyCode = cv.waitKeyEx(30);
switch (keyCode) {
case 0x1b: // 'ESC'
return;
default:
break;
}
// frame = applyMask(img, maskbits); // no problem
// const copied = img.copy(); // no problem
// frame = applyMask(copied, maskbits);
const copied = await img.copyAsync(); // this method call will be the trigger
frame = applyMask(copied, maskbits);
cv.imshow('gc test', frame);
}
}
void main();
Error Message
- Windows 11
#
# Fatal error in , line 0
# Check failed: marking_done_.
#
#
#
#FailureMessage Object: 00000052FE1FEA50
----- Native stack trace -----
1: 00007FF7C38AC94B node::SetCppgcReference+17979
2: 00007FF7C379AA6F node::TriggerNodeReport+72223
3: 00007FF7C4696FB2 V8_Fatal+162
4: 00007FF7C411AD41 v8::CppHeap::Terminate+657
5: 00007FF7C40E9700 v8::Platform::SystemClockTimeMillis+699104
6: 00007FF7C40DC8C0 v8::Platform::SystemClockTimeMillis+646304
7: 00007FF7C40DFC22 v8::Platform::SystemClockTimeMillis+659458
8: 00007FF7C40D4AFF v8::Platform::SystemClockTimeMillis+614111
9: 00007FF7C379E4D3 X509_STORE_get_cleanup+4803
10: 00007FF7C379CE86 node::TriggerNodeReport+81462
11: 00007FF7C3911E0B uv_update_time+491
12: 00007FF7C3911984 uv_run+900
13: 00007FF7C38E28B5 node::SpinEventLoop+405
14: 00007FF7C37C2CD8 X509_STORE_get_cleanup+154312
15: 00007FF7C38635BD node::Start+4909
16: 00007FF7C38622C0 node::Start+48
17: 00007FF7C361D90C AES_cbc_encrypt+151356
18: 00007FF7C4A9975C inflateValidate+19196
19: 00007FFFB94D257D BaseThreadInitThunk+29
20: 00007FFFBA9CAA48 RtlUserThreadStart+40
- Ubuntu 22.04 (WSL2)
#
# Fatal error in , line 0
# Check failed: marking_done_.
#
#
#
#FailureMessage Object: 0x7ffd5b79b400
----- Native stack trace -----
1: 0xd491d1 [node]
2: 0x2169281 V8_Fatal(char const*, ...) [node]
3: 0x10c07c9 v8::internal::CppHeap::TraceEpilogue() [node]
4: 0x1127d43 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::internal::GarbageCollectionReason, char const*) [node]
5: 0x11285bc v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
6: 0x11292da v8::internal::Heap::FinalizeIncrementalMarkingIfComplete(v8::internal::GarbageCollectionReason) [node]
7: 0x112ac6a v8::internal::IncrementalMarkingJob::Task::RunInternal() [node]
8: 0xd49166 [node]
9: 0xd4c70f node::PerIsolatePlatformData::FlushForegroundTasksInternal() [node]
10: 0x18bf0b3 [node]
11: 0x18d3b2b [node]
12: 0x18bfdd7 uv_run [node]
13: 0xbc9be6 node::SpinEventLoopInternal(node::Environment*) [node]
14: 0xd1d921 [node]
15: 0xd1e38d node::NodeMainInstance::Run() [node]
16: 0xc710bf node::Start(int, char**) [node]
17: 0x7f2ee6006d90 [/lib/x86_64-linux-gnu/libc.so.6]
18: 0x7f2ee6006e40 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
19: 0xbc7afe _start [node]
Trace/breakpoint trap
How to reproduce the issue
-
Unzip cv-crash-case.zip
-
npm ci -
npm run rebuild-
node ./node_modules/@u4/opencv4nodejs/bin/install.js rebuild
-
-
npm start- launch sample app & will be crashed in Node20
OpenCV version : 4.9.0 (4.7.0)
With OpenCV-contrib? : no
OS: Windows 11, Ubuntu 22.04 (WSL2)
Build Tools (Win): Visual Studio Build Tools 2017 (15.9.42)
I hope this report helps you solve this problem.
I'm currently working on the V7.
I will invite you soon to try it :)
It seems to occur when operating on copyAsync() memory through map(). This issue does not happen if you use copy() instead of copyAsync() or if you use the original data as is.
It seems that you can reproduce it just by calling copyAsync(). The processing in applyMask() was not necessary.
+ const frame = await img.copyAsync(); // this method call will be the trigger
- const copied = await img.copyAsync(); // this method call will be the trigger
- frame = applyMask(copied, maskbits);
you can try the Version 7.0.0-alpha-5.
the version should works as ESM and CJS
7.0.0-alpha-6 is out, and works fine as ESM.
@u4/[email protected] exports were incorrect. @u4/[email protected] exports are workings
@u4/opencv4nodejs@7 is out.
Although v7.0.0 has been released, the installation fails in my environment due to the following problem.
npm ERR! UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 43505: invalid start byte
It looks like the python logic is treating something as UTF-8. But I am wondering because this problem does not occur in v6.5.3 in the same environment.
Attach the log below.
npm error log
$ npm i
npm ERR! code 1
npm ERR! path C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\@u4\opencv4nodejs
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp info find Python using Python version 3.10.9 found at "C:\Users\ogata\AppData\Local\Programs\Python\Python310\python.exe"
npm ERR! gyp info find VS using VS2017 (15.9.28307.1759) found at:
npm ERR! gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools"
npm ERR! gyp info find VS run with --verbose for detailed information
npm ERR! gyp info spawn C:\Users\ogata\AppData\Local\Programs\Python\Python310\python.exe
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args 'C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\node-gyp\\gyp\\gyp_main.py',
npm ERR! gyp info spawn args 'binding.gyp',
npm ERR! gyp info spawn args '-f',
npm ERR! gyp info spawn args 'msvs',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args 'C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\@u4\\opencv4nodejs\\build\\config.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args 'C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\node-gyp\\addon.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args 'C:\\Users\\ogata\\AppData\\Local\\node-gyp\\Cache\\20.12.2\\include\\node\\common.gypi',
npm ERR! gyp info spawn args '-Dlibrary=shared_library',
npm ERR! gyp info spawn args '-Dvisibility=default',
npm ERR! gyp info spawn args '-Dnode_root_dir=C:\\Users\\ogata\\AppData\\Local\\node-gyp\\Cache\\20.12.2',
npm ERR! gyp info spawn args '-Dnode_gyp_dir=C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\node-gyp',
npm ERR! gyp info spawn args '-Dnode_lib_file=C:\\\\Users\\\\ogata\\\\AppData\\\\Local\\\\node-gyp\\\\Cache\\\\20.12.2\\\\<(target_arch)\\\\node.lib',
npm ERR! gyp info spawn args '-Dmodule_root_dir=C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\@u4\\opencv4nodejs',
npm ERR! gyp info spawn args '-Dnode_engine=v8',
npm ERR! gyp info spawn args '--depth=.',
npm ERR! gyp info spawn args '--no-parallel',
npm ERR! gyp info spawn args '--generator-output',
npm ERR! gyp info spawn args 'C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\@u4\\opencv4nodejs\\build',
npm ERR! gyp info spawn args '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! Traceback (most recent call last):
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\gyp_main.py", line 45, in <module>
npm ERR! sys.exit(gyp.script_main())
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 688, in script_main
npm ERR! return main(sys.argv[1:])
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 680, in main
npm ERR! return gyp_main(args)
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 647, in gyp_main
npm ERR! [generator, flat_list, targets, data] = Load(
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 157, in Load
npm ERR! result = gyp.input.Load(
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 2985, in Load
npm ERR! LoadTargetBuildFile(
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 427, in LoadTargetBuildFile
npm ERR! ProcessVariablesAndConditionsInDict(
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 1399, in ProcessVariablesAndConditionsInDict
npm ERR! ProcessVariablesAndConditionsInList(value, phase, variables, build_file)
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 1412, in ProcessVariablesAndConditionsInList
npm ERR! ProcessVariablesAndConditionsInDict(item, phase, variables, build_file)
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 1399, in ProcessVariablesAndConditionsInDict
npm ERR! ProcessVariablesAndConditionsInList(value, phase, variables, build_file)
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 1416, in ProcessVariablesAndConditionsInList
npm ERR! expanded = ExpandVariables(item, phase, variables, build_file)
npm ERR! File "C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 980, in ExpandVariables
npm ERR! replacement = result.stdout.decode("utf-8").rstrip()
npm ERR! UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 43505: invalid start byte
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1
npm ERR! gyp ERR! stack at ChildProcess.<anonymous> (C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\node-gyp\lib\configure.js:297:18)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:518:28)
npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:294:12)
npm ERR! gyp ERR! System Windows_NT 10.0.22631
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Work\\Instore\\workspace\\_survey\\cv-crash-case_fix\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\@u4\opencv4nodejs
npm ERR! gyp ERR! node -v v20.12.2
npm ERR! gyp ERR! node-gyp -v v10.1.0
npm ERR! gyp ERR! not ok
npm ERR! A complete log of this run can be found in: C:\Users\ogata\AppData\Local\npm-cache\_logs\2024-05-07T01_43_06_718Z-debug-0.log
Also, in the change logs,
Native code is now generated directly by the default node-gyp rebuild.
However, is there a way to prevent this when npm install? This behavior is not desirable for me. Since I switched the flags passed to cmake as shown below on Windows and Ubuntu, I always called the script provided by opencv-build to build. This is because passing an OR of these flags will cause a build error.
- Windows:
-
-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>
-
- Ubuntu:
-
-DCMAKE_BUILD_RPATH=$ORIGIN
-
I notice some strange error with nodeJS 22.
what is your environement ?
what is your environement ?
Below is my environment.
NodeJS: v20.12.2
npm: v10.5.0
OpenCV version : 4.9.0
With OpenCV-contrib? : no
OS: Windows 11
Build Tools (Win): Visual Studio Build Tools 2017 (15.9.42)
@u4/opencv4nodejs: v7.0.0
node-gyp: v10.1.0
Some progress has been made on installation issues. This issue is caused by a combination of several previous changes.
factor
-
By defining the
rebuildscript,node-gyp rebuildis called duringnpm install-
rebuildis a built-in command. We can suppress its execution withnpm i --ignore-scripts. - Therefore, there is a high possibility that it is running before building opencv. If this is not the intended behavior, I recommend renaming the
rebuild.
-
-
It seems that
@std/fmt/0.223.0/colors.jsis being used incorrectly inopencv-build.- Even if we use
npm i --ignore-scriptsto avoid the first problem, another problem #138 occured. - The cause is that a boolean is passed to
@std/fmt/0.223.0/colors.js L117.
function run(str, code) { console.log(`★str: ${str}`); return enabled ? `${code.open}${str.replace(code.regexp, code.open)}${code.close}` : str; }★str: %s ★str: %s info config found opencv4nodejs section in C:\Work\Instore\workspace\_survey\cv-crash-case_fix\package.json info envAutodetect autodetect 0 changes info init no_autobuild is set. info init info applyEnvsFromPackageJson the following opencv4nodejs environment variables are set in the package.json: ★str: autoBuildOpencvVersion ★str: autoBuildOpencvVersion ★str: 4.9.0 ★str: 4.9.0 info applyEnvsFromPackageJson autoBuildOpencvVersion: 4.9.0 ★str: autoBuildFlags ★str: autoBuildFlags ★str: -DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui ★str: -DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui info applyEnvsFromPackageJson autoBuildFlags: -DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui ★str: autoBuildWithoutContrib ★str: autoBuildWithoutContrib ★str: true info init no_autobuild is set. info init info applyEnvsFromPackageJson the following opencv4nodejs environment variables are set in the package.json: ★str: autoBuildOpencvVersion ★str: autoBuildOpencvVersion ★str: 4.9.0 ★str: 4.9.0 info applyEnvsFromPackageJson autoBuildOpencvVersion: 4.9.0 ★str: autoBuildFlags ★str: autoBuildFlags ★str: -DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui ★str: -DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui info applyEnvsFromPackageJson autoBuildFlags: -DBUILD_LIST=core,imgproc,imgcodecs,videoio,highgui ★str: autoBuildWithoutContrib ★str: autoBuildWithoutContrib ★str: true C:\Work\Instore\workspace\_survey\cv-crash-case_fix\node_modules\@u4\opencv-build\script\deps\jsr.io\@std\fmt\0.223.0\colors.js:120 ? `${code.open}${str.replace(code.regexp, code.open)}${code.close}` ^ TypeError: str.replace is not a function- It also seems to be misused when generating parameters for
vcxproj, causing build failures on Win32. In the end, I fixed it as follows and got around it.
function run(str, code) { console.log(`★str: ${str}`); return String(str); } - Even if we use
-
It seems that the
cwdsettings and-fspecification inopencv4nodejsare incorrect. I modified as belows.- cjs/install/compileLib.js L302
// let flags = ' -f binding_old.gyp'; let flags = '';- cjs/install/compileLib.js L315
// const cwd = path_1.default.join(__dirname, '..'); const cwd = path_1.default.join(__dirname, '../..');
By applying the above patch, I was just able to build and run it.
Unfortunately, I have confirmed that the crash problem still occurs even with v7.0.0 installed with the above fix.
However, it is difficult to reproduce when using only copyAsync(), but it can be reproduced when the debugger is started or when using applyMask() which was first reported.
How to reproduce the issue
-
Unzip cv-crash-case_v7.zip
-
npm run setup -
npm run patch-
node ./tasks/patch.js
-
-
npm run build-
node ./node_modules/@u4/opencv4nodejs/bin/install.js rebuild
-
-
npm start- launch sample app & will be crashed in Node20
NodeJS : 20.12.2 npm : 10.5.0 OpenCV version : 4.9.0 With OpenCV-contrib? : no OS: Windows 11 Build Tools (Win): Visual Studio Build Tools 2017 (15.9.42)