|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.tests.integration.jersey5796; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +import java.lang.ref.WeakReference; |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.concurrent.LinkedBlockingDeque; |
| 24 | + |
| 25 | +import javax.ws.rs.GET; |
| 26 | +import javax.ws.rs.Path; |
| 27 | +import javax.ws.rs.client.Client; |
| 28 | +import javax.ws.rs.client.ClientBuilder; |
| 29 | +import javax.ws.rs.core.Application; |
| 30 | +import javax.ws.rs.core.GenericType; |
| 31 | +import javax.ws.rs.core.Response; |
| 32 | + |
| 33 | +import org.glassfish.jersey.client.ChunkedInput; |
| 34 | +import org.glassfish.jersey.server.ChunkedOutput; |
| 35 | +import org.glassfish.jersey.server.ResourceConfig; |
| 36 | +import org.glassfish.jersey.test.JerseyTest; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | + |
| 40 | +public class Jersey5796Test extends JerseyTest { |
| 41 | + |
| 42 | + @Override |
| 43 | + protected Application configure() { |
| 44 | + return new ResourceConfig(Resource.class); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testMemoryLeak() throws Exception { |
| 49 | + Client client = ClientBuilder.newClient(); |
| 50 | + for (int i = 0; i < 50; i++) { |
| 51 | + Response response = client.target(getBaseUri()).property("test", "test").path("/get1").request().get(); |
| 52 | + assertEquals("GET", response.readEntity(String.class)); |
| 53 | + } |
| 54 | + Runtime.getRuntime().gc(); |
| 55 | + // Give some time to GC |
| 56 | + Thread.sleep(500); |
| 57 | + assertEquals(0, livingClientRuntimeInstances(client)); |
| 58 | + client.close(); |
| 59 | + } |
| 60 | + |
| 61 | + /* Reproduces issue 4507 |
| 62 | + MultiException stack 1 of 1 |
| 63 | + java.lang.IllegalStateException: ServiceLocatorImpl(__HK2_Generated_0,0,427183206) has been shut down |
| 64 | + at org.jvnet.hk2.internal.ServiceLocatorImpl.checkState(ServiceLocatorImpl.java:2399) |
| 65 | + at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandleImpl(ServiceLocatorImpl.java:627) |
| 66 | + at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandle(ServiceLocatorImpl.java:620) |
| 67 | + at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandle(ServiceLocatorImpl.java:638) |
| 68 | + at org.jvnet.hk2.internal.FactoryCreator.getFactoryHandle(FactoryCreator.java:79) |
| 69 | + at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:149) |
| 70 | + at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:521) |
| 71 | + at org.glassfish.jersey.inject.hk2.RequestContext.lambda$findOrCreate$0(RequestContext.java:60) |
| 72 | + at org.glassfish.jersey.internal.inject.ForeignDescriptorImpl.dispose(ForeignDescriptorImpl.java:63) |
| 73 | + at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.remove(Hk2RequestScope.java:126) |
| 74 | + at java.base/java.lang.Iterable.forEach(Iterable.java:75) |
| 75 | + at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:143) |
| 76 | + at org.glassfish.jersey.server.ChunkedOutput.flushQueue(ChunkedOutput.java:405) |
| 77 | + at org.glassfish.jersey.server.ChunkedOutput.write(ChunkedOutput.java:264) |
| 78 | + at org.glassfish.jersey.tests.integration.jersey5796.Jersey5796Test$Resource.lambda$get2$0(Jersey5796Test.java:116) |
| 79 | + at java.base/java.lang.Thread.run(Thread.java:1583) |
| 80 | +
|
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void testChunkedInput() throws Exception { |
| 84 | + Client client = ClientBuilder.newClient(); |
| 85 | + assertEquals(0, livingClientRuntimeInstances(client)); |
| 86 | + for (int i = 0; i < 50; i++) { |
| 87 | + ChunkedInput<String> chunkedInput = client.target(getBaseUri()).property("test", "test") |
| 88 | + .path("/get2").request().get(new GenericType<ChunkedInput<String>>() {}); |
| 89 | + chunkedInput.setParser(ChunkedInput.createParser("\n")); |
| 90 | + int j = 0; |
| 91 | + String chunk; |
| 92 | + while ((chunk = chunkedInput.read()) != null) { |
| 93 | + assertEquals("Chunk " + j, chunk); |
| 94 | + j++; |
| 95 | + } |
| 96 | + } |
| 97 | + Runtime.getRuntime().gc(); |
| 98 | + Thread.sleep(500); |
| 99 | + assertEquals(0, livingClientRuntimeInstances(client)); |
| 100 | + client.close(); |
| 101 | + } |
| 102 | + |
| 103 | + private int livingClientRuntimeInstances(Client client) throws Exception { |
| 104 | + Class<?> clientRuntime = Class.forName("org.glassfish.jersey.client.ClientRuntime"); |
| 105 | + Class<?> clazz = client.getClass(); |
| 106 | + Field field = clazz.getDeclaredField("shutdownHooks"); |
| 107 | + field.setAccessible(true); |
| 108 | + LinkedBlockingDeque<WeakReference<?>> shutdownHooks = (LinkedBlockingDeque<WeakReference<?>>) field.get(client); |
| 109 | + int counter = 0; |
| 110 | + for (WeakReference<?> ref : shutdownHooks) { |
| 111 | + if (ref.get() != null && ref.get().getClass() == clientRuntime) { |
| 112 | + counter++; |
| 113 | + } |
| 114 | + } |
| 115 | + return counter; |
| 116 | + } |
| 117 | + |
| 118 | + @Path("/") |
| 119 | + public static class Resource { |
| 120 | + |
| 121 | + @GET |
| 122 | + @Path("/get1") |
| 123 | + public String get1() { |
| 124 | + return "GET"; |
| 125 | + } |
| 126 | + |
| 127 | + @GET |
| 128 | + @Path("/get2") |
| 129 | + public ChunkedOutput<String> get2() { |
| 130 | + ChunkedOutput<String> output = new ChunkedOutput<>(String.class); |
| 131 | + new Thread(() -> { |
| 132 | + try { |
| 133 | + for (int i = 0; i < 3; i++) { |
| 134 | + output.write("Chunk " + i + "\n"); |
| 135 | + } |
| 136 | + } catch (Exception e1) { |
| 137 | + e1.printStackTrace(); |
| 138 | + } finally { |
| 139 | + try { |
| 140 | + output.close(); |
| 141 | + } catch (Exception e2) { |
| 142 | + e2.printStackTrace(); |
| 143 | + } |
| 144 | + } |
| 145 | + }).start(); |
| 146 | + return output; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments