Why doesn’t jdk.internal.loader.Loader implement AutoCloseable?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why doesn’t jdk.internal.loader.Loader implement AutoCloseable?

Christian Stein
Hi,

following the documentation of ModulLayer [1] to create a
temporary layer for testing, results in a FileSystemException
when trying to delete the modular jar file after tests are finished.

On Windows, at least.
Find more details and a minimal working example here: [2]

An implementation of a `Loader.close()` method similar to the
one found URLClassLoader [3] would solve the problem.

Shall I open an issue over at https://bugs.openjdk.java.net?

Cheers,
Christian

[1]
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/ModuleLayer.html
[2] https://sormuras.github.io/blog/2019-05-26-jdk-module-layer-class-loader
[3]
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/net/URLClassLoader.html#close()
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn’t jdk.internal.loader.Loader implement AutoCloseable?

Alan Bateman
On 26/05/2019 06:45, Christian Stein wrote:
> Hi,
>
> following the documentation of ModulLayer [1] to create a
> temporary layer for testing, results in a FileSystemException
> when trying to delete the modular jar file after tests are finished.
Can you create a WeakReference to the module layer and do the cleanup
when the reference is queued? When a module layer is GC'ed then you
should find the JAR files containing modules will be closed and you can
delete them. As this is testing then maybe the test finish can null out
the reference to the module layer (and the class loaders if you have
references to those), call System.gc, and wait for the weak reference to
be cleaned.

That said, it might be time to change the Windows sharing mode that
JarFile/ZipFile uses to open JAR files. I'll create an issue for that.

-Alan
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn’t jdk.internal.loader.Loader implement AutoCloseable?

Christian Stein
On Sun, May 26, 2019 at 10:31 AM Alan Bateman <[hidden email]>
wrote:

> On 26/05/2019 06:45, Christian Stein wrote:
> > Hi,
> >
> > following the documentation of ModulLayer [1] to create a
> > temporary layer for testing, results in a FileSystemException
> > when trying to delete the modular jar file after tests are finished.
> Can you create a WeakReference to the module layer and do the cleanup
> when the reference is queued? When a module layer is GC'ed then you
> should find the JAR files containing modules will be closed and you can
> delete them. As this is testing then maybe the test finish can null out
> the reference to the module layer (and the class loaders if you have
> references to those), call System.gc, and wait for the weak reference to
> be cleaned.
>

Only tried to null out the loader, yet.

So I added the layer and the reference to a class loaded by
the layers' loader to the "to be nulled out ensemble" [1]:

  layer = null;
  loader = null;
  mainClass = null;

  System.gc();
  Thread.sleep(2000);

...et voilà, the jar file is unlocked a can be deleted.
Many thanks for the hint.


> That said, it might be time to change the Windows sharing mode that
> JarFile/ZipFile uses to open JAR files. I'll create an issue for that.
>

Sounds great! Still early enough for JDK 13? ;-)

[1]
https://github.com/sormuras/sormuras.github.io/blob/master/demo/test/jdk/JarLock.java#L69
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn’t jdk.internal.loader.Loader implement AutoCloseable?

Christian Stein
In reply to this post by Alan Bateman
On Sun, May 26, 2019 at 10:31 AM Alan Bateman <[hidden email]>
wrote:

> That said, it might be time to change the Windows sharing mode that
> JarFile/ZipFile uses to open JAR files. I'll create an issue for that.
>
> -Alan
>

Is this the related issue?

https://bugs.openjdk.java.net/browse/JDK-8224794
Reply | Threaded
Open this post in threaded view
|

Re: Why doesn’t jdk.internal.loader.Loader implement AutoCloseable?

Alan Bateman
On 07/06/2019 07:31, Christian Stein wrote:
> :
> Is this the related issue?
>
> https://bugs.openjdk.java.net/browse/JDK-8224794
Yes, that's it. I should have sent the link here as follow-up to that
part of the discussion.