opencv4nodejs icon indicating copy to clipboard operation
opencv4nodejs copied to clipboard

`@u4/opencv4nodejs` cannot be used in worker threads

Open mf-mitchelrydzynski opened this issue 2 months ago • 0 comments

Environment

  • opencv4nodejs: 6.5.2
  • openvc: 4.9.0
  • nodejs: 18.16.1
  • macos: sonoma 14.4.1

Description of Issue

I'm attempting to offload image processing to workers using the native worker_threads library. The first time my application starts up a worker, runs the computation, and cleans up the worker, things work great. However, the second attempt to do so causes my whole app to crash silently. I noticed that commenting out any imports of @u4/opencv4nodejs in the worker files allows them to start and terminate as often as I like without issue. Upon investigating, I found the below resources related to context aware c++ addons.

Additional Resources

  • https://github.com/justadudewhohacks/opencv4nodejs/issues/555 - same open issue in the upstream repo
  • https://github.com/nodejs/node/issues/21481 - closed issue in nodejs further describing the problem
  • https://nodejs.org/docs/latest-v10.x/api/addons.html#addons_context_aware_addons - relevant nodejs documentation
  • https://github.com/UrielCh/opencv4nodejs/issues/43 - open issue in this library, same fundamental problem showing up there because electron runs multiple node instances

Given the computationally intensive nature of image processing, supporting workers in order to not block the main thread seems like it should be a requirement for this library. Creating a long running subprocess is not an option for me due to memory constraints. Happy to provide any more info/help land on a solution. Thanks!

mf-mitchelrydzynski avatar May 07 '24 19:05 mf-mitchelrydzynski