Catch to prevent push-receiver crash on disconnect #1540
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch addresses an unhandled promise rejection that occurs in push-receiver 4.3.0. This version of push-receiver introduced a new whenReady promise that consumers can use to know when the initial connection is ready. However, it seems to have a bug when the socket connection fails as the code rejects the promise but has no catch for this rejection, thus creating an unhandled promise which leads to a process crash on modern NodeJS >15 which exit on unhandled promises by default.
It's possible to attach a catch handler outside of the library during first use, but the issue is that a new promise is created on every connection attempt, so, while that will catch the initial failure, subsequent retries are new promises without a catch.
This patch takes a different approach, instead of attaching the catch handler during creating, it instead listens for "ON_DISCONNECT" event, which happens just before the promise rejection, and immediately attaches a catch handler to the promise to prevent the unhandled rejection error. It's a big of a hack, but seems to work reliably in testing until we can get upstream to fix the behavior (issue is already opened).