bellard / quickjs

Public repository of the QuickJS Javascript Engine.
https://bellard.org/quickjs
Other
8.4k stars 873 forks source link

If dynamic `import()` `BLOCK` JS thread? #168

Open meowtec opened 1 year ago

meowtec commented 1 year ago

I am trying to modify js_load_file function, with adding some long-time I/O operations inside it, such as requesting module file from remote server , or just sleep(10) for test. After add sleep(10) into js_load_file, the thread will be blocked by js_dynamic_import_job.

It would be better to make js_load_file a non-blocking function, or inside another thread.

saghul commented 1 year ago

If you use a blocking function it will surely block.

Maybe you could instead run the blocking code to fetch the module asynchronously and then pass it on to load file.

meowtec commented 1 year ago

If you use a blocking function it will surely block.

Maybe you could instead run the blocking code to fetch the module asynchronously and then pass it on to load file.

But JS_SetModuleLoaderFunc only can receive a synchronously module_loader. The type of module_loader is:

typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx,
                                        const char *module_name, void *opaque);

(I am new to C language, I think that a asynchronously function should have a callback argument, is that right?)

Link this:

typedef void JSModuleLoaderFunc(JSContext *ctx,
                                        const char *module_name, void *opaque, JSModuleLoaderCB cb);
meowtec commented 1 year ago

@saghul

BTW, I tried import() in txiki.js, and it also blocked:

setInterval(() => {
  console.log(~~(Date.now() / 1000) % 100)
}, 1000)

// a simple http server that won't finish response
// this will block timer tasks
import('http://127.0.0.1:8989/').then(res => console.log(res.test))
saghul commented 1 year ago

Good catch, I should add a timeout for http imports!

zombieyang commented 2 days ago

So, is import() still synchronous now? The only solution is to add timeout?


I think #351 is discussing something similar

saghul commented 2 days ago

Those were 2 separate issues. Imports are currently blocking.