From 64c6a601b2fa78750db67412096d7bec006ef5ee Mon Sep 17 00:00:00 2001 From: Bastien Teinturier <31281497+t-bast@users.noreply.github.com> Date: Mon, 10 Jul 2023 11:00:49 +0200 Subject: [PATCH] Add coroutine exception handler on more socket contexts (#507) Let's see if this fixes the issue while we prepare a larger rework of the `ElectrumClient`. --- .../kotlin/fr/acinq/lightning/io/JvmTcpSocket.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/jvmMain/kotlin/fr/acinq/lightning/io/JvmTcpSocket.kt b/src/jvmMain/kotlin/fr/acinq/lightning/io/JvmTcpSocket.kt index 4efde5994..3a738eba7 100644 --- a/src/jvmMain/kotlin/fr/acinq/lightning/io/JvmTcpSocket.kt +++ b/src/jvmMain/kotlin/fr/acinq/lightning/io/JvmTcpSocket.kt @@ -160,18 +160,19 @@ class JvmTcpSocket(val socket: Socket, val loggerFactory: LoggerFactory) : TcpSo internal actual object PlatformSocketBuilder : TcpSocket.Builder { override suspend fun connect(host: String, port: Int, tls: TcpSocket.TLS, loggerFactory: LoggerFactory): TcpSocket { val logger = loggerFactory.newLogger(this::class) - return withContext(Dispatchers.IO) { + val context = tlsContext(logger) + return withContext(context) { try { - val socket = aSocket(SelectorManager(Dispatchers.IO)).tcp().connect(host, port).let { socket -> + val socket = aSocket(SelectorManager(context)).tcp().connect(host, port).let { socket -> when (tls) { - is TcpSocket.TLS.TRUSTED_CERTIFICATES -> socket.tls(tlsContext(logger)) - TcpSocket.TLS.UNSAFE_CERTIFICATES -> socket.tls(tlsContext(logger)) { + is TcpSocket.TLS.TRUSTED_CERTIFICATES -> socket.tls(context) + TcpSocket.TLS.UNSAFE_CERTIFICATES -> socket.tls(context) { logger.warning { "using unsafe TLS!" } trustManager = JvmTcpSocket.unsafeX509TrustManager() } is TcpSocket.TLS.PINNED_PUBLIC_KEY -> { logger.info { "using certificate pinning for connections with $host" } - socket.tls(tlsContext(logger), JvmTcpSocket.tlsConfigForPinnedCert(tls.pubKey, logger)) + socket.tls(context, JvmTcpSocket.tlsConfigForPinnedCert(tls.pubKey, logger)) } else -> socket }