-
-
Notifications
You must be signed in to change notification settings - Fork 27.2k
fix: handle awaitTermination result and ensure proper ExecutorService shutdown #3244
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
Conversation
… shutdown - Added handling for the result of awaitTermination to avoid Sonar warning - Wrapped ExecutorService with try-finally for proper shutdown (java:S2095) - Prevents potential resource leak and aligns with best practices Fixes: iluwatar#2865 Note: ExecutorService is not AutoCloseable, so try-with-resources is not applicable. Used try-finally instead.
PR SummaryThis PR addresses a high severity issue (java:S2095) reported by SonarCloud by improving resource handling and ensuring proper ExecutorService shutdown in the Changes
autogenerated by presubmit.ai |
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.
✅ LGTM!
Review Summary
Commits Considered (2)
-
c217d2d: Merge branch 'iluwatar:master' into master
-
634e77a: fix: handle awaitTermination result and ensure proper ExecutorService shutdown
-
Added handling for the result of awaitTermination to avoid Sonar warning
-
Wrapped ExecutorService with try-finally for proper shutdown (java:S2095)
-
Prevents potential resource leak and aligns with best practices
Fixes: #2865
Note: ExecutorService is not AutoCloseable, so try-with-resources is not applicable. Used try-finally instead.
Files Processed (1)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
-
leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [80-82]
possible issue: "Unnecessary
shutdownNow()
call."
Hi @iluwatar, kindly reminding for review when you get a chance 🙂 |
addTasks(taskSet); | ||
boolean terminated = exec.awaitTermination(2, TimeUnit.SECONDS); | ||
if (!terminated) { | ||
System.out.println("Executor did not terminate in the given time."); |
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.
Use Lombok's @slf4j annotation for logging
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.
✅ LGTM!
Review Summary
Commits Considered (1)
Files Processed (2)
- leader-followers/pom.xml (1 hunk)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (2 hunks)
Actionable Comments (0)
Skipped Comments (1)
-
leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [26-36]
enhancement: "Improve logging in finally block."
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.
✅ LGTM!
Review Summary
Commits Considered (1)
- a758249: Merge branch 'iluwatar:master' into master
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)
- Defined logger explicitly with LoggerFactory.getLogger(...) - Ensured compatibility with Lombok's @slf4j annotation - Fixed compilation error caused by missing 'log' variable
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.
✅ LGTM!
Review Summary
Commits Considered (1)
Files Processed (2)
- leader-followers/pom.xml (1 hunk)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (2 hunks)
Actionable Comments (0)
Skipped Comments (1)
-
leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [26-36]
enhancement: "Improve exception handling and logging in finally block."
Hi @iluwatar, thank you for the feedback! ✅ I've addressed your review comment by defining the SLF4J logger explicitly instead of using Lombok's Let me know if there's anything else I should improve 🙌 |
leader-followers/pom.xml
Outdated
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.24</version> | ||
<scope>provided</scope> | ||
</dependency> |
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.
The dependency already exists in parent pom.xml, so no need to declare it here
*/ | ||
@Slf4j | ||
public class App { | ||
private static final Logger log = LoggerFactory.getLogger(App.class); |
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.
No need to declare the logger like this. After inserting the annotation you can just log like LOGGER.info("hello")
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.
✅ LGTM!
Review Summary
Commits Considered (1)
- 4ef609c: fix: add missing logger definition for SLF4J
Files Processed (1)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (2 hunks)
Actionable Comments (0)
Skipped Comments (0)
|
✅ Updated code based on review:
Let me know if there's anything else to revise. |
Hi @iluwatar! 🙌
All checks have passed ✅ and there are no conflicts with base branch. |
Looks good! Thank you for the contribution 🎉 @all-contributors please add @2897robo for code |
Thank you so much for reviewing and merging! 🙏 Much appreciated! 🚀 |
What does this PR do?
This pull request addresses a high severity issue (java:S2095) reported by SonarCloud in the
App.java
file of theleader-followers
module.The issue was:
What was the problem?
The
ExecutorService
created usingExecutors.newFixedThreadPool(...)
was not guaranteed to shut down properly, which could lead to:What does this PR change?
ExecutorService
usage inside atry-finally
blockawaitTermination(...)
to check whether the shutdown was completed within the timeoutshutdownNow()
in thefinally
blockWhy not try-with-resources?
ExecutorService
does not implementAutoCloseable
, so it cannot be used withtry-with-resources
.Hence, we use
try-finally
as the proper alternative here.References
✅ Fixes: #2865
🧹 Impact: Improves resource handling, stability, and SonarCloud quality gates