-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Optimize System.exit Usage #6125
Comments
@halibobo1205 Can |
What is the meaning of CI of the 2nd technical debt in Background? |
Directly calling System.exit prevents the Spring application from shutting down gracefully. Looking forward to this optimization. |
@xxo1shine Handle these exceptions at appropriate higher-level calling points using a unified exit logic, maintaining the same exit logic as before, except that the exit position changes. |
@S1ep The main goal of CI is to provide quick feedback so that if a defect is introduced into the code base, it can be identified and corrected as soon as possible. CI helps reduce the time and effort required for integration problems and allows developers to enhance software quality continuously. |
This issue has been added to the tronprotocol/pm#107, welcome to share the latest progress @halibobo1205, and discuss together with @xxo1shine @S1ep @CarlChaoCarl |
2 simple questions: |
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
findTronError(e).ifPresent(this::logAndExit);
});
private void logAndExit(TronError exit) {
final int code = exit.getCode();
logger.error("Shutting down with code: {}.", code, exit);
Thread exitThread = exitThreadFactory.newThread(() -> System.exit(code));
exitThread.start();
} |
Background
There are several cases where System.exit is directly invoked to terminate the system after encountering specific exceptions. While this approach is straightforward, it can lead to various technical debts:
System.exit
calls make it difficult to trace and maintain program workflows.System.exit
is challenging to unit test. Such calls cause Gradle unit tests to terminate unexpectedly, affecting code coverage statistics and reducing the reliability and stability of CI.finally
code blocks, potentially leading to improperly closed resources such as database connections and file handles.Rationale
Replace direct
System.exit
calls with specific exceptions, such asTronError
, and handle these exceptions at appropriate higher-level calling points using a unified exit logic. This approach preserves the default exit behavior while addressing the issues above to the greatest extent possible.The text was updated successfully, but these errors were encountered: