Fixed potential bugs due to incomplete signal handling #81699
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.
Problem
1. potential bug: incomplete signal handling:
The current code calls
process.exit()
directly upon receiving an exit signal such asSIGTERM
orSIGINT
.Calling
process.exit()
terminates the Node.js process immediately.This does not guarantee completion of asynchronous processing or execution of the finally block. As a result,
disableMemoryDebuggingMode()
may not be called whenexternalDebugMemoryUsage
is enabled and resources may not be released.2. code duplication: redundant cleanup process:
cleanup process (call to
disableMemoryDebuggingMode()
) exists in both.catch()
and.finally()
blocks.Since the
.finally
block is always executed regardless of the success or failure of the Promise, I thought the cleanup process in.catch()
was redundant.Proposal
1. Combine cleanup logic into a function:
encapsulate the cleanup process as a
cleanup
function to eliminate code duplication.2. Improve the signal handler:
Modify it so that when it receives an exit signal,
it first calls the cleanup function before terminating the process.
This will ensure that
cleanup
is also performed when a signal is received.3. Simplify the Promise chain:
.finally(cleanup)
to remove redundant calls in.catch()
.This change ensures that no matter under what circumstances a process exits,
if the memory debugging feature is enabled, it will always disable it and ensure that it exits clean.
It can then play an important role in preventing resource leaks and stabilizing the system.