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

Fix resource leaks #591

Merged
merged 16 commits into from
May 18, 2018
Merged

Fix resource leaks #591

merged 16 commits into from
May 18, 2018

Conversation

KengoTODA
Copy link
Member

Current implementation does not close SourceFinder even after analysis.
This PR also applies try-with-resources to handle zip files.

@iloveeclipse I'm not sure about the difference between FindBugs2#dispose() and FindBugs2#clearCache(), could you review? At least, can we destruct project instance by clearCaches() or not?

@KengoTODA KengoTODA changed the title Fix resource leaks WIP: Fix resource leaks Mar 21, 2018
@KengoTODA
Copy link
Member Author

This PR still needs to add a fix for PluginLoader, see roberthubert@7c47138

On Windows, cached connection can keep file handler then it causes
resource leak. We have no motivation to use cached connection so
we disable cache by default.

This commit is cherry-pick from roberthubert@7c47138
KengoTODA referenced this pull request in roberthubert/spotbugs Mar 30, 2018
public static Project readXML(File f) throws IOException, SAXException {
InputStream in = new BufferedInputStream(new FileInputStream(f));
public static Project readXML(File f) throws IOException, SAXException {
@SuppressWarnings("resource") // will be closed by caller
Project project = new Project();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKER Use try-with-resources or close this "Project" in a "finally" clause. rule

@KengoTODA KengoTODA added this to the SpotBugs 3.1.3 milestone Apr 2, 2018
}

@CheckReturnValue
private Project createProject(Path[] files) {
final Project project = new Project();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKER Use try-with-resources or close this "Project" in a "finally" clause. rule

@victorwss
Copy link

Other issues targeted for 3.1.3, like #593/#602 were already fixed. If I understood it right, it seems that 3.1.3 was not yet released because it should contain the fix for this issue.

However, #593/#602 is a blocker for me, so I'm really needing a new release ASAP. Then, I would like to know if this issue is in a state to be fixed and merged very soon or not. If not, I suggest to defer it to 3.1.4 and release 3.1.3 with what we already have.

CHANGELOG.md Outdated
@@ -15,6 +15,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
* Handle annotation on `package-info.class` properly ([#592](https://github.com/spotbugs/spotbugs/issues/592))
* Update asm to 6.1.1 to support Java 10
* Update Apache BCEL to 6.2 to support Java 9 package & module reference
* Close source file after analysis ([#591](https://github.com/spotbugs/spotbugs/issues/591))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move this to Unreleased part

@KengoTODA KengoTODA changed the title WIP: Fix resource leaks Fix resource leaks Apr 30, 2018
@KengoTODA
Copy link
Member Author

@iloveeclipse please check and review:

@iloveeclipse I'm not sure about the difference between FindBugs2#dispose() and FindBugs2#clearCache(), could you review? At least, can we destruct project instance by clearCaches() or not?

Note that we can ignore these two SonarQube warnings; method caller should be responsible to close resources, not these methods themselves.

@iloveeclipse
Copy link
Member

iloveeclipse commented Apr 30, 2018

If I remember right, clearCaches() was there to allow Eclipse plugin to free memory after analyis while the rest of FB2 would still be reusable. dispose() is the end of life. I quickly tried the change, it seem no to break the plugin, but I have no time for a deep review. I would probably don't close the project on clearCaches() call, it seem to close the source finder which seem to close all source repositories, and they will be not restored again.

@KengoTODA
Copy link
Member Author

OK then I will stop closing project in clearCaches(), and make sure that FindBugs2.main(...) invokes not only clearCaches() but also dispose().

@@ -137,7 +138,7 @@ void storeAdjustment(String key, String value) {
if (u == null) {
return;
}
BufferedReader in = UTF8.bufferedReader(u.openStream());
BufferedReader in = UTF8.bufferedReader(IO.openNonCachedStream(u));
try {
while (true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Reduce the total number of break and continue statements in this loop to use at most one. rule

import java.net.URL;
import java.net.URLConnection;

import javax.annotation.CheckForNull;
import javax.annotation.WillClose;
import javax.annotation.WillNotClose;

import edu.umd.cs.findbugs.annotations.CheckReturnValue;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.util.Util;

public class IO {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Add a private constructor to hide the implicit public one. rule

@@ -267,8 +267,8 @@ private static void finishLazyInitialization() {
System.err.println(msg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this use of System.out or System.err by a logger. rule

@@ -1623,8 +1614,6 @@ public static Summary validate(File file) throws IllegalArgumentException {
throw new IllegalArgumentException(e);
} catch (IOException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Combine this catch with the one at line 1613, which has the same body. rule

if (!bugReporter.getQueuedErrors().isEmpty()) {
AssertionError assertionError = new AssertionError(
"Analysis failed with exception. Check stderr for detail.");
bugReporter.getQueuedErrors().stream().map(error -> error.getCause())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Replace this lambda with a method reference. rule

final Project project = new Project();
project.setProjectName(getClass().getSimpleName());
engine.setProject(project);

if (PLUGIN_JAR != null) {
try {
String pluginId = Plugin.addCustomPlugin(PLUGIN_JAR.toURI()).getPluginId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR A "NullPointerException" could be thrown; "addCustomPlugin()" can return null. rule

@@ -648,17 +648,14 @@ boolean processZipEntries(File f, ZipElementHandler handler) {
System.out.println("empty zip file: '" + f + "'");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Replace this use of System.out or System.err by a logger. rule

@spotbugs-bot
Copy link
Member

SonarQube analysis reported 254 issues

  • BLOCKER 3 blocker
  • CRITICAL 39 critical
  • MAJOR 133 major
  • MINOR 69 minor
  • INFO 10 info

Watch the comments in this conversation to review them.

Top 10 extra issues

Note: The following issues were found on lines that were not modified in the pull request. Because these issues can't be reported as line comments, they are summarized here:

  1. BLOCKER FindBugs2.java#L123: Rename field "progress" to prevent any misunderstanding/clash with field "PROGRESS" defined on line 92. rule
  2. CRITICAL BugRanker.java#L213: Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed. rule
  3. CRITICAL BugRanker.java#L301: Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed. rule
  4. CRITICAL FindBugs2.java#L189: Refactor this method to reduce its Cognitive Complexity from 26 to the 15 allowed. rule
  5. CRITICAL FindBugs2.java#L476: Refactor this method to reduce its Cognitive Complexity from 31 to the 15 allowed. rule
  6. CRITICAL FindBugs2.java#L486: Define a constant instead of duplicating this literal "Unable to read filter: " 3 times. rule
  7. CRITICAL FindBugs2.java#L704: Refactor this method to reduce its Cognitive Complexity from 42 to the 15 allowed. rule
  8. CRITICAL FindBugs2.java#L948: Refactor this method to reduce its Cognitive Complexity from 96 to the 15 allowed. rule
  9. CRITICAL PluginLoader.java#L231: Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed. rule
  10. CRITICAL PluginLoader.java#L303: Change this "try" to a try-with-resources. rule

@KengoTODA
Copy link
Member Author

@iloveeclipse PR has succeeded. Please check. I think all the warning by spotbugs can be ignored, since they're for existing code, not the newly added one.

Please use 'squash and merge' to merge this PR, it seems that recently GitHub isn't so wise to handle conflict.

Copy link
Member

@iloveeclipse iloveeclipse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@KengoTODA KengoTODA merged commit 5f88794 into release-3.1 May 18, 2018
@KengoTODA KengoTODA deleted the fix-resource-leaks branch May 18, 2018 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants