[jetty-dev] Resource leak with URLResource?

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

[jetty-dev] Resource leak with URLResource?

Jens Reimann
Hi,

I am just checking out a file handle leak with Jetty running in an OSGi
setup.

I do provide a custom "WebAppContext" which overrides getResource (…)
and returns Resource instances (created using Resource.newSource())
which are URL based and internally create a URLResource.

However doing so will cause the resources to be opened, but never closed
by the Jetty Servlet infrastructure.

I think the cause for this is in the method "URL
WebAppContext.Context.getResource(String path)". Which:
a) first it gets a resource
b) calls Resource.exists() -> which opens a stream for a URLResource
c) returns the URL by .getURL()
d) never closes the resource using Resource.close ();

This will work with FileResource instances since these implement exist
without actually opening the file. But on a URLResource most operations
do open a connection which has to be closed.

Thanks for your help

Jens

_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-dev] Resource leak with URLResource?

Joakim Erdfelt-9
Which version of Jetty, what version of Java, and on what OS?

--
Joakim Erdfelt <[hidden email]>
Expert advice, services and support from from the Jetty & CometD experts

On Wed, Apr 29, 2015 at 4:02 AM, Jens Reimann <[hidden email]> wrote:
Hi,

I am just checking out a file handle leak with Jetty running in an OSGi
setup.

I do provide a custom "WebAppContext" which overrides getResource (…)
and returns Resource instances (created using Resource.newSource())
which are URL based and internally create a URLResource.

However doing so will cause the resources to be opened, but never closed
by the Jetty Servlet infrastructure.

I think the cause for this is in the method "URL
WebAppContext.Context.getResource(String path)". Which:
a) first it gets a resource
b) calls Resource.exists() -> which opens a stream for a URLResource
c) returns the URL by .getURL()
d) never closes the resource using Resource.close ();

This will work with FileResource instances since these implement exist
without actually opening the file. But on a URLResource most operations
do open a connection which has to be closed.

Thanks for your help

Jens

_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev


_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-dev] Resource leak with URLResource?

Jens Reimann
Hi,

Sorry ...

* Jetty: 9.2.9.v20150224
* Java 8 (OpenJDK and Oracle 1.8.0_45)
* OS: Linux 32/64 bit, CentOS 6.4, Linux Mint 17.1

Jens

On 04/29/2015 02:47 PM, Joakim Erdfelt wrote:
Which version of Jetty, what version of Java, and on what OS?

--
Joakim Erdfelt <[hidden email]>
Expert advice, services and support from from the Jetty & CometD experts

On Wed, Apr 29, 2015 at 4:02 AM, Jens Reimann <[hidden email]> wrote:
Hi,

I am just checking out a file handle leak with Jetty running in an OSGi
setup.

I do provide a custom "WebAppContext" which overrides getResource (…)
and returns Resource instances (created using Resource.newSource())
which are URL based and internally create a URLResource.

However doing so will cause the resources to be opened, but never closed
by the Jetty Servlet infrastructure.

I think the cause for this is in the method "URL
WebAppContext.Context.getResource(String path)". Which:
a) first it gets a resource
b) calls Resource.exists() -> which opens a stream for a URLResource
c) returns the URL by .getURL()
d) never closes the resource using Resource.close ();

This will work with FileResource instances since these implement exist
without actually opening the file. But on a URLResource most operations
do open a connection which has to be closed.

Thanks for your help

Jens

_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev



_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev



_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-dev] Resource leak with URLResource?

Jan Bartel-3
Hi Jens,

The jvm can cache URL connections, such as JarUrlConnections and also
JarFile entries. By default, caching is enabled.  So, even if the
Resource was closed, because caching is enabled, the jvm will still
hold onto a reference to your url connection. You can try calling
Resource.setDefaultUseCaches(false) to disable caching, and you should
see that even without an explicit close, garbage collection should
eventually get rid of the URL connection/JarFile entries etc.

That said, I've opened an issue to look at any places in the code such
as this that we could call Resource.close():
https://bugs.eclipse.org/bugs/show_bug.cgi?id=465864

Jan
_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-dev] Resource leak with URLResource?

Jens Reimann
Hello Jan,

thanks for looking into this.

However I am not talking about JVM or Jetty caches. The Resource base
class does provide a few methods, like the "exists" check. For Resource
there are a few implementations, such as URLResource, which does perform
the "exists" check by opening the stream. Now if the URL is backed by a
file, due to some URL handler implementing the stream based on a file
resource, then the InputStream might be a FileInputStream, with a file
handle.

Now in this case the method checks for existence of the Resource, (which
creates a stream), but never calls close. So the stream will not get
closed. This might be ok for some stream types, although I doubt that,
but for a file stream, this will keep the file handle open.

So I think that either each resource must be closed properly, or the
check should not be allowed to cache the InputStream.

Thanks for creating the bug in bugzilla.

Jens

On 04/30/2015 12:33 AM, Jan Bartel wrote:

> Hi Jens,
>
> The jvm can cache URL connections, such as JarUrlConnections and also
> JarFile entries. By default, caching is enabled.  So, even if the
> Resource was closed, because caching is enabled, the jvm will still
> hold onto a reference to your url connection. You can try calling
> Resource.setDefaultUseCaches(false) to disable caching, and you should
> see that even without an explicit close, garbage collection should
> eventually get rid of the URL connection/JarFile entries etc.
>
> That said, I've opened an issue to look at any places in the code such
> as this that we could call Resource.close():
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=465864
>
> Jan
> _______________________________________________
> jetty-dev mailing list
> [hidden email]
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-dev


--
IBH SYSTEMS GmbH
D-85235 Pfaffenhofen an der Glonn
Läutenring 43
Geschäftsführer / CEO: Dr. Thomas Heitzig

Amtsgericht München
Handelsregister Nummer  HRB 197959
USt ID: DE267945175

Office Munich
D 80992 München
Agnes-Pockels-Bogen 1
T +49 89 18 9 17 49 0

The information transmitted is intended only for the person or entity
to which it is addressed and may contain confidential and/or pivileged
material. Any review, retransmission, dissemination or other use of,
or taking of any action in reliance upon, this information by persons
or entities other than the intended recipient is prohibited. If you
received this in error, please contact the sender and delete the
material from any computer.

_______________________________________________
jetty-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev