Skip to content

Commit

Permalink
consume request body to break deadlock: client waits for server to cl…
Browse files Browse the repository at this point in the history
…ose socket, while server waits for client to close socket
  • Loading branch information
EugenDueck committed May 7, 2024
1 parent 0723c27 commit ce24cec
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/test/java/org/takes/http/BkBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.net.Socket;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.io.IOUtils;
import org.cactoos.bytes.BytesOf;
import org.cactoos.text.Joined;
import org.hamcrest.MatcherAssert;
Expand All @@ -49,6 +50,7 @@
import org.takes.misc.Href;
import org.takes.rq.RqFake;
import org.takes.rq.RqHeaders;
import org.takes.rq.RqLengthAware;
import org.takes.rq.RqPrint;
import org.takes.rq.RqSocket;
import org.takes.rs.ResponseOf;
Expand Down Expand Up @@ -167,7 +169,16 @@ void handlesTwoRequestInOneConnection() throws Exception {
new Thread(
() -> {
try {
new BkBasic(new TkText(text)).accept(
new BkBasic(req -> {
// we need to consume the request body to resolve the deadlock between RqLive.parse() trying
// to read the next byte from the socket in the line
// data = RqLive.data(input, new Opt.Empty<>());
// and the for loop further down in this test preventing the socket from closing on the
// client side (which would lead to an EOS being read by RqLive.parse(), making the server
// close the connection)
IOUtils.consume(new RqLengthAware(req).body());
return new TkText(text).act(req);
}).accept(
server.accept()
);
} catch (final IOException exception) {
Expand Down

0 comments on commit ce24cec

Please sign in to comment.