@@ -8,6 +8,7 @@ import de.quati.pgen.wal.ReplicaIdentity.Companion.toSql
88import kotlinx.coroutines.CoroutineScope
99import kotlinx.coroutines.Dispatchers
1010import kotlinx.coroutines.Job
11+ import kotlinx.coroutines.NonCancellable
1112import kotlinx.coroutines.channels.BufferOverflow
1213import kotlinx.coroutines.flow.MutableSharedFlow
1314import kotlinx.coroutines.flow.SharedFlow
@@ -16,6 +17,7 @@ import kotlinx.coroutines.isActive
1617import kotlinx.coroutines.launch
1718import kotlinx.coroutines.sync.Mutex
1819import kotlinx.coroutines.sync.withLock
20+ import kotlinx.coroutines.withContext
1921import kotlinx.serialization.json.JsonObject
2022import org.postgresql.PGConnection
2123import org.postgresql.PGProperty
@@ -153,13 +155,19 @@ public class PgenWalEventListener private constructor(
153155 val currentJob = job ? : return @withLock
154156 currentJob.cancel()
155157 streamConnectionMutex.withLock {
156- streamConnection?.unwrap(PGConnection ::class .java)?.cancelQuery()
158+ runCatching {
159+ streamConnection?.unwrap(PGConnection ::class .java)?.cancelQuery()
160+ }
157161 }
158- currentJob.join()
159- job = null
160- streamConnectionMutex.withLock {
161- streamConnection?.close()
162- streamConnection = null
162+ withContext(NonCancellable ) {
163+ currentJob.join()
164+ job = null
165+ streamConnectionMutex.withLock {
166+ runCatching {
167+ streamConnection?.close()
168+ }
169+ streamConnection = null
170+ }
163171 }
164172 }
165173 }
0 commit comments