Skip to content

Any recommended way for graceful shutdown of uncaughtException #26

Closed

Description

@davisford

With kue, the recommendation was to do something like this:

var dying = false;

function handleException(err) {
  console.error('uncaught exception or sigterm', err);
  if (!dying) {
     queue.shutdown(function (err) {
       if (err) { console.error('Kue did not shutdown gracefully', err); }
       process.exit(1);
     });
    dying = true;  
  }
}

process.on('SIGTERM', handleException);
process.on('uncaughtException', handleException);

I suppose kue had some things to do to gracefully clean up and close the redis connection. I didn't see any mention of this in the docs, etc. so just curious if it was necessary at all with bee-queue?

Activity

  1. LewisJEllis commented on Dec 8, 2015

    @LewisJEllis
    Member

    Hm, this is definitely something I haven't thought of/documented. I haven't looked at exactly what queue.shutdown does for kue, but queue.close will gracefully close the Redis connection, so I'd start with that for now. I'll have to think about whether anything else should be done for a graceful shutdown; there could, for example, be cases with jobs where .save() has been called, but they haven't made it into Redis yet, and we might like to wait for that to happen (although maybe the redis driver does that?).

  2. shekyan commented on Feb 23, 2016

    @shekyan

    See, if you thought about this case (incomplete jobs), we would definitely use this instead of bull ;)

  3. skeggse commented on Jul 20, 2017

    @skeggse
    Member

    The recommended graceful shutdown behavior as of #64 is:

    process.on('uncaughtException', () => {
      // Queue#close is idempotent - no need to guard against duplicate calls.
      queue.close()
        .catch((err) => console.error('bee-queue failed to shut down gracefully', err))
        .then(() => process.exit(1));
    });
  4. added this to the 1.0.0 milestone on Jul 20, 2017
  5. self-assigned this
    on Aug 9, 2017
  6. skeggse commented on Aug 9, 2017

    @skeggse
    Member

    Hm we should probably add that snippet to the documentation.

  7. reopened this on Aug 9, 2017
  8. modified the milestones: 1.1.0, 1.0.0 on Aug 9, 2017
  9. tothandras commented on Feb 23, 2018

    @tothandras

    According to the Node.js documentation, the event handler has to be synchronous.

    The correct use of 'uncaughtException' is to perform synchronous cleanup of allocated resource.

    Does this mean that the async function must be wrapped with node-sync or a similar library?

  10. LewisJEllis commented on Feb 23, 2018

    @LewisJEllis
    Member

    The documentation does not normatively state that the event handler must be synchronous. It makes a recommendation that under "correct" usage it should be synchronous, and warns against one type of "incorrect" usage in the form of resuming normal operation.

    I do find the wording's suggestion that synchronous cleanup is singularly correct, and therefore that it would be incorrect to even attempt to perform any asynchronous operations, to be a bit over-prescriptive and to gloss over an important subtlety, but in the strictest sense of program correctness the wording is not wrong because we do lose some guarantees along the way. The truly important takeaway is that normal operation must not be resumed. As long as the uncaughtException handler causes the process to move into a clean-up-and-shut-down phase and we can be sure that the process will not resume normal operation or otherwise continue to run indefinitely, we're otherwise free to do what we like as long as we understand what we're getting ourselves into.

    I have significant experience implementing and testing uncaughtException handlers that perform asynchronous operations during the clean-up-and-shut-down phase of the process's lifecycle, and I can assure you that there is no hard requirement that the handler be synchronous. I can also assure you that it is reasonable to perform asynchronous cleanup operations (e.g. sending out any final logs, allowing in-flight requests or queries to finish, closing connections, etc) as long as 1) it is on a best-effort basis where we acknowledge that there are no guarantees that any asynchronous operations performed during this phase finish or work correctly, and 2) the (relatively imminent) shutdown of the process is still guaranteed.

    For further reference, I gave a meetup/conference talk "Robust Error Handling in Node.js" last year that touched quite heavily on the clean-up-and-shutdown phase of the process's lifecycle and what uncaughtException handlers should and should not be used for.

  11. LewisJEllis commented on Feb 23, 2018

    @LewisJEllis
    Member

    That aside, I would recommend against using any sort of "synchronous" hackaround library for almost any purpose; most are artifacts from the days of callbacks when everyone was squirming to get away from them. If you want your code to look synchronous, Node has native async/await.

  12. tothandras commented on Feb 24, 2018

    @tothandras

    @LewisJEllis Thank you very much for the detailed explanation! I was always confused about the wording of this part of the Node documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions