-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Issue #12272 - Potential deadlock with Vaadin. #12506
Merged
sbordet
merged 1 commit into
jetty-12.0.x
from
fix/jetty-12.0.x/12272/h2-shutdown-pending-request
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
@@ -33,23 +35,29 @@ | |
import org.eclipse.jetty.http.HttpURI; | ||
import org.eclipse.jetty.http.HttpVersion; | ||
import org.eclipse.jetty.http.MetaData; | ||
import org.eclipse.jetty.http2.CloseState; | ||
import org.eclipse.jetty.http2.ErrorCode; | ||
import org.eclipse.jetty.http2.HTTP2Session; | ||
import org.eclipse.jetty.http2.api.Session; | ||
import org.eclipse.jetty.http2.api.Stream; | ||
import org.eclipse.jetty.http2.client.HTTP2Client; | ||
import org.eclipse.jetty.http2.frames.DataFrame; | ||
import org.eclipse.jetty.http2.frames.HeadersFrame; | ||
import org.eclipse.jetty.http2.frames.ResetFrame; | ||
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory; | ||
import org.eclipse.jetty.io.EofException; | ||
import org.eclipse.jetty.server.HttpConfiguration; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.util.Callback; | ||
import org.eclipse.jetty.util.FuturePromise; | ||
import org.eclipse.jetty.util.component.LifeCycle; | ||
import org.eclipse.jetty.util.thread.QueuedThreadPool; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
@@ -64,15 +72,20 @@ public class Http2AsyncIOServletTest | |
|
||
private void start(HttpServlet httpServlet) throws Exception | ||
{ | ||
server = new Server(); | ||
QueuedThreadPool serverThreads = new QueuedThreadPool(); | ||
serverThreads.setName("server"); | ||
server = new Server(serverThreads); | ||
connector = new ServerConnector(server, 1, 1, new HTTP2CServerConnectionFactory(httpConfig)); | ||
server.addConnector(connector); | ||
ServletContextHandler servletContextHandler = new ServletContextHandler("/"); | ||
servletContextHandler.addServlet(new ServletHolder(httpServlet), "/*"); | ||
server.setHandler(servletContextHandler); | ||
server.start(); | ||
|
||
QueuedThreadPool clientThreads = new QueuedThreadPool(); | ||
clientThreads.setName("client"); | ||
client = new HTTP2Client(); | ||
client.setExecutor(clientThreads); | ||
client.start(); | ||
} | ||
|
||
|
@@ -218,4 +231,206 @@ public void onStartAsync(AsyncEvent event) | |
|
||
assertTrue(errorLatch.await(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {false, true}) | ||
public void testSessionCloseWithPendingRequestThenReset(boolean useReaderWriter) throws Exception | ||
{ | ||
// Disable output aggregation for Servlets, so each byte is echoed back. | ||
httpConfig.setOutputAggregationSize(0); | ||
CountDownLatch serverFailureLatch = new CountDownLatch(1); | ||
start(new HttpServlet() | ||
{ | ||
@Override | ||
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
try | ||
{ | ||
if (useReaderWriter) | ||
request.getReader().transferTo(response.getWriter()); | ||
else | ||
request.getInputStream().transferTo(response.getOutputStream()); | ||
} | ||
catch (Throwable x) | ||
{ | ||
serverFailureLatch.countDown(); | ||
throw x; | ||
} | ||
} | ||
}); | ||
|
||
HTTP2Session session = (HTTP2Session)client.connect(new InetSocketAddress("localhost", connector.getLocalPort()), new Session.Listener() {}) | ||
.get(5, TimeUnit.SECONDS); | ||
Queue<Stream.Data> dataList = new ConcurrentLinkedQueue<>(); | ||
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("/"), HttpVersion.HTTP_2, HttpFields.EMPTY); | ||
Stream stream = session.newStream(new HeadersFrame(request, null, false), new Stream.Listener() | ||
{ | ||
@Override | ||
public void onDataAvailable(Stream stream) | ||
{ | ||
while (true) | ||
{ | ||
Stream.Data data = stream.readData(); | ||
if (data == null) | ||
{ | ||
stream.demand(); | ||
return; | ||
} | ||
dataList.offer(data); | ||
if (data.frame().isEndStream()) | ||
return; | ||
} | ||
} | ||
}).get(5, TimeUnit.SECONDS); | ||
|
||
stream.data(new DataFrame(stream.getId(), UTF_8.encode("Hello Jetty"), false)) | ||
.get(5, TimeUnit.SECONDS); | ||
stream.demand(); | ||
|
||
await().atMost(5, TimeUnit.SECONDS).until(() -> !dataList.isEmpty()); | ||
|
||
// Initiates graceful close, waits for the streams to finish as per specification. | ||
session.close(ErrorCode.NO_ERROR.code, "client_close", Callback.NOOP); | ||
|
||
// Finish the pending stream, either by resetting or sending the last frame. | ||
stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code)); | ||
|
||
// The server should see the effects of the reset. | ||
assertTrue(serverFailureLatch.await(5, TimeUnit.SECONDS)); | ||
// The session must eventually be closed. | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> session.getCloseState() == CloseState.CLOSED); | ||
// The endPoint must eventually be closed. | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> !session.getEndPoint().isOpen()); | ||
|
||
// Cleanup. | ||
dataList.forEach(Stream.Data::release); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {false, true}) | ||
public void testSessionCloseWithPendingRequestServerIdleTimeout(boolean useReaderWriter) throws Exception | ||
{ | ||
// Disable output aggregation for Servlets, so each byte is echoed back. | ||
httpConfig.setOutputAggregationSize(0); | ||
CountDownLatch serverFailureLatch = new CountDownLatch(1); | ||
start(new HttpServlet() | ||
{ | ||
@Override | ||
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
try | ||
{ | ||
if (useReaderWriter) | ||
request.getReader().transferTo(response.getWriter()); | ||
else | ||
request.getInputStream().transferTo(response.getOutputStream()); | ||
} | ||
catch (Throwable x) | ||
{ | ||
serverFailureLatch.countDown(); | ||
throw x; | ||
} | ||
} | ||
}); | ||
long idleTimeout = 1000; | ||
connector.setIdleTimeout(idleTimeout); | ||
|
||
HTTP2Session session = (HTTP2Session)client.connect(new InetSocketAddress("localhost", connector.getLocalPort()), new Session.Listener() {}) | ||
.get(5, TimeUnit.SECONDS); | ||
Queue<Stream.Data> dataList = new ConcurrentLinkedQueue<>(); | ||
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("/"), HttpVersion.HTTP_2, HttpFields.EMPTY); | ||
Stream stream = session.newStream(new HeadersFrame(request, null, false), new Stream.Listener() | ||
{ | ||
@Override | ||
public void onDataAvailable(Stream stream) | ||
{ | ||
while (true) | ||
{ | ||
Stream.Data data = stream.readData(); | ||
if (data == null) | ||
{ | ||
stream.demand(); | ||
return; | ||
} | ||
dataList.offer(data); | ||
if (data.frame().isEndStream()) | ||
return; | ||
} | ||
} | ||
}).get(5, TimeUnit.SECONDS); | ||
|
||
stream.data(new DataFrame(stream.getId(), UTF_8.encode("Hello Jetty"), false)) | ||
.get(5, TimeUnit.SECONDS); | ||
stream.demand(); | ||
|
||
await().atMost(5, TimeUnit.SECONDS).until(() -> !dataList.isEmpty()); | ||
|
||
// Initiates graceful close, waits for the streams to finish as per specification. | ||
session.close(ErrorCode.NO_ERROR.code, "client_close", Callback.NOOP); | ||
|
||
// Do not finish the streams, the server must idle timeout. | ||
assertTrue(serverFailureLatch.await(2 * idleTimeout, TimeUnit.SECONDS)); | ||
// The session must eventually be closed. | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> session.getCloseState() == CloseState.CLOSED); | ||
// The endPoint must eventually be closed. | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> !session.getEndPoint().isOpen()); | ||
|
||
// Cleanup. | ||
dataList.forEach(Stream.Data::release); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {false, true}) | ||
public void testSessionCloseWithPendingRequestThenClientDisconnectThenServerIdleTimeout(boolean useReaderWriter) throws Exception | ||
{ | ||
AtomicReference<Thread> serverThreadRef = new AtomicReference<>(); | ||
CountDownLatch serverFailureLatch = new CountDownLatch(1); | ||
start(new HttpServlet() | ||
{ | ||
@Override | ||
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
try | ||
{ | ||
serverThreadRef.set(Thread.currentThread()); | ||
if (useReaderWriter) | ||
request.getReader().transferTo(response.getWriter()); | ||
else | ||
request.getInputStream().transferTo(response.getOutputStream()); | ||
} | ||
catch (Throwable x) | ||
{ | ||
serverFailureLatch.countDown(); | ||
throw x; | ||
} | ||
} | ||
}); | ||
long idleTimeout = 1000; | ||
connector.setIdleTimeout(idleTimeout); | ||
|
||
HTTP2Session session = (HTTP2Session)client.connect(new InetSocketAddress("localhost", connector.getLocalPort()), new Session.Listener() {}) | ||
.get(5, TimeUnit.SECONDS); | ||
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("/"), HttpVersion.HTTP_2, HttpFields.EMPTY); | ||
Stream stream = session.newStream(new HeadersFrame(request, null, false), new Stream.Listener() {}).get(5, TimeUnit.SECONDS); | ||
|
||
stream.data(new DataFrame(stream.getId(), UTF_8.encode("Hello Jetty"), false)) | ||
.get(5, TimeUnit.SECONDS); | ||
stream.demand(); | ||
|
||
await().atMost(5, TimeUnit.SECONDS).until(() -> | ||
{ | ||
Thread serverThread = serverThreadRef.get(); | ||
return serverThread != null && serverThread.getState() == Thread.State.WAITING; | ||
}); | ||
|
||
// Initiates graceful close, then immediately disconnect. | ||
session.close(ErrorCode.NO_ERROR.code, "client_close", Callback.from(session::disconnect)); | ||
|
||
// Do not finish the streams, the server must idle timeout. | ||
assertTrue(serverFailureLatch.await(2 * idleTimeout, TimeUnit.SECONDS)); | ||
// The session must eventually be closed. | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> session.getCloseState() == CloseState.CLOSED); | ||
// The endPoint must eventually be closed. | ||
await().atMost(5, TimeUnit.SECONDS).until(() -> !session.getEndPoint().isOpen()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cleanup could be done right after awaiting
!dataList.isEmpty()