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

Emit event when code in vm is fully executed #31240

Closed
mehmetb opened this issue Jan 7, 2020 · 3 comments
Closed

Emit event when code in vm is fully executed #31240

mehmetb opened this issue Jan 7, 2020 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. vm Issues and PRs related to the vm subsystem. wontfix Issues that will not be fixed.

Comments

@mehmetb
Copy link

mehmetb commented Jan 7, 2020

Is your feature request related to a problem? Please describe.
There is no way to know if the script running in vm is fully executed or not. Of course, all synchronous tasks are immediately executed when runInContext is called. But since all contexts share the event loop, we cannot tell if there are any more callbacks (of the vm code) in the event loop. (e.g. a setTimeout callback)

Describe the solution you'd like
vm.Script should use a separate event loop and emit an event when there are no more callbacks left.

Describe alternatives you've considered
As bnoordhuis stated, we can add bookkeeping to every call into the VM.

@bnoordhuis bnoordhuis added feature request Issues that request new features to be added to Node.js. vm Issues and PRs related to the vm subsystem. labels Jan 7, 2020
@bnoordhuis
Copy link
Member

There is no way to know if the script running in vm is fully executed or not.

There is really no way to know short of solving the Halting Problem.

Example: a promise handler can always enqueue a new promise, which in turn can enqueue a new promise, etc.

It might be possible to implement some kind of "kill switch" that forcibly terminates the script and all outstanding microtasks/handles/requests/etc., but that's about it. But then you might as well use workers.

vm.Script should use a separate event loop

That's probably impossible because you can't nest event loops for technical reasons.

Example: on some platforms where node uses kqueue for its event loop (macos, the BSDs), adding a kqueue fd to another kqueue fd doesn't work because the nested fd doesn't generate events.

It also doesn't really solve the issue of microtasks/promises.

@mehmetb
Copy link
Author

mehmetb commented Jan 7, 2020

It also doesn't really solve the issue of microtasks/promises.

I was afraid you'd say that :/

But then you might as well use workers.

Yes, this will work. Sharing code between worker threads is harder but I now can tell that changing how VM works is harder.

Thank you for the thorough explanation @bnoordhuis!

@bnoordhuis
Copy link
Member

I'll go ahead and close this out then. Thanks anyway for opening the issue!

@bnoordhuis bnoordhuis added the wontfix Issues that will not be fixed. label Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. vm Issues and PRs related to the vm subsystem. wontfix Issues that will not be fixed.
Projects
None yet
Development

No branches or pull requests

2 participants