set_interrupt_handler example usage #49
-
Hi there!, I'm trying to explore the usage of set_interrupt_handler to prevent long running or runaway functions (e.g. infinite loop etc.), but the handler is not getting called. Am I missing something? Below is the sample code where the registered interrupt handler never gets called. use futures::executor::block_on;
use log::LevelFilter;
use quickjs_runtime::esruntime::EsRuntime;
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
use quickjs_runtime::esscript::EsScript;
use quickjs_runtime::esvalue::*;
use std::sync::Arc;
use std::{thread, time};
async fn test(rt: Arc<EsRuntime>) {
let _res = rt
.eval(EsScript::new(
"basics.es",
r#"
for (let i = 0; i < 100; i++) {
rust.sleep();
console.log(i);
}
"#,
))
.await
.ok()
.unwrap();
}
fn main() {
simple_logging::log_to_stderr(LevelFilter::Info);
let mut rt = EsRuntimeBuilder::new();
rt.set_interrupt_handler(|_| {
// this handler never gets invoked
println!("in interrupt handler...");
true
});
let rt = rt.build();
rt.set_function(vec!["rust"], "sleep", |_, _| {
let one_sec = time::Duration::from_secs(1);
thread::sleep(one_sec);
Ok(1.to_es_value_facade())
})
.ok()
.expect("set sleep function failed");
block_on(test(rt));
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 12 replies
-
Hmm yes sorry.. I added the interrupt handler without testing it or adding a test-case The implementation actually did nothing, I fixed it in the main branch now. I'm not sure if it will actually work with your code, as far as I can tell the implementation in quickjs is based on calling it every x actions in quickjs, doing a sleep in rust will not add to that count and thus the interrupt handler may not be called. In my test case I do a for loop with 10k calls and the interrupt handler is called about 4 times. Hope this helps, Kind regards. |
Beta Was this translation helpful? Give feedback.
Hmm yes sorry.. I added the interrupt handler without testing it or adding a test-case
The implementation actually did nothing, I fixed it in the main branch now.
I'm not sure if it will actually work with your code, as far as I can tell the implementation in quickjs is based on calling it every x actions in quickjs, doing a sleep in rust will not add to that count and thus the interrupt handler may not be called.
In my test case I do a for loop with 10k calls and the interrupt handler is called about 4 times.
Hope this helps,
Kind regards.