From 7e748bd7dcfe271e5c6bb1b908fd3b15d421c182 Mon Sep 17 00:00:00 2001 From: Robinson Date: Fri, 16 Jun 2023 14:48:19 +0200 Subject: [PATCH] Reference copies of atomic lists when calling them (in case the list is cleared in a reentrant call --- src/dorkbox/network/connection/ListenerManager.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/dorkbox/network/connection/ListenerManager.kt b/src/dorkbox/network/connection/ListenerManager.kt index 2ac8a769..0ede0f79 100644 --- a/src/dorkbox/network/connection/ListenerManager.kt +++ b/src/dorkbox/network/connection/ListenerManager.kt @@ -367,7 +367,8 @@ internal class ListenerManager(private val logger: KLogg * Invoked when a connection is first initialized, but BEFORE it's connected to the remote address. */ suspend fun notifyInit(connection: CONNECTION) { - onInitList.value.forEach { + val list = onInitList.value + list.forEach { try { it(connection) } catch (t: Throwable) { @@ -382,7 +383,8 @@ internal class ListenerManager(private val logger: KLogg * Invoked when a connection is connected to a remote address. */ suspend fun notifyConnect(connection: CONNECTION) { - onConnectList.value.forEach { + val list = onConnectList.value + list.forEach { try { it(connection) } catch (t: Throwable) { @@ -397,7 +399,8 @@ internal class ListenerManager(private val logger: KLogg * Invoked when a connection is disconnected to a remote address. */ suspend fun notifyDisconnect(connection: CONNECTION) { - onDisconnectList.value.forEach { + val list = onDisconnectList.value + list.forEach { try { it(connection) } catch (t: Throwable) { @@ -416,7 +419,8 @@ internal class ListenerManager(private val logger: KLogg fun notifyError(connection: CONNECTION, exception: Throwable) { logger.error("Error with connection $connection", exception) - onErrorList.value.forEach { + val list = onErrorList.value + list.forEach { try { it(connection, exception) } catch (t: Throwable) { @@ -435,7 +439,8 @@ internal class ListenerManager(private val logger: KLogg fun notifyError(exception: Throwable) { logger.error("Global error", exception) - onErrorGlobalList.value.forEach { + val list = onErrorGlobalList.value + list.forEach { try { it(exception) } catch (t: Throwable) {