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

Fix crash in bsc project when thread has error #235

Merged
merged 3 commits into from
Mar 4, 2025

Conversation

TwitchBronBron
Copy link
Member

Gracefully recover whenever the bsc project worker thread crashes (instead of bringing down the entire process...).

The fix was to observer the .on('error' event from the worker thread. But then also, when we reject a deferred, if nobody is listening then that will also crash node so we need to immediately observer the rejection and ignore the error.

public async activate(options: Parameters<ProgramBuilder['run']>[0]) {
const timeEnd = this.logger.timeStart('log', 'activate');

// start a new worker thread or get an unused existing thread
this.worker = bscProjectWorkerPool.getWorker();

//!!!IMPORTANT!!! this observer must be registered in order to prevent the worker thread from crashing the main thread
this.worker.on('error', (error) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the most important part of the fix. by not observing this, node sees an unhandled exception and crashes.

this.logger.error('Worker encountered an error', error);
this.errorDeferred.reject(error);
//!!!IMPORTANT!!! this is required to prevent node from freaking out about an uncaught promise
this.errorDeferred.promise.catch(e => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also important. It's new code that helps track whether our worker has crashed. but by not observing the rejection, that would also bring down the node process. So we observe it, do nothing, and assume someone else will pay attention on the outside.

@TwitchBronBron TwitchBronBron merged commit 1d310d4 into master Mar 4, 2025
6 checks passed
@TwitchBronBron TwitchBronBron deleted the fix-bsc-project-crash branch March 4, 2025 20:51
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.

1 participant