How to cancel a promise execution

212 views
Skip to first unread message

bambam

unread,
Feb 8, 2021, 5:34:08 PM2/8/21
to v8-dev
Hello guys... I'm embedding the V8 into my own application to run some untrusted code concurrently. Every untrusted code is executed inside a promise and when any of these codes blocks the system (by doing a while(true){} for example) I need to stop it. So, I've been trying to find a way of canceling JS promises but I haven't found anything useful. Does anyone here know where I could find some related information?

Yang Guo

unread,
Feb 8, 2021, 5:42:52 PM2/8/21
to v8-...@googlegroups.com
What you are looking for is not cancelling a promise, but stopping code execution. JS is not concurrent, so rejecting or cancelling (which does not exist for JS promises) wouldn't help.

Take a look at v8::Isolate::TerminateExecution

Cheers,

Yang

On Mon, 8 Feb 2021, 23:34 bambam, <ehtre...@gmail.com> wrote:
Hello guys... I'm embedding the V8 into my own application to run some untrusted code concurrently. Every untrusted code is executed inside a promise and when any of these codes blocks the system (by doing a while(true){} for example) I need to stop it. So, I've been trying to find a way of canceling JS promises but I haven't found anything useful. Does anyone here know where I could find some related information?

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/6a499919-362f-48f9-8651-f8644f584255n%40googlegroups.com.

bambam

unread,
Feb 8, 2021, 6:04:47 PM2/8/21
to v8-dev
Thank you for your answer Yang,

Let me give you a more detailed explanation: what I'm doing here is a server that runs untrusted code inside a v8::Isolate to process incoming HTTP requests and returns HTTP responses. Every time a request arrives, the same JS async function is called to process it, generating the corresponding response. What happens is that, for performance reasons, I need to process multiple requests in the same v8::Isolate, this is why I'm using an async function, this way every time a new request arrives I start processing it regardless of the previous requests' processing state. However, if at some point any of the running async functions block the V8, all the other async functions stop, because the CPU gets stuck. If I simply call v8::Isolate::TerminateExecution the CPU is released and all the system works again, however, all the async functions that weren't causing the system block are destroyed because the entire v8::Isolate is destroyed. What I need to do is to "destroy" only the async function that caused the system blocking and this is why I thought about promise "stopping" or "aborting" or "canceling" or anything like this.

cheers,

Bambam

Yang Guo

unread,
Feb 9, 2021, 1:01:28 AM2/9/21
to v8-...@googlegroups.com
You can recover from termination. I'm not sure anymore how microtasks deal with termination. Maybe they indeed get all rejected. One workaround is to wrap every individual response handler execution in C++ callback which calls into JS. When you terminate, you'd terminate the JS execution and drop into the C++ callback. At that point you can cancel termination and reject this one particular promise.

Yang

bambam

unread,
Feb 9, 2021, 9:48:52 AM2/9/21
to v8-dev
This sounds good to me, I haven't tested how the microtaskqueue behaves when we call the TerminateExecution yet. I've been holding the HTTP connections at the C++ side while their requests are processed by the JS side, so it seems to be possible to recover this way. If I got it right you expect all promises to be rejected during the terminating process, right?

When you mentioned the "C++ response callbacks" did you mean my regular binds through which I send the responses or did you think about something special like a PromiseHook where I could identify the termination process?

Thank you for your contribution,

Cheers
Reply all
Reply to author
Forward
0 new messages