Programmatically handling unresponsive scripts / endless loops

Viewed 35

I am running untrusted scripts in a WebAssembly sandbox (using a non-JS interpreter written in WASM). It is possible for these untrusted scripts to run an endless loop that would make the main application unresponsive. Is it possible to set a "timer interrupt" on a function that is expected to terminate within N milliseconds?

Pseudocode example:

const timeoutMS = 1000;
const untrustedFn = () => {};

// Theoretical function. I don't know if modern JS environments
// support this.
// This will throw an exception if `untrustedFn` does not finish in 1000 ms.
finishExecutionWithinMs(timeoutMS, untrustedFn);
  • setTimeout would not be possible here since the endless loop would block the event loop from servicing the callback.
  • Running the script in a worker is possible, but I am looking for a simpler alternative.
  • Many browsers support automatically terminating unresponsive scripts, but I need a higher level of granularity (need to be able to configure the timeout interval and handle failure in a catch)

Do modern browsers support functionality like this?

0 Answers
Related