Bug 549823 - file handle resource leak when using the ASTParser
Summary: file handle resource leak when using the ASTParser
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.12   Edit
Hardware: PC Mac OS X
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-06 13:29 EDT by Martin Lippert CLA
Modified: 2023-04-16 09:16 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Lippert CLA 2019-08-06 13:29:55 EDT
I am using:

org.eclipse.jdt.core.dom.ASTParser.createASTs(String[] sourceFilePaths, String[] encodings, String[] bindingKeys, FileASTRequestor requestor, IProgressMonitor monitor)

to create the ASTs for a bunch of Java source files, passing in the source files and configuring the parser upfront for that. But the internally created file handles (created by the NameEnvironment) doesn't seem to be closed again.

This is the call stack that causes the file handles to be created:

	at java.util.zip.ZipFile.<init>(ZipFile.java:156)
	at java.util.zip.ZipFile.<init>(ZipFile.java:169)
	at org.eclipse.jdt.internal.compiler.batch.ClasspathJar.initialize(ClasspathJar.java:202)
	at org.eclipse.jdt.internal.compiler.batch.FileSystem.<init>(FileSystem.java:210)
	at org.eclipse.jdt.internal.compiler.batch.FileSystem.<init>(FileSystem.java:252)
	at org.eclipse.jdt.core.dom.NameEnvironmentWithProgress.<init>(NameEnvironmentWithProgress.java:36)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:649)
	at org.eclipse.jdt.core.dom.ASTParser.createASTs(ASTParser.java:1012)

The underlying NameEnvironmentWithProgress has a "reset" method, but that is never called when using the ASTParser. I think it should be called at the end of the org.eclipse.jdt.core.dom.ASTParser.createASTs method, since the created NameEnvironmentWithProgress is locally created in that method and seems to never escape that scope.

What do you think? If I am on the right track here, I would be happy to submit a patch, but would love to circle this with you upfront to make sure I am not missing something here.
Comment 1 Stephan Herrmann CLA 2019-08-06 15:24:04 EDT
Omission of calling "reset" _could_ be a plain bug, but there's a catch: if you create AST with bindings, invoking methods of those bindings may require to call back into compiler lookup, including calls to the name environment (which can still be referenced via LookupEnvironment#nameEnvironment). 

Hence this reminds me of the session idea mentioned in bug 526520 comment 1.

If we had a clear concept of closing an ASTParser session, then, of course, this close should also reset the name environment.
Comment 2 Martin Lippert CLA 2019-08-07 05:29:02 EDT
Agree, the session construct would allow us to close the name environment and therefore release the file handles again.

In addition to that, given the existing API of the ASTParser, I still think that not closing the name environment is a bug here, since you have no way to do that yourself manually afterwards. The name environment is hidden in the CompilationUnitResolvers static method. It is referenced from the lookup environment, but that is not accessible from the outside world either and the reset method of the LookupEnvironment explicitly mentions that the name environment should be reset by the one who created it.

I think all the methods that call the CompilationUnitResolvers static methods are affected here. Maybe those methods should return a session object that allows to close the session (and calls the cleanup).
Comment 3 Eclipse Genie CLA 2023-04-16 09:16:19 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.