Skip to content

Commit 7257283

Browse files
authored
fix: correctly close HTTP-related CRT resources in native sourceset (#1345)
1 parent 7890bec commit 7257283

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

.github/workflows/artifact-size-metrics.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ jobs:
4545
steps:
4646
- name: Checkout Sources
4747
uses: actions/checkout@v4
48+
with:
49+
path: smithy-kotlin
4850
- name: Setup build
49-
uses: .github/actions/setup-build
51+
uses: ./smithy-kotlin/.github/actions/setup-build
5052
- name: Configure JDK
5153
uses: actions/setup-java@v3
5254
with:
@@ -60,16 +62,21 @@ jobs:
6062
aws-region: us-west-2
6163
- name: Configure Gradle
6264
uses: awslabs/aws-kotlin-repo-tools/.github/actions/configure-gradle@main
65+
with:
66+
working-directory: smithy-kotlin
6367
- name: Generate Artifact Size Metrics
6468
run: ./gradlew -Paws.kotlin.native=false artifactSizeMetrics
69+
working-directory: smithy-kotlin
6570
- name: Analyze Artifact Size Metrics
6671
run: ./gradlew analyzeArtifactSizeMetrics
67-
72+
working-directory: smithy-kotlin
6873
- name: Show Results
6974
uses: awslabs/aws-kotlin-repo-tools/.github/actions/artifact-size-metrics/show-results@main
70-
75+
with:
76+
working-directory: smithy-kotlin
7177
- name: Evaluate
7278
if: ${{ !contains(github.event.pull_request.labels.*.name, 'acknowledge-artifact-size-increase') }}
79+
working-directory: smithy-kotlin
7380
run: |
7481
cd build/reports/metrics
7582
cat has-significant-change.txt | grep false || {

runtime/crt-util/jvmAndNative/src/aws/smithy/kotlin/runtime/crt/SdkDefaultIO.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,39 @@ private const val DEFAULT_EVENT_LOOP_THREAD_COUNT: Int = 1
1414
/**
1515
* Default (CRT) IO used by the SDK when not configured manually/directly
1616
*/
17+
@Deprecated("This API is no longer used by the SDK and will be removed in version 1.6.x")
1718
@InternalApi
1819
public object SdkDefaultIO {
1920
/**
2021
* The default event loop group to run IO on
2122
*/
23+
@Deprecated("This API is no longer used by the SDK and will be removed in version 1.6.x")
2224
public val EventLoop: EventLoopGroup by lazy {
23-
// TODO - can we register shutdown in appropriate runtimes (e.g. jvm: addShutdown, native: atexit(), etc) when/if these lazy block(s) run?
2425
EventLoopGroup(DEFAULT_EVENT_LOOP_THREAD_COUNT)
2526
}
2627

2728
/**
2829
* The default host resolver to resolve DNS queries with
2930
*/
31+
@Deprecated("This API is no longer used by the SDK and will be removed in version 1.6.x")
3032
public val HostResolver: HostResolver by lazy {
33+
@Suppress("DEPRECATION")
3134
HostResolver(EventLoop)
3235
}
3336

3437
/**
3538
* The default client bootstrap
3639
*/
40+
@Deprecated("This API is no longer used by the SDK and will be removed in version 1.6.x")
3741
public val ClientBootstrap: ClientBootstrap by lazy {
42+
@Suppress("DEPRECATION")
3843
ClientBootstrap(EventLoop, HostResolver)
3944
}
4045

4146
/**
4247
* The default TLS context
4348
*/
49+
@Deprecated("This API is no longer used by the SDK and will be removed in version 1.6.x")
4450
public val TlsContext: TlsContext by lazy {
4551
TlsContext(TlsContextOptions.defaultClient())
4652
}

runtime/protocol/http-client-engines/http-client-engine-crt/jvmAndNative/src/aws/smithy/kotlin/runtime/http/engine/crt/ConnectionManager.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
package aws.smithy.kotlin.runtime.http.engine.crt
66

77
import aws.sdk.kotlin.crt.http.*
8+
import aws.sdk.kotlin.crt.io.ClientBootstrap
89
import aws.sdk.kotlin.crt.io.SocketOptions
910
import aws.sdk.kotlin.crt.io.TlsContext
1011
import aws.sdk.kotlin.crt.io.TlsContextOptionsBuilder
1112
import aws.sdk.kotlin.crt.io.Uri
12-
import aws.smithy.kotlin.runtime.crt.SdkDefaultIO
1313
import aws.smithy.kotlin.runtime.http.HttpErrorCode
1414
import aws.smithy.kotlin.runtime.http.HttpException
1515
import aws.smithy.kotlin.runtime.http.engine.ProxyConfig
@@ -39,8 +39,11 @@ internal class ConnectionManager(
3939
.build()
4040
.let(::CrtTlsContext)
4141

42+
private val manageClientBootstrap = config.clientBootstrap == null
43+
private val clientBootstrap = config.clientBootstrap ?: ClientBootstrap()
44+
4245
private val options = HttpClientConnectionManagerOptionsBuilder().apply {
43-
clientBootstrap = config.clientBootstrap ?: SdkDefaultIO.ClientBootstrap
46+
clientBootstrap = this@ConnectionManager.clientBootstrap
4447
tlsContext = crtTlsContext
4548
manualWindowManagement = true
4649
socketOptions = SocketOptions(
@@ -55,7 +58,7 @@ internal class ConnectionManager(
5558
private val connManagers = mutableMapOf<String, HttpClientConnectionManager>()
5659
private val mutex = Mutex()
5760

58-
public suspend fun acquire(request: HttpRequest): HttpClientConnection {
61+
suspend fun acquire(request: HttpRequest): HttpClientConnection {
5962
val proxyConfig = config.proxySelector.select(request.url)
6063

6164
val manager = getManagerForUri(request.uri, proxyConfig)
@@ -88,6 +91,7 @@ internal class ConnectionManager(
8891
throw httpEx
8992
}
9093
}
94+
9195
private suspend fun getManagerForUri(uri: Uri, proxyConfig: ProxyConfig): HttpClientConnectionManager = mutex.withLock {
9296
connManagers.getOrPut(uri.authority) {
9397
val connOpts = options.apply {
@@ -109,9 +113,12 @@ internal class ConnectionManager(
109113
HttpClientConnectionManager(connOpts)
110114
}
111115
}
116+
112117
override fun close() {
113118
connManagers.forEach { entry -> entry.value.close() }
114119
crtTlsContext.close()
120+
121+
if (manageClientBootstrap) clientBootstrap.close()
115122
}
116123

117124
private inner class LeasedConnection(private val delegate: HttpClientConnection) : HttpClientConnection by delegate {
@@ -127,7 +134,7 @@ internal class ConnectionManager(
127134
}
128135

129136
private fun toCrtTlsVersion(sdkTlsVersion: SdkTlsVersion?): CrtTlsVersion = when (sdkTlsVersion) {
130-
null -> aws.sdk.kotlin.crt.io.TlsVersion.SYS_DEFAULT
137+
null -> CrtTlsVersion.SYS_DEFAULT
131138
TlsVersion.TLS_1_0 -> CrtTlsVersion.TLSv1
132139
TlsVersion.TLS_1_1 -> CrtTlsVersion.TLS_V1_1
133140
TlsVersion.TLS_1_2 -> CrtTlsVersion.TLS_V1_2

runtime/protocol/http-client-engines/http-client-engine-crt/jvmAndNative/src/aws/smithy/kotlin/runtime/http/engine/crt/CrtHttpEngine.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import aws.smithy.kotlin.runtime.io.internal.SdkDispatchers
1515
import aws.smithy.kotlin.runtime.operation.ExecutionContext
1616
import aws.smithy.kotlin.runtime.telemetry.logging.logger
1717
import aws.smithy.kotlin.runtime.time.Instant
18-
import kotlinx.coroutines.*
18+
import kotlinx.coroutines.job
1919
import kotlinx.coroutines.sync.Semaphore
2020
import kotlinx.coroutines.sync.withPermit
21+
import kotlinx.coroutines.withContext
2122

2223
internal const val DEFAULT_WINDOW_SIZE_BYTES: Int = 16 * 1024
2324
internal const val CHUNK_BUFFER_SIZE: Long = 64 * 1024

runtime/protocol/http-client-engines/http-client-engine-default/native/src/aws/smithy/kotlin/runtime/http/engine/DefaultHttpEngineNative.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ package aws.smithy.kotlin.runtime.http.engine
77

88
import aws.smithy.kotlin.runtime.http.engine.crt.CrtHttpEngine
99

10-
internal actual fun newDefaultHttpEngine(block: (HttpClientEngineConfig.Builder.() -> Unit)?): CloseableHttpClientEngine = CrtHttpEngine()
10+
internal actual fun newDefaultHttpEngine(
11+
block: (HttpClientEngineConfig.Builder.() -> Unit)?,
12+
): CloseableHttpClientEngine = when (block) {
13+
null -> CrtHttpEngine()
14+
else -> CrtHttpEngine(block)
15+
}

0 commit comments

Comments
 (0)