Bug 574270 - False Positive Potential Resource Leak Warnings With Inherited AutoCloseable Classes
Summary: False Positive Potential Resource Leak Warnings With Inherited AutoCloseable ...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.21   Edit
Hardware: PC Windows 7
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: api, core
Depends on:
Blocks:
 
Reported: 2021-06-17 08:50 EDT by geoff bland CLA
Modified: 2023-01-04 09:15 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description geoff bland CLA 2021-06-17 08:50:05 EDT
This worked fine in Eclipse 2019.06 but has been broken in each version since. This affects us as we have many "cached" resources that are giving false positives potentially masking real issues if we need to mark all the false positives to ignore.

Example code. We have a wrapper class that encapsulates a resource, here we just keep it for the life of application and provide a reference to any other class that needs it. Note we provide the reference in two ways. 

package test_utilities;

import javax.sql.DataSource;

public class ExampleResourceWrapper {

	private static ExampleResource internalDataSourceSingleton;

	public ExampleResourceWrapper() {
		initialise();
	}

	private static void initialise() {
		if (internalDataSourceSingleton == null) {
			// Keeps the database open until we shutdown
		  internalDataSourceSingleton = new ExampleResource();
		}
	}
	
	public static DataSource getDataSource1() {
		return internalDataSourceSingleton;
	}
	
	public static ExampleResource getDataSource2() {
		return internalDataSourceSingleton;
	}
}

The ExampleResource class just subclasses the AutoCloseable class to show the issue.

package test_utilities;

import org.apache.commons.dbcp2.BasicDataSource;

public class ExampleResource extends BasicDataSource {
	// Just a wrapper
}

Here's some code using the cached resource and showing the issue:

package test_utilities;

import javax.sql.DataSource;

public class ExampleResourceLeak {

	public static void main(String[] args) {

		// As expected no warning here 
		DataSource ds1 = ExampleResourceWrapper.getDataSource1();
		
		// False positive "potential resource leak" warning here
		DataSource ds2 = ExampleResourceWrapper.getDataSource2();

		// Code just to remove the not used warning
		ds1.getClass();
		ds2.getClass();
	}

}


So why does ExampleResourceWrapper.getDataSource1() work as expected and not show an error but ExampleResourceWrapper.getDataSource2() shows the False positive "potential resource leak" warning.
Comment 1 Sravan Kumar Lakkimsetti CLA 2021-06-22 01:59:11 EDT
May be duplicate of bug 565205
Comment 2 geoff bland CLA 2021-11-04 10:56:14 EDT
(In reply to Sravan Kumar Lakkimsetti from comment #1)
> May be duplicate of bug 565205

I don't think it is a duplicate of that bug. Bug 565205 is caused by the resource actually not being freed. For this issue the resource is freed, where the warning is raised we are not "opening/creating" the resource (rather just getting a reference to it) so should not be expected to close the resource - yet we get a warning the the resource is not closed.
Comment 3 geoff bland CLA 2021-12-16 06:12:43 EST
Any progress on this? This is a real problem for us - we develop a highly distributed system with many ports that deals with many files, sockets, database etc - it is important to us that we get warning of potential resource leaks. Having so many false positive warnings prevent us from upgrading our Eclipse versions and we are now a couple of years behind and would prefer to use the latest Eclipse.
Comment 4 geoff bland CLA 2022-06-28 09:56:55 EDT
Chasing this again - it would be great if someone could take a look at this as we would like to upgrade the version of Eclipse we use yet can't if we get all these false positives due to the risk of these masking an real issue.
Comment 5 geoff bland CLA 2023-01-04 07:32:58 EST
Chasing again - is this not a problem for many users? Surely it makes the "Potential Resource Leak" check useless in Eclipse?
Comment 6 geoff bland CLA 2023-01-04 09:15:18 EST
The issue still exists in Eclipse 2022-12.