Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CXF-8992: WebClient.fromClient() broken due to garbage collection #2281

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;

import javax.xml.stream.XMLStreamWriter;

import jakarta.annotation.Nullable;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.WebApplicationException;
Expand Down Expand Up @@ -130,6 +132,34 @@ public abstract class AbstractClient implements Client {
protected ClientConfiguration cfg = new ClientConfiguration();
private ClientState state;
private final AtomicBoolean closed = new AtomicBoolean();
private volatile ConfigurationReference configurationReference = new ConfigurationReference(cfg);

protected static final class ConfigurationReference {
private final AtomicLong refCount = new AtomicLong(1);
private final ClientConfiguration cfg;

public ConfigurationReference(ClientConfiguration cfg) {
this.cfg = cfg;
}

public ConfigurationReference acquire() {
refCount.incrementAndGet();
return this;
}

public long refCount() {
return refCount.get();
}

public long release() {
return refCount.decrementAndGet();
}

public ClientConfiguration getConfiguration() {
return cfg;
}
}

protected AbstractClient(ClientState initialState) {
this.state = initialState;
}
Expand Down Expand Up @@ -349,34 +379,47 @@ public void close() {
if (cfg.getBus() == null) {
return;
}
cfg.getEndpoint().getCleanupHooks().

final ConfigurationReference reference = configurationReference;
final boolean shutdownConfiguration = reference == null || reference.release() == 0L;
if (shutdownConfiguration) {
cfg.getEndpoint().getCleanupHooks().
forEach(c -> {
try {
c.close();
} catch (IOException e) {
//ignore
}
});
ClientLifeCycleManager mgr = cfg.getBus().getExtension(ClientLifeCycleManager.class);
}

final ClientLifeCycleManager mgr = cfg.getBus().getExtension(ClientLifeCycleManager.class);
if (null != mgr) {
mgr.clientDestroyed(new FrontendClientAdapter(getConfiguration()));
}

if (cfg.getConduitSelector() instanceof Closeable) {
try {
((Closeable)cfg.getConduitSelector()).close();
} catch (IOException e) {
//ignore, we're destroying anyway
if (shutdownConfiguration) {
if (cfg.getConduitSelector() instanceof Closeable) {
try {
((Closeable)cfg.getConduitSelector()).close();
} catch (IOException e) {
//ignore, we're destroying anyway
}
} else {
cfg.getConduit().close();
}
} else {
cfg.getConduit().close();
}

// reset state
state.reset();
if (cfg.isShutdownBusOnClose()) {

if (shutdownConfiguration && cfg.isShutdownBusOnClose()) {
cfg.getBus().shutdown(false);
}

state = null;
cfg = null;
configurationReference = null;
}
}

Expand Down Expand Up @@ -908,6 +951,7 @@ public ClientConfiguration getConfiguration() {

protected void setConfiguration(ClientConfiguration config) {
cfg = config;
configurationReference = new ConfigurationReference(config);
}

// Note that some conduit selectors may update Message.ENDPOINT_ADDRESS
Expand Down Expand Up @@ -1328,4 +1372,26 @@ protected void closeAsyncResponseIfPossible(Response r, Message outMessage, Jaxr
protected void handleAsyncFault(Message message) {
}
}

/**
* References the configuration (by another client) so it won't shut down till all the
* client instances are closed.
* @param reference configuration reference
*/
protected void setConfigurationReference(ConfigurationReference reference) {
if (reference == null) {
throw new IllegalArgumentException("The configuration reference is not set "
+ "(the client was already closed)");
}
this.configurationReference = reference.acquire();
this.cfg = configurationReference.getConfiguration();
}

/**
* Returns configuration reference
* @return configuration reference
*/
protected @Nullable ConfigurationReference getConfigurationReference() {
return this.configurationReference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
*
*/
public class WebClient extends AbstractClient {
// Use client configuration reference instead of sharing the instance directly
public static final String USE_CONFIGURATION_REFERENCE_WHEN_COPY = "use.configuration.reference.when.copy";

private static final String REQUEST_CLASS = "request.class";
private static final String REQUEST_TYPE = "request.type";
private static final String REQUEST_ANNS = "request.annotations";
Expand Down Expand Up @@ -1232,7 +1235,21 @@ protected void doWriteBody(Message outMessage,
static void copyProperties(Client toClient, Client fromClient) {
AbstractClient newClient = toAbstractClient(toClient);
AbstractClient oldClient = toAbstractClient(fromClient);
newClient.setConfiguration(oldClient.getConfiguration());
final ClientConfiguration oldCfg = oldClient.getConfiguration();

boolean useConfigurationReference = true;
if (oldCfg != null && oldCfg.getBus() != null) {
Object useConfigurationReferenceProp = oldCfg.getBus().getProperty(USE_CONFIGURATION_REFERENCE_WHEN_COPY);
if (useConfigurationReferenceProp != null) {
useConfigurationReference = PropertyUtils.isTrue(useConfigurationReferenceProp);
}
}

if (useConfigurationReference) {
newClient.setConfigurationReference(oldClient.getConfigurationReference());
} else {
newClient.setConfiguration(oldClient.getConfiguration());
}
}

private static AbstractClient toAbstractClient(Object client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@

import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -268,6 +273,44 @@ public void testInvokePathEmptyAllowed() throws Exception {
assertNotNull(store.getBook(""));
}

@Test
public void testCreateClientFrom() throws Exception {
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress("http://bar");
bean.setResourceClass(BookStore.class);

final Client client = bean.create();
final WebClient wc = WebClient.fromClient(client);
assertThat(wc.getConfigurationReference(), is(not(nullValue())));
assertThat(wc.getConfigurationReference().refCount(), equalTo(2L));

client.close();
assertThat(wc.getConfigurationReference().refCount(), equalTo(1L));

wc.close();
assertThat(wc.getConfigurationReference(), is(nullValue()));
}

@Test
public void testCreateClientFromAndInvoke() throws Exception {
final SuperBookStore superBookResource = JAXRSClientFactory
.create("http://localhost:9000", SuperBookStore.class);
final Client client = (Client) superBookResource;
final WebClient wc = WebClient.fromClient(client);

final Book book = superBookResource.getNewBook("id4", true);
assertNotNull(book);

assertThat(wc.getConfigurationReference(), is(not(nullValue())));
assertThat(wc.getConfigurationReference().refCount(), equalTo(2L));

client.close();
assertThat(wc.getConfigurationReference().refCount(), equalTo(1L));

wc.close();
assertThat(wc.getConfigurationReference(), is(nullValue()));

}

private final class TestFeature extends AbstractFeature {
private TestInterceptor testInterceptor;
Expand Down
Loading
Loading