Skip to content

Commit

Permalink
Memory leak when using Jersey with HK2 eclipse-ee4j#5796
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Feb 6, 2025
1 parent 382f69e commit 68fd3ad
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -55,6 +55,7 @@
import org.glassfish.jersey.internal.util.collection.LazyValue;
import org.glassfish.jersey.internal.util.collection.Value;
import org.glassfish.jersey.internal.util.collection.Values;
import org.glassfish.jersey.message.internal.MessageBodyFactory;
import org.glassfish.jersey.model.internal.CommonConfig;
import org.glassfish.jersey.model.internal.ComponentBag;
import org.glassfish.jersey.model.internal.ManagedObjectsFinalizer;
Expand Down Expand Up @@ -416,17 +417,12 @@ private ClientRuntime initRuntime() {

final ClientBootstrapBag bootstrapBag = new ClientBootstrapBag();
bootstrapBag.setManagedObjectsFinalizer(new ManagedObjectsFinalizer(injectionManager));

final ClientMessageBodyFactory.MessageBodyWorkersConfigurator messageBodyWorkersConfigurator =
new ClientMessageBodyFactory.MessageBodyWorkersConfigurator();

List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(
new RequestScope.RequestScopeConfigurator(),
List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(new RequestScope.RequestScopeConfigurator(),
new ParamConverterConfigurator(),
new ParameterUpdaterConfigurator(),
new RuntimeConfigConfigurator(runtimeCfgState),
new ContextResolverFactory.ContextResolversConfigurator(),
messageBodyWorkersConfigurator,
new MessageBodyFactory.MessageBodyWorkersConfigurator(),
new ExceptionMapperFactory.ExceptionMappersConfigurator(),
new JaxrsProviders.ProvidersConfigurator(),
new AutoDiscoverableConfigurator(RuntimeType.CLIENT),
Expand Down Expand Up @@ -467,8 +463,6 @@ private ClientRuntime initRuntime() {
final ClientRuntime crt = new ClientRuntime(configuration, connector, injectionManager, bootstrapBag);

client.registerShutdownHook(crt);
messageBodyWorkersConfigurator.setClientRuntime(crt);

return crt;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.glassfish.jersey.client;

import java.io.InputStream;
import java.util.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -173,6 +174,8 @@ Runnable createRunnableForAsyncProcessing(ClientRequest request, final ResponseC

@Override
public void response(final ClientResponse response) {
InputStream in = response.getEntityStream();
request.getClientConfig().getClient().putClientRuntimeLifeCycle(in, ClientRuntime.this);
requestScope.runInScope(() -> processResponse(request, response, callback));
}

Expand Down Expand Up @@ -298,6 +301,8 @@ public ClientResponse invoke(final ClientRequest request) {

try {
response = connector.apply(addUserAgent(Stages.process(request, requestProcessingRoot), connector.getName()));
InputStream in = response.getEntityStream();
request.getClientConfig().getClient().putClientRuntimeLifeCycle(in, this);
} catch (final AbortException aborted) {
response = aborted.getAbortResponse();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2025 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,11 +16,13 @@

package org.glassfish.jersey.client;

import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
Expand Down Expand Up @@ -69,6 +71,8 @@ public SSLContext getDefaultSslContext() {
private final LinkedBlockingDeque<WeakReference<JerseyClient.ShutdownHook>> shutdownHooks =
new LinkedBlockingDeque<WeakReference<JerseyClient.ShutdownHook>>();
private final ReferenceQueue<JerseyClient.ShutdownHook> shReferenceQueue = new ReferenceQueue<JerseyClient.ShutdownHook>();
// Keeps ClientRuntime alive till InputStream is GCed
private final Map<InputStream, ClientRuntime> clientRuntimeLifeCycle = new WeakHashMap<>();

/**
* Client instance shutdown hook.
Expand Down Expand Up @@ -387,4 +391,8 @@ public JerseyClient preInitialize() {
config.preInitialize();
return this;
}

void putClientRuntimeLifeCycle(InputStream in, ClientRuntime cr) {
clientRuntimeLifeCycle.put(in, cr);
}
}
42 changes: 42 additions & 0 deletions tests/integration/jersey-5796/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>org.glassfish.jersey.tests.integration</groupId>
<version>2.47-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jersey-5796</artifactId>
<name>jersey-tests-integration-jersey-5796</name>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-bundle</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.jersey5796;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.concurrent.LinkedBlockingDeque;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ChunkedInput;
import org.glassfish.jersey.server.ChunkedOutput;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.jupiter.api.Test;


public class Jersey5796Test extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(Resource.class);
}

@Test
public void testMemoryLeak() throws Exception {
Client client = ClientBuilder.newClient();
for (int i = 0; i < 50; i++) {
Response response = client.target(getBaseUri()).property("test", "test").path("/get1").request().get();
assertEquals("GET", response.readEntity(String.class));
}
Runtime.getRuntime().gc();
// Give some time to GC
Thread.sleep(500);
assertEquals(0, livingClientRuntimeInstances(client));
client.close();
}

/* Reproduces issue 4507
MultiException stack 1 of 1
java.lang.IllegalStateException: ServiceLocatorImpl(__HK2_Generated_0,0,427183206) has been shut down
at org.jvnet.hk2.internal.ServiceLocatorImpl.checkState(ServiceLocatorImpl.java:2399)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandleImpl(ServiceLocatorImpl.java:627)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandle(ServiceLocatorImpl.java:620)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandle(ServiceLocatorImpl.java:638)
at org.jvnet.hk2.internal.FactoryCreator.getFactoryHandle(FactoryCreator.java:79)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:149)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:521)
at org.glassfish.jersey.inject.hk2.RequestContext.lambda$findOrCreate$0(RequestContext.java:60)
at org.glassfish.jersey.internal.inject.ForeignDescriptorImpl.dispose(ForeignDescriptorImpl.java:63)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.remove(Hk2RequestScope.java:126)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:143)
at org.glassfish.jersey.server.ChunkedOutput.flushQueue(ChunkedOutput.java:405)
at org.glassfish.jersey.server.ChunkedOutput.write(ChunkedOutput.java:264)
at org.glassfish.jersey.tests.integration.jersey5796.Jersey5796Test$Resource.lambda$get2$0(Jersey5796Test.java:116)
at java.base/java.lang.Thread.run(Thread.java:1583)
*/
@Test
public void testChunkedInput() throws Exception {
Client client = ClientBuilder.newClient();
assertEquals(0, livingClientRuntimeInstances(client));
for (int i = 0; i < 50; i++) {
ChunkedInput<String> chunkedInput = client.target(getBaseUri()).property("test", "test")
.path("/get2").request().get(new GenericType<ChunkedInput<String>>() {});
chunkedInput.setParser(ChunkedInput.createParser("\n"));
int j = 0;
String chunk;
while ((chunk = chunkedInput.read()) != null) {
assertEquals("Chunk " + j, chunk);
j++;
}
}
Runtime.getRuntime().gc();
Thread.sleep(500);
assertEquals(0, livingClientRuntimeInstances(client));
client.close();
}

private int livingClientRuntimeInstances(Client client) throws Exception {
Class<?> clientRuntime = Class.forName("org.glassfish.jersey.client.ClientRuntime");
Class<?> clazz = client.getClass();
Field field = clazz.getDeclaredField("shutdownHooks");
field.setAccessible(true);
LinkedBlockingDeque<WeakReference<?>> shutdownHooks = (LinkedBlockingDeque<WeakReference<?>>) field.get(client);
int counter = 0;
for (WeakReference<?> ref : shutdownHooks) {
if (ref.get() != null && ref.get().getClass() == clientRuntime) {
counter++;
}
}
return counter;
}

@Path("/")
public static class Resource {

@GET
@Path("/get1")
public String get1() {
return "GET";
}

@GET
@Path("/get2")
public ChunkedOutput<String> get2() {
ChunkedOutput<String> output = new ChunkedOutput<>(String.class);
new Thread(() -> {
try {
for (int i = 0; i < 3; i++) {
output.write("Chunk " + i + "\n");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
output.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}).start();
return output;
}
}
}
1 change: 1 addition & 0 deletions tests/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<module>jersey-4697</module>
<module>jersey-4722</module>
<module>jersey-4949</module>
<module>jersey-5796</module>
<module>jetty-response-close</module>
<module>microprofile</module>
<module>portability-jersey-1</module>
Expand Down

0 comments on commit 68fd3ad

Please sign in to comment.