-
Notifications
You must be signed in to change notification settings - Fork 973
Fix a bug where an HTTP/1 request could fail with ClosedSessionException when sending requests concurrently.
#6229
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
Draft
ikhoon
wants to merge
1
commit into
line:main
Choose a base branch
from
ikhoon:http1-closedsessionexception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−1
Draft
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 hidden or 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
116 changes: 116 additions & 0 deletions
116
core/src/test/java/com/linecorp/armeria/client/Http1ClientClosedSessionExceptionTest.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Copyright 2025 LINE Corporation | ||
| * | ||
| * LINE Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| * | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.client; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
| import com.linecorp.armeria.common.CommonPools; | ||
| import com.linecorp.armeria.common.HttpHeaderNames; | ||
| import com.linecorp.armeria.common.SessionProtocol; | ||
| import com.linecorp.armeria.common.logging.RequestLog; | ||
| import com.linecorp.armeria.internal.testing.NettyServerExtension; | ||
|
|
||
| import io.netty.channel.Channel; | ||
| import io.netty.channel.ChannelDuplexHandler; | ||
| import io.netty.channel.ChannelFutureListener; | ||
| import io.netty.channel.ChannelHandlerContext; | ||
| import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
| import io.netty.handler.codec.http.FullHttpRequest; | ||
| import io.netty.handler.codec.http.HttpObjectAggregator; | ||
| import io.netty.handler.codec.http.HttpResponse; | ||
| import io.netty.handler.codec.http.HttpResponseStatus; | ||
| import io.netty.handler.codec.http.HttpServerCodec; | ||
| import io.netty.handler.codec.http.HttpVersion; | ||
|
|
||
| class Http1ClientClosedSessionExceptionTest { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(Http1ClientClosedSessionExceptionTest.class); | ||
|
|
||
| @RegisterExtension | ||
| static NettyServerExtension http1Server = new NettyServerExtension() { | ||
| @Override | ||
| protected void configure(Channel ch) throws Exception { | ||
| ch.pipeline().addLast(new HttpServerCodec()); | ||
| ch.pipeline().addLast(new HttpObjectAggregator(1024)); | ||
| ch.pipeline().addLast(new ChannelDuplexHandler() { | ||
| @Override | ||
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
| if (msg instanceof FullHttpRequest) { | ||
| final HttpResponse response = new DefaultFullHttpResponse( | ||
| HttpVersion.HTTP_1_1, HttpResponseStatus.OK); | ||
| // Make sure the connection is closed after the response is sent. | ||
| response.headers().add(HttpHeaderNames.CONNECTION, "close"); | ||
| ctx.writeAndFlush(response) | ||
| .addListener(ChannelFutureListener.CLOSE); | ||
| } else { | ||
| ctx.fireChannelRead(msg); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| @Test | ||
| void shouldSendHttp1RequestConcurrently() throws Throwable { | ||
| final WebClient client = WebClient.of(SessionProtocol.HTTP, http1Server.endpoint()); | ||
|
|
||
| SessionProtocolNegotiationCache.setUnsupported( | ||
| new InetSocketAddress("127.0.0.1", http1Server.address().getPort()), | ||
| SessionProtocol.H2C); | ||
| final AtomicReference<Throwable> failed = new AtomicReference<>(); | ||
| final Runnable loader = () -> { | ||
| while (failed.get() == null) { | ||
| try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { | ||
| try { | ||
| final CompletableFuture<AggregatedHttpResponse> future = client.get("/").aggregate(); | ||
| final ClientRequestContext ctx = captor.get(); | ||
| future.join(); | ||
| final RequestLog log = ctx.log().whenComplete().join(); | ||
| final Throwable responseCause = log.responseCause(); | ||
| if (responseCause != null) { | ||
| failed.set(responseCause); | ||
| } | ||
| } catch (Throwable ex) { | ||
| failed.set(new RuntimeException("Failed to receive the response. ctx: " + captor.get(), | ||
| ex)); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| for (int i = 0; i < 5; i++) { | ||
| CommonPools.blockingTaskExecutor().execute(loader); | ||
| } | ||
|
|
||
| for (int i = 0; i < 5; i++) { | ||
| Thread.sleep(1000); | ||
| if (failed.get() != null) { | ||
| throw failed.get(); | ||
| } | ||
| } | ||
| logger.info("All requests completed successfully"); | ||
| } | ||
| } |
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.
Makes me wonder is there a way to check in which state the channel is? We reproduced this issue in a production and forked this fix and it helped but not fully,
The bigger the delay was the less this reproduced, so it makes me believe that correct way would be to reschedule but check in callback if the state is suitable and if not reschedule again. The best way would have been to react to some event that channel is initialized and we can proceed with connect.