@@ -164,23 +164,25 @@ internal actual object PlatformSocketBuilder : TcpSocket.Builder {
164
164
actual override suspend fun connect (host : String , port : Int , tls : TcpSocket .TLS , loggerFactory : LoggerFactory ): TcpSocket {
165
165
val logger = loggerFactory.newLogger(this ::class )
166
166
return withContext(Dispatchers .IO ) {
167
+ var socket: Socket ? = null
167
168
try {
168
- val socket = aSocket(SelectorManager (Dispatchers .IO )).tcp().connect(host, port).let { socket ->
169
+ socket = aSocket(SelectorManager (Dispatchers .IO )).tcp().connect(host, port).let {
169
170
when (tls) {
170
- is TcpSocket .TLS .TRUSTED_CERTIFICATES -> socket .tls(tlsContext(logger))
171
- TcpSocket .TLS .UNSAFE_CERTIFICATES -> socket .tls(tlsContext(logger)) {
171
+ is TcpSocket .TLS .TRUSTED_CERTIFICATES -> it .tls(tlsContext(logger))
172
+ TcpSocket .TLS .UNSAFE_CERTIFICATES -> it .tls(tlsContext(logger)) {
172
173
logger.warning { " using unsafe TLS!" }
173
174
trustManager = JvmTcpSocket .unsafeX509TrustManager()
174
175
}
175
176
is TcpSocket .TLS .PINNED_PUBLIC_KEY -> {
176
177
logger.info { " using certificate pinning for connections with $host " }
177
- socket .tls(tlsContext(logger), JvmTcpSocket .tlsConfigForPinnedCert(tls.pubKey, logger))
178
+ it .tls(tlsContext(logger), JvmTcpSocket .tlsConfigForPinnedCert(tls.pubKey, logger))
178
179
}
179
- else -> socket
180
+ else -> it
180
181
}
181
182
}
182
183
JvmTcpSocket (socket, loggerFactory)
183
184
} catch (e: Exception ) {
185
+ socket?.dispose()
184
186
throw when (e) {
185
187
is ConnectException -> TcpSocket .IOException .ConnectionRefused (e)
186
188
is SocketException -> TcpSocket .IOException .Unknown (e.message, e)
0 commit comments