-
Notifications
You must be signed in to change notification settings - Fork 247
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 SmartLifecycle#stop #360
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -40,7 +40,7 @@ public boolean isAutoStartup() { | |||||||
@Override | ||||||||
public void stop(Runnable runnable) { | ||||||||
runnable.run(); | ||||||||
this.isRunning = false; | ||||||||
stop(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: SmartLifecycle contract violation in stop(Runnable) implementation The current implementation violates the SmartLifecycle contract by executing the callback before stopping the component. This can lead to race conditions and break Spring's shutdown coordination mechanism. According to Spring's SmartLifecycle contract:
Here's the correct implementation: @Override
public void stop(Runnable callback) {
- callback.run();
- stop();
+ stop();
+ callback.run();
} This ensures:
📝 Committable suggestion
Suggested change
|
||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -73,6 +73,6 @@ public int getPhase() { | |||||||||||
@Override | ||||||||||||
public void stop(Runnable callback) { | ||||||||||||
callback.run(); | ||||||||||||
this.running.set(false); | ||||||||||||
stop(); | ||||||||||||
} | ||||||||||||
Comment on lines
+76
to
77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the order of operations in stop(Runnable). The current implementation violates Spring's SmartLifecycle contract. According to the contract, the stop operation should be performed before executing the callback. This ensures that the component is fully stopped before notifying the container. Apply this diff to fix the order: @Override
public void stop(Runnable callback) {
- callback.run();
stop();
+ callback.run();
} 📝 Committable suggestion
Suggested change
🛠️ Refactor suggestion Add error handling and logging for the stop operation. The stop operation lacks error handling and logging. If either the stop() call or callback execution fails, it could leave the system in an inconsistent state. Consider implementing with proper error handling and logging: @Override
public void stop(Runnable callback) {
+ LOGGER.info("Stopping MetaServer...");
+ try {
stop();
+ LOGGER.info("MetaServer stopped successfully");
callback.run();
+ } catch (Exception ex) {
+ LOGGER.error("Error during MetaServer shutdown", ex);
+ throw ex;
+ }
}
|
||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -74,6 +74,6 @@ public int getPhase() { | |||||||||||||
@Override | ||||||||||||||
public void stop(Runnable callback) { | ||||||||||||||
callback.run(); | ||||||||||||||
this.running.set(false); | ||||||||||||||
stop(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Incorrect order of operations in stop(Runnable callback) The current implementation executes the callback before stopping the server, which violates the Spring Framework's SmartLifecycle contract. The callback should only be executed after the shutdown is complete to properly signal completion to the container. Apply this diff to fix the order: @Override
public void stop(Runnable callback) {
- callback.run();
stop();
+ callback.run();
} This ensures that:
📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the order of operations in stop(Runnable). The current implementation executes the callback before stopping the server, which violates the SmartLifecycle contract. The callback should be invoked after the shutdown is complete to properly signal completion to Spring's lifecycle management. Apply this diff to fix the order: @Override
public void stop(Runnable callback) {
- callback.run();
stop();
+ callback.run();
} This ensures that:
📝 Committable suggestion
Suggested change
|
||||||||||||||
} | ||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect implementation of SmartLifecycle#stop callback ordering
The current implementation executes the callback before completing the stop procedure, which violates Spring's SmartLifecycle contract. The callback should only be executed after the bean has fully stopped to ensure proper shutdown sequencing of dependent beans.
Apply this fix to ensure correct shutdown order:
Additionally, consider enhancing the stop() method to properly cleanup resources: