@@ -173,7 +173,7 @@ class ElectrumClient(
173173 private val json = Json { ignoreUnknownKeys = true }
174174
175175 private val eventChannel: Channel <ClientEvent > = Channel (BUFFERED )
176- private val output: Channel <ByteArray > = Channel (BUFFERED )
176+ private var output: Channel <ByteArray > = Channel (BUFFERED )
177177
178178 private val _connectionState = MutableStateFlow (Connection .CLOSED )
179179 val connectionState: StateFlow <Connection > get() = _connectionState
@@ -208,7 +208,7 @@ class ElectrumClient(
208208 actions.forEach { action ->
209209 yield ()
210210 when (action) {
211- is SendRequest -> output.send(action.request.encodeToByteArray())
211+ is SendRequest -> if ( ! output.isClosedForSend) output.send(action.request.encodeToByteArray())
212212 is SendHeader -> notificationsChannel.send(HeaderSubscriptionResponse (action.height, action.blockHeader))
213213 is SendResponse -> notificationsChannel.send(action.response)
214214 is BroadcastStatus -> if (_connectionState .value != action.connection) _connectionState .value = action.connection
@@ -224,6 +224,7 @@ class ElectrumClient(
224224
225225 fun disconnect () {
226226 if (this ::socket.isInitialized) socket.close()
227+ output.close()
227228 }
228229
229230 // Warning : lateinit vars have to be used AFTER their init to avoid any crashes
@@ -271,6 +272,8 @@ class ElectrumClient(
271272 }
272273
273274 suspend fun respond () {
275+ // Reset the output channel to avoid sending obsolete messages
276+ output = Channel (BUFFERED )
274277 for (msg in output) { send(msg) }
275278 }
276279
0 commit comments