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

Add loopUntilNoMoreEvents() API to wait for arbitrary code. #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

achimnol
Copy link

  • Existing APIs rely on explicit callback argument or predicate.
    This API does not require such prior information, just like
    how the nodejs main loop continues or terminates.
  • To use this API to block for a specific region of code, you should
    take care of other existing callbacks using unref() and ref() methods
    to prevent them from making loopUntilNoEvents() to block "longer"
    than you want.

@abbr
Copy link
Owner

abbr commented Apr 3, 2016

Can you give a use case where the new api is needed? I don't see adding a predicate as inconvenient even when result of async call is not a concern but only the timing matters.
Also, I don't get how can the new api prevent blocking longer than necessary. I suspect the api will be more prone to deadlock. Can you elaborate your 2nd bullet point?

@achimnol
Copy link
Author

achimnol commented Apr 4, 2016

This is required to synchronously execute arbitrary user-input codes, using eval() or vm.Script objects, where I cannot determine which callback is the "last" executed one and augment the user code accordingly in a reliable way.

Of course, this is very "dangerous" for normal node users and I even won't recommend to use this except for very specific use cases. In my case, the node process does not manage any kind of multiplexing at all because there is another separate server daemon that multiplexes multiple clients, detects timeouts, and cleans up no-longer-used or crashed node processes. Each node process is volatile and used only to execute the user code synchronously before returning the results via network. Normally we can just spawn a node process on every user request and wait until it terminates, but we want to keep the context across multiple user code execution requests within a limited time span (e.g., reuse a variable defined by previous code execution requests). This led me to write a kind of "generic barrier" API in this PR. loopUntilNoEvents() does what the main loop of node does to wait until it terminates.

A caveat when using this API is that you need to disable all other pending callbacks to prevent indefinite blocking, socket listeners and timers for example, using unref() APIs that internally calls uv_unref(). For my case, this is perfectly okay and acceptable of course.

@achimnol
Copy link
Author

achimnol commented Apr 4, 2016

Renamed the new API to loopUntilNoMoreEvents, and rebased the history.

@achimnol achimnol changed the title Add loopUntilNoEvents() API to wait for aribtrary code. Add loopUntilNoMoreEvents() API to wait for aribtrary code. Apr 4, 2016
@achimnol achimnol changed the title Add loopUntilNoMoreEvents() API to wait for aribtrary code. Add loopUntilNoMoreEvents() API to wait for arbitrary code. Apr 4, 2016
 * Existing APIs rely on explicit callback argument or predicate.
   This API does not require such prior information, just like
   how the node.js main loop continues or terminates.

 * To use this API to block for a specific region of code, you should
   take care of other existing callbacks using unref() and ref() methods
   to prevent them from making loopUntilNoMoreEvents() to block "longer"
   than you want.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants