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

Exit the main process after all closure steps are completed #61

Closed
4 tasks done
nunoguedelha opened this issue Nov 3, 2021 · 2 comments
Closed
4 tasks done

Exit the main process after all closure steps are completed #61

nunoguedelha opened this issue Nov 3, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@nunoguedelha
Copy link
Collaborator

nunoguedelha commented Nov 3, 2021

The Two Main Issues Addressed Here

  • Some modules included in the main script may add listeners to the closure events SIGxxxx, which call process.exit() or any other procedure that we don't want being executed before the terminationHandler of the main application.
  • The process should exit automatically if there is no pending queued events in the Node Event Loop. But for some reason, yet to be analyzed, there are pending events preventing the process to exit on its own. A possible workaround is to call process.exit().

References

Event Loop
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout
https://nodejs.dev/learn/the-nodejs-event-loop
https://nodejs.dev/learn/understanding-process-nexttick
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

Avoiding a Premature Exit

  • Before even adding the listener to the a given closure signal (SIGINT, etc): (a) backup the existing listeners registered for that same signal, (b) remove them, (c) add our own listener terminationHandler, (d) restore the backuped listeners after completing terminationHandler. => Exit the main process after all closure steps are completed #62

Workaround

Further Analysis

@nunoguedelha nunoguedelha self-assigned this Nov 3, 2021
@nunoguedelha nunoguedelha added the bug Something isn't working label Nov 3, 2021
nunoguedelha added a commit that referenced this issue Nov 3, 2021
…losure last step).

- Refer to #61. For some reason, there are pending events in the Node
  Event Loop preventing the process to exit on its own.
@nunoguedelha nunoguedelha changed the title exit the process (should be automatic if there no pending scheduled events in the Node.js Event Loop). Exit the main process after all closure steps are completed Nov 3, 2021
nunoguedelha added a commit that referenced this issue Nov 3, 2021
- Close ports explicitely.
- Log pending handlers and requests in the Event Loop.
  Reference:
  nodejs/node#1128
  https://github.com/thlorenz/active-handles
@nunoguedelha
Copy link
Collaborator Author

nunoguedelha commented Nov 3, 2021

Closing all the ports manually, and checking the pending handles and requests in the Event Loop ⌛

=> Moved to #76

@nunoguedelha
Copy link
Collaborator Author

nunoguedelha commented Nov 4, 2021

Avoiding a Premature Exit

Before even adding the listener to the a given closure signal (SIGINT, etc), we execute the following a the end of the main script:

After completing terminationHandler, so, after Subset C...

Promise.all([subsetCpromise,subsetApromise.closeServers]).then(
function(values) {
values.forEach((v) => console.log(v));
process.exit();
}
).catch(console.error);

...do:

  • restore the backed-up listeners (process.on: this restores the 'once' wrapper. For Node v14.17.0 use process.rawListeners(eventName) instead of process.listeners(eventName))
  • Add a listener which includes the workaround mentioned in the issue description. The listener is the last after the backuped listeners (process.on(signal,() => {process.exit()}))
  • Emit again the captured signal signal.

nunoguedelha added a commit that referenced this issue Mar 23, 2022
… termination after closing all the sockets

- Call 'process.exit()' for forcing the process termination.
- Refer to #61. For some reason, there are pending events in the Node
  Event Loop preventing the process to exit on its own.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant