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

Optimize System.exit Usage #6125

Open
halibobo1205 opened this issue Dec 17, 2024 · 8 comments
Open

Optimize System.exit Usage #6125

halibobo1205 opened this issue Dec 17, 2024 · 8 comments

Comments

@halibobo1205
Copy link
Contributor

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:

  1. Poor maintainability: Scattered System.exit calls make it difficult to trace and maintain program workflows.
  2. Testing Environment Impact: Code containing 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.
  3. Resource Release Risks: System.exit does not trigger the execution of 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 as TronError, 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.

@xxo1shine
Copy link
Contributor

@halibobo1205  Can replacing direct System.exit calls with specific exceptions solve the above problem? How to exit the process specifically?

@S1ep
Copy link

S1ep commented Dec 18, 2024

What is the meaning of CI of the 2nd technical debt in Background?

@CarlChaoCarl
Copy link
Contributor

@halibobo1205

Directly calling System.exit prevents the Spring application from shutting down gracefully. Looking forward to this optimization.

@halibobo1205
Copy link
Contributor Author

@halibobo1205  Can replacing direct System.exit calls with specific exceptions solve the above problem? How to exit the process specifically?

@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.

@halibobo1205
Copy link
Contributor Author

What is the meaning of CI of the 2nd technical debt in Background?

@S1ep
Continuous integration (CI) is a process in which developers continually commit code in small increments—sometimes multiple times a day—which is then automatically built and tested before it is merged with the shared repository.

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.

@Murphytron
Copy link

This issue has been added to the tronprotocol/pm#107, welcome to share the latest progress @halibobo1205, and discuss together with @xxo1shine @S1ep @CarlChaoCarl

@angrynurd
Copy link

2 simple questions:
1.How can we ensure that critical resources such as network connections and file handles are properly and promptly released after replacing System.exit calls in the Java-Tron project?
2.How can we design and implement a classification and priority handling system for exceptions in the new error management framework to ensure that the system shuts down in an orderly and reasonable manner under different error scenarios?

@halibobo1205
Copy link
Contributor Author

halibobo1205 commented Jan 7, 2025

@angrynurd

  1. Resources are properly and promptly released by finally code blocks or triggered by ShutdownHook
  2. Set global exception handler by Thread.setDefaultUncaughtExceptionHandler, use an independent thread for System.exit, shuts down in an orderly is managed by Spring.
     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();
 }

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

No branches or pull requests

6 participants