quickjs icon indicating copy to clipboard operation
quickjs copied to clipboard

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

Open meowtec opened this issue 2 years ago • 6 comments

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.

meowtec avatar Feb 18 '23 13:02 meowtec

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.

saghul avatar Feb 18 '23 15:02 saghul

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 avatar Feb 19 '23 03:02 meowtec

@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))

meowtec avatar Feb 19 '23 03:02 meowtec

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

saghul avatar Feb 19 '23 14:02 saghul