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

DataChunkedInputStream deadlock protection removed #2401

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,8 +38,6 @@
public class DataChunkInputStream extends InputStream {
private static final Logger LOGGER = Logger.getLogger(DataChunkInputStream.class.getName());

private final String originalThreadID;
private final boolean validate;
private final Flow.Publisher<DataChunk> originalPublisher;
private int bufferIndex;
private CompletableFuture<DataChunk> current = new CompletableFuture<>();
Expand Down Expand Up @@ -74,8 +72,6 @@ public DataChunkInputStream(Flow.Publisher<DataChunk> originalPublisher) {
*/
public DataChunkInputStream(Flow.Publisher<DataChunk> originalPublisher, boolean validate) {
this.originalPublisher = originalPublisher;
this.originalThreadID = getCurrentThreadIdent();
this.validate = validate;
}

/**
Expand Down Expand Up @@ -120,7 +116,6 @@ public int read() throws IOException {

@Override
public int read(byte[] buf, int off, int len) throws IOException {
validate();
if (subscribed.compareAndSet(false, true)) {
originalPublisher.subscribe(new DataChunkSubscriber()); // subscribe for first time
}
Expand Down Expand Up @@ -180,17 +175,6 @@ public int read(byte[] buf, int off, int len) throws IOException {
}
}

private String getCurrentThreadIdent() {
Thread thread = Thread.currentThread();
return thread.getName() + ":" + thread.getId();
}

private void validate() {
if (validate && originalThreadID.equals(getCurrentThreadIdent())) {
throw new IllegalStateException("DataChunkInputStream needs to be handled in separate thread to prevent deadlock.");
}
}

// -- DataChunkSubscriber -------------------------------------------------
//
// Following methods are executed by Netty IO threads (except first chunk)
Expand Down
Expand Up @@ -18,7 +18,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import javax.json.Json;
Expand All @@ -34,7 +33,6 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand Down Expand Up @@ -166,55 +164,6 @@ public void testRequestSpecificReader() throws Exception {
.get();
}

@Test
public void testInputStreamSameThread() {
ExecutionException exception = assertThrows(ExecutionException.class, () -> {
webClient.get()
.request(InputStream.class)
.thenApply(it -> {
try {
it.readAllBytes();
} catch (IOException ignored) {
}
fail("This should have failed!");
return CompletableFuture.completedFuture(it);
})
.toCompletableFuture()
.get();
});
if (exception.getCause() instanceof IllegalStateException) {
assertThat(exception.getCause().getMessage(),
is("DataChunkInputStream needs to be handled in separate thread to prevent deadlock."));
} else {
fail(exception);
}
}

@Test
public void testInputStreamSameThreadTestContentAs() {
ExecutionException exception = assertThrows(ExecutionException.class, () -> {
webClient.get()
.request()
.thenCompose(it -> it.content().as(InputStream.class))
.thenApply(it -> {
try {
it.readAllBytes();
} catch (IOException ignored) {
}
fail("This should have failed!");
return CompletableFuture.completedFuture(it);
})
.toCompletableFuture()
.get();
});
if (exception.getCause() instanceof IllegalStateException) {
assertThat(exception.getCause().getMessage(),
is("DataChunkInputStream needs to be handled in separate thread to prevent deadlock."));
} else {
fail(exception);
}
}

@Test
public void testInputStreamDifferentThread() throws IOException, ExecutionException, InterruptedException {
InputStream is = webClient.get()
Expand Down