Skip to content
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

Cancel/Interrupt active evaluation #3442

Open
mattsse opened this issue Oct 30, 2023 · 4 comments
Open

Cancel/Interrupt active evaluation #3442

mattsse opened this issue Oct 30, 2023 · 4 comments
Labels
API enhancement New feature or request

Comments

@mattsse
Copy link
Contributor

mattsse commented Oct 30, 2023

feature
I would like to be able to cancel an evaluation (or JsValue::call) after a certain timeout.
Perhaps this is already possible, but I wasn't able to find it.

boja for example has interrupts:

https://github.com/dop251/goja/blob/b396bb4c349df65109dea3df00fb60f6a044950d/runtime.go#L1474C9-L1482

my motivation for this is to have the option to terminate a (malicious) evaluation for example while(1) {}

Example code
Give a code example that should work after the implementation of this feature.

@mattsse mattsse added the enhancement New feature or request label Oct 30, 2023
@jedel1043
Copy link
Member

There isn't a hook to do this at the moment, but we have plans to support this. The simplest API I can think of is a way to set a flag for the Context from another thread that marks it as interrupted. Would that be enough to support your use case?

@mattsse
Copy link
Contributor Author

mattsse commented Oct 30, 2023

cool.

The simplest API I can think of is a way to set a flag for the Context from another thread that marks it as interrupted

yeah, this is exactly what I'm looking for, like Interrupt(Arc<AtomicBool>) or equivalent.

@jedel1043
Copy link
Member

yeah, this is exactly what I'm looking for, like Interrupt(Arc<AtomicBool>) or equivalent.

Alright, I'll put this on our backlog then.

@jedel1043 jedel1043 moved this to To do in Boa pre-v1 Oct 30, 2023
@jedel1043 jedel1043 added the API label Oct 30, 2023
@felipefdl
Copy link

It's difficult to use Boa for running untrusted third-party code without a way to interrupt eval.

I appreciate the simplicity and effectiveness of rquickjs's set_interrupt_handler method for handling this issue. It's a great solution for many use cases.

Here's an example showing how a timeout could be implemented using set_interrupt_handler:

let timestamp_now = std::time::SystemTime::now();

rt.set_interrupt_handler(Some(Box::new(move || {
  if let Ok(elapsed) = timestamp_now.elapsed() {
    if elapsed.as_millis() > 1000 {
      return true;
    }
  }

  false
})));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API enhancement New feature or request
Projects
Status: To do
Development

No branches or pull requests

3 participants