Skip to content

Commit 720dbf0

Browse files
authored
Prevent conn leak on thread interrupt (#6835)
* Prevent conn leak on thread interrupt If a thread blocked waiting on a connection pool is interrupted, it's possible for the connection to become leaked if the pool then completes the future from its side. This commit prevents that from happening by intercepting the interrupt and attempting to cancel the lease future. If that is unsuccessful, the connection is returned to the upper, and the thread is reinterrupted. Adresses #6786 * Review comments * Checkstyle fix
1 parent 14a717c commit 720dbf0

File tree

8 files changed

+789
-3
lines changed

8 files changed

+789
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Apache 5 HTTP Client",
4+
"contributor": "",
5+
"description": "Fixed a connection leak that could occur when the thread waiting to acquire a connection from the pool is interrupted. Fixes [#6786](https://github.com/aws/aws-sdk-java-v2/issues/6786)."
6+
}

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/Apache5HttpClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
5151
import org.apache.hc.client5.http.impl.classic.HttpClients;
5252
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
53-
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
5453
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner;
5554
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
5655
import org.apache.hc.client5.http.protocol.HttpClientContext;
@@ -92,6 +91,7 @@
9291
import software.amazon.awssdk.http.apache5.internal.SdkProxyRoutePlanner;
9392
import software.amazon.awssdk.http.apache5.internal.conn.ClientConnectionManagerFactory;
9493
import software.amazon.awssdk.http.apache5.internal.conn.IdleConnectionReaper;
94+
import software.amazon.awssdk.http.apache5.internal.conn.SafePoolingHttpClientConnectionManagerBuilder;
9595
import software.amazon.awssdk.http.apache5.internal.conn.SdkConnectionKeepAliveStrategy;
9696
import software.amazon.awssdk.http.apache5.internal.conn.SdkTlsSocketFactory;
9797
import software.amazon.awssdk.http.apache5.internal.impl.Apache5HttpRequestFactory;
@@ -755,8 +755,8 @@ public PoolingHttpClientConnectionManager create(Apache5HttpClient.DefaultBuilde
755755

756756
TlsSocketStrategy tlsStrategy = getPreferredTlsStrategy(configuration, standardOptions);
757757

758-
PoolingHttpClientConnectionManagerBuilder builder =
759-
PoolingHttpClientConnectionManagerBuilder.create()
758+
SafePoolingHttpClientConnectionManagerBuilder builder =
759+
SafePoolingHttpClientConnectionManagerBuilder.create()
760760
.setTlsSocketStrategy(tlsStrategy)
761761
.setSchemePortResolver(DefaultSchemePortResolver.INSTANCE)
762762
.setDnsResolver(configuration.dnsResolver);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache5.internal.conn;
17+
18+
import org.apache.hc.client5.http.impl.io.ManagedHttpClientConnectionFactory;
19+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
20+
import org.apache.hc.client5.http.io.HttpClientConnectionOperator;
21+
import org.apache.hc.core5.pool.DefaultDisposalCallback;
22+
import org.apache.hc.core5.pool.PoolReusePolicy;
23+
import software.amazon.awssdk.annotations.SdkInternalApi;
24+
25+
/**
26+
* Specialization of {@link PoolingHttpClientConnectionManager} to enable use of {@link SafeStrictConnPool} to prevent leaking
27+
* connections when the thread waiting on the future is interrupted.
28+
*/
29+
@SdkInternalApi
30+
public final class SafePoolingHttpClientConnectionManager extends PoolingHttpClientConnectionManager {
31+
public SafePoolingHttpClientConnectionManager(HttpClientConnectionOperator connectionOperator) {
32+
super(connectionOperator,
33+
new SafeStrictConnPool(
34+
DEFAULT_MAX_CONNECTIONS_PER_ROUTE,
35+
DEFAULT_MAX_TOTAL_CONNECTIONS,
36+
null,
37+
PoolReusePolicy.LIFO,
38+
new DefaultDisposalCallback<>(),
39+
null
40+
),
41+
ManagedHttpClientConnectionFactory.INSTANCE
42+
);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
/*
17+
* ====================================================================
18+
* Licensed to the Apache Software Foundation (ASF) under one
19+
* or more contributor license agreements. See the NOTICE file
20+
* distributed with this work for additional information
21+
* regarding copyright ownership. The ASF licenses this file
22+
* to you under the Apache License, Version 2.0 (the
23+
* "License"); you may not use this file except in compliance
24+
* with the License. You may obtain a copy of the License at
25+
*
26+
* http://www.apache.org/licenses/LICENSE-2.0
27+
*
28+
* Unless required by applicable law or agreed to in writing,
29+
* software distributed under the License is distributed on an
30+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
31+
* KIND, either express or implied. See the License for the
32+
* specific language governing permissions and limitations
33+
* under the License.
34+
* ====================================================================
35+
*
36+
* This software consists of voluntary contributions made by many
37+
* individuals on behalf of the Apache Software Foundation. For more
38+
* information on the Apache Software Foundation, please see
39+
* <http://www.apache.org/>.
40+
*
41+
*/
42+
43+
package software.amazon.awssdk.http.apache5.internal.conn;
44+
45+
import org.apache.hc.client5.http.DnsResolver;
46+
import org.apache.hc.client5.http.HttpRoute;
47+
import org.apache.hc.client5.http.SchemePortResolver;
48+
import org.apache.hc.client5.http.config.ConnectionConfig;
49+
import org.apache.hc.client5.http.config.TlsConfig;
50+
import org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator;
51+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
52+
import org.apache.hc.client5.http.io.HttpClientConnectionOperator;
53+
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
54+
import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
55+
import org.apache.hc.core5.function.Resolver;
56+
import org.apache.hc.core5.http.HttpHost;
57+
import org.apache.hc.core5.http.URIScheme;
58+
import org.apache.hc.core5.http.config.RegistryBuilder;
59+
import org.apache.hc.core5.http.io.SocketConfig;
60+
import software.amazon.awssdk.annotations.SdkInternalApi;
61+
62+
/**
63+
* This is a fork of {@link PoolingHttpClientConnectionManagerBuilder} from Apache 5. The purpose of this forked class is to
64+
* enable usage of the {@link SafePoolingHttpClientConnectionManager} to enable the workaround for
65+
* https://github.com/aws/aws-sdk-java-v2/issues/6786.
66+
*/
67+
// This a direct copy of PoolingHttpClientConnectionManagerBuilder with minor changes to remove methods we don't use and
68+
// updates to follow our style guide.
69+
@SdkInternalApi
70+
public final class SafePoolingHttpClientConnectionManagerBuilder {
71+
72+
private TlsSocketStrategy tlsSocketStrategy;
73+
private SchemePortResolver schemePortResolver;
74+
private DnsResolver dnsResolver;
75+
private Resolver<HttpRoute, SocketConfig> socketConfigResolver;
76+
private Resolver<HttpRoute, ConnectionConfig> connectionConfigResolver;
77+
private Resolver<HttpHost, TlsConfig> tlsConfigResolver;
78+
79+
private int maxConnTotal;
80+
private int maxConnPerRoute;
81+
82+
public static SafePoolingHttpClientConnectionManagerBuilder create() {
83+
return new SafePoolingHttpClientConnectionManagerBuilder();
84+
}
85+
86+
/**
87+
* Sets {@link TlsSocketStrategy} instance.
88+
*
89+
* @return this instance.
90+
*/
91+
public SafePoolingHttpClientConnectionManagerBuilder setTlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy) {
92+
this.tlsSocketStrategy = tlsSocketStrategy;
93+
return this;
94+
}
95+
96+
/**
97+
* Sets {@link DnsResolver} instance.
98+
*
99+
* @return this instance.
100+
*/
101+
public SafePoolingHttpClientConnectionManagerBuilder setDnsResolver(DnsResolver dnsResolver) {
102+
this.dnsResolver = dnsResolver;
103+
return this;
104+
}
105+
106+
/**
107+
* Sets {@link SchemePortResolver} instance.
108+
*
109+
* @return this instance.
110+
*/
111+
public SafePoolingHttpClientConnectionManagerBuilder setSchemePortResolver(SchemePortResolver schemePortResolver) {
112+
this.schemePortResolver = schemePortResolver;
113+
return this;
114+
}
115+
116+
/**
117+
* Sets maximum total connection value.
118+
*
119+
* @return this instance.
120+
*/
121+
public SafePoolingHttpClientConnectionManagerBuilder setMaxConnTotal(int maxConnTotal) {
122+
this.maxConnTotal = maxConnTotal;
123+
return this;
124+
}
125+
126+
/**
127+
* Sets maximum connection per route value.
128+
*
129+
* @return this instance.
130+
*/
131+
public SafePoolingHttpClientConnectionManagerBuilder setMaxConnPerRoute(int maxConnPerRoute) {
132+
this.maxConnPerRoute = maxConnPerRoute;
133+
return this;
134+
}
135+
136+
/**
137+
* Sets the same {@link SocketConfig} for all routes.
138+
*
139+
* @return this instance.
140+
*/
141+
public SafePoolingHttpClientConnectionManagerBuilder setDefaultSocketConfig(SocketConfig config) {
142+
this.socketConfigResolver = route -> config;
143+
return this;
144+
}
145+
146+
/**
147+
* Sets {@link Resolver} of {@link SocketConfig} on a per route basis.
148+
*
149+
* @return this instance.
150+
* @since 5.2
151+
*/
152+
public SafePoolingHttpClientConnectionManagerBuilder setSocketConfigResolver(
153+
Resolver<HttpRoute, SocketConfig> socketConfigResolver) {
154+
this.socketConfigResolver = socketConfigResolver;
155+
return this;
156+
}
157+
158+
/**
159+
* Sets the same {@link ConnectionConfig} for all routes.
160+
*
161+
* @return this instance.
162+
* @since 5.2
163+
*/
164+
public SafePoolingHttpClientConnectionManagerBuilder setDefaultConnectionConfig(ConnectionConfig config) {
165+
this.connectionConfigResolver = route -> config;
166+
return this;
167+
}
168+
169+
/**
170+
* Sets {@link Resolver} of {@link ConnectionConfig} on a per route basis.
171+
*
172+
* @return this instance.
173+
* @since 5.2
174+
*/
175+
public SafePoolingHttpClientConnectionManagerBuilder setConnectionConfigResolver(
176+
Resolver<HttpRoute, ConnectionConfig> connectionConfigResolver) {
177+
this.connectionConfigResolver = connectionConfigResolver;
178+
return this;
179+
}
180+
181+
/**
182+
* Sets the same {@link TlsConfig} for all hosts.
183+
*
184+
* @return this instance.
185+
* @since 5.2
186+
*/
187+
public SafePoolingHttpClientConnectionManagerBuilder setDefaultTlsConfig(TlsConfig config) {
188+
this.tlsConfigResolver = host -> config;
189+
return this;
190+
}
191+
192+
/**
193+
* Sets {@link Resolver} of {@link TlsConfig} on a per host basis.
194+
*
195+
* @return this instance.
196+
* @since 5.2
197+
*/
198+
public SafePoolingHttpClientConnectionManagerBuilder setTlsConfigResolver(
199+
Resolver<HttpHost, TlsConfig> tlsConfigResolver) {
200+
this.tlsConfigResolver = tlsConfigResolver;
201+
return this;
202+
}
203+
204+
protected HttpClientConnectionOperator createConnectionOperator(SchemePortResolver schemePortResolver,
205+
DnsResolver dnsResolver,
206+
TlsSocketStrategy tlsSocketStrategy) {
207+
return new DefaultHttpClientConnectionOperator(schemePortResolver, dnsResolver,
208+
RegistryBuilder.<TlsSocketStrategy>create()
209+
.register(URIScheme.HTTPS.id, tlsSocketStrategy)
210+
.build());
211+
}
212+
213+
public SafePoolingHttpClientConnectionManager build() {
214+
TlsSocketStrategy tlsSocketStrategyCopy;
215+
if (tlsSocketStrategy != null) {
216+
tlsSocketStrategyCopy = tlsSocketStrategy;
217+
} else {
218+
tlsSocketStrategyCopy = DefaultClientTlsStrategy.createDefault();
219+
}
220+
221+
SafePoolingHttpClientConnectionManager poolingmgr = new SafePoolingHttpClientConnectionManager(
222+
createConnectionOperator(schemePortResolver, dnsResolver, tlsSocketStrategyCopy));
223+
poolingmgr.setSocketConfigResolver(socketConfigResolver);
224+
poolingmgr.setConnectionConfigResolver(connectionConfigResolver);
225+
poolingmgr.setTlsConfigResolver(tlsConfigResolver);
226+
if (maxConnTotal > 0) {
227+
poolingmgr.setMaxTotal(maxConnTotal);
228+
}
229+
if (maxConnPerRoute > 0) {
230+
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
231+
}
232+
return poolingmgr;
233+
}
234+
235+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache5.internal.conn;
17+
18+
import java.util.concurrent.Future;
19+
import org.apache.hc.client5.http.HttpRoute;
20+
import org.apache.hc.client5.http.io.ManagedHttpClientConnection;
21+
import org.apache.hc.core5.concurrent.FutureCallback;
22+
import org.apache.hc.core5.pool.ConnPoolListener;
23+
import org.apache.hc.core5.pool.DisposalCallback;
24+
import org.apache.hc.core5.pool.PoolEntry;
25+
import org.apache.hc.core5.pool.PoolReusePolicy;
26+
import org.apache.hc.core5.pool.StrictConnPool;
27+
import org.apache.hc.core5.util.TimeValue;
28+
import org.apache.hc.core5.util.Timeout;
29+
import software.amazon.awssdk.annotations.SdkInternalApi;
30+
import software.amazon.awssdk.http.apache5.internal.utils.CancelOnInterruptWrapper;
31+
32+
/**
33+
* Specialization of {@link StrictConnPool} that prevents leaking the connection when thread waiting on the future is
34+
* interrupted.
35+
*/
36+
@SdkInternalApi
37+
public final class SafeStrictConnPool extends StrictConnPool<HttpRoute, ManagedHttpClientConnection> {
38+
public SafeStrictConnPool(int defaultMaxPerRoute,
39+
int maxTotal,
40+
TimeValue timeToLive,
41+
PoolReusePolicy policy,
42+
DisposalCallback<ManagedHttpClientConnection> disposalCallback,
43+
ConnPoolListener<HttpRoute> connPoolListener) {
44+
super(defaultMaxPerRoute, maxTotal, timeToLive, policy, disposalCallback, connPoolListener);
45+
}
46+
47+
public Future<PoolEntry<HttpRoute, ManagedHttpClientConnection>> lease(HttpRoute route,
48+
Object state,
49+
Timeout requestTimeout,
50+
FutureCallback<PoolEntry<HttpRoute,
51+
ManagedHttpClientConnection>> callback) {
52+
return safeLease(super.lease(route, state, requestTimeout, callback));
53+
}
54+
55+
private Future<PoolEntry<HttpRoute, ManagedHttpClientConnection>> safeLease(
56+
Future<PoolEntry<HttpRoute, ManagedHttpClientConnection>> leaseFuture) {
57+
return new CancelOnInterruptWrapper<>(leaseFuture);
58+
}
59+
}

0 commit comments

Comments
 (0)