-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling async API from node worker threads results in errors #489
Comments
@kdy1 Thank you for the report! Would you be able to provide us with a small code example so we can reproduce what you're trying to make work? Thank you! |
@dherman I've created a basic version of module which invokes native addon from worker thread. |
Worker support is documented here: |
@kdy1 seems like it not work now.
|
@Lyoko-Jeremie The N-API backend does not have this issue. |
@kjvalencik where/when can i have it ? |
Enable it in your Docs for |
same reasult in the worker_thread
i use the config
code pub fn async_task(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let undefined = cx.undefined();
let cb = cx.argument::<JsFunction>(0)?.root(&mut cx);
let queue = cx.queue();
std::thread::spawn(move || {
println!("sleeping: {:?}", thread::current().id());
thread::sleep(std::time::Duration::from_secs(5));
println!("sleeping done");
queue.send(move |mut cx| {
let callback = cb.into_inner(&mut cx);
let this = cx.undefined();
let null = cx.null();
println!("Inside schedule fn");
let args: Vec<Handle<JsValue>> = vec![
cx.string("abc").upcast::<JsValue>(),
cx.number(100).upcast()
];
callback.call(&mut cx, this, args)?;
Ok(())
});
println!("scheduled");
});
Ok(undefined)
}
register_module!(mut cx, {
cx.export_function("async_task", async_task)?;
Ok(())
}); test it use : // test.js
// https://github.com/neon-bindings/neon/issues/489
// https://github.com/kdy1/neon-worker-thread
const {
Worker,
isMainThread,
parentPort,
workerData
} = require("worker_threads");
const rxjs = require('rxjs');
if (isMainThread) {
module.exports = function parseJSAsync(script) {
const s=new rxjs.Subject();
const worker = new Worker(__filename, {
workerData: script
});
worker.on("message", s.next.bind(s));
worker.on("error", s.error.bind(s));
worker.on("exit", code => {
if (code !== 0)
s.error(new Error(`Worker stopped with exit code ${code}`));
console.log(`Worker stopped with exit code ${code}`);
s.complete();
});
return s;
};
} else {
const addon = require('../native');
addon.async_task(function (err, v) {
parentPort.postMessage("Error: ", err);
parentPort.postMessage("Result: ", v);
});
setTimeout(() => {
console.error('end')
}, 1000 * 8)
} // run.js
const t = require('./test')
console.log(t);
t().subscribe(
value => {
console.log(value)
},
error => {
console.error(error)
},
() => {
console.log('completed')
}
); |
@Lyoko-Jeremie Can you try deleting I copied your code into a project and made minimal changes:
I ran the test 1000x and it passed successfully each time without the error.
|
is it my node verion too low ? i will try to upgrade it. |
now i upgrade the nodejs
and a new problem happend
i can see the if i switch back to v0.6.0 like follow , all build will work well.
where am i wrong ? |
now i install nightly version,
BTW: if i remove the |
oh, i use the #662 fixed the dll issue. now the new napi-6 is work. |
now i get a is it not impl now ? or some feature flag not enable ? or it will be delete in future ? @kjvalencik |
Task will not be implemented in the N-API backend. It is completely removed in the next unreleased version. Instead, use your own thread pool and callback in with |
@kjvalencik thank you ~ |
Closing since this does not apply to the Node-API backend. Also, I found out I was wrong on the usefulness of node worker pool tasks and they will be implemented. neon-bindings/rfcs#35 |
Related: swc-project/node-swc#21
Error message:
FATAL ERROR: HandleScope::HandleScope Entering the V8 API without proper locking in place
The text was updated successfully, but these errors were encountered: