Skip to content

Commit e060806

Browse files
committed
Håndter cancellation exceptions riktig
Hvis man ikke rethrower CancellationExceptions i courutines så kan man ende opp med såkalte rogue coroutines som suspender evig. Se Kotlin/kotlinx.coroutines#3658 for mer info
1 parent f1e2211 commit e060806

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

src/main/kotlin/no/nav/sosialhjelp/innsyn/vedlegg/KrypteringService.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package no.nav.sosialhjelp.innsyn.vedlegg
22

3+
import kotlinx.coroutines.CancellationException
34
import kotlinx.coroutines.CoroutineScope
45
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.currentCoroutineContext
7+
import kotlinx.coroutines.ensureActive
58
import kotlinx.coroutines.launch
69
import no.ks.kryptering.CMSKrypteringImpl
710
import no.nav.sosialhjelp.innsyn.utils.logger
@@ -39,6 +42,7 @@ class KrypteringServiceImpl : KrypteringService {
3942
kryptering.krypterData(pos, fileInputStream, certificate, Security.getProvider("BC"))
4043
log.debug("Ferdig med kryptering")
4144
} catch (e: Exception) {
45+
if (e is CancellationException) currentCoroutineContext().ensureActive()
4246
log.error("Det skjedde en feil ved kryptering, exception blir lagt til kryptert InputStream", e)
4347
throw IllegalStateException("An error occurred during encryption", e)
4448
}

src/main/kotlin/no/nav/sosialhjelp/innsyn/vedlegg/VedleggOpplastingService.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package no.nav.sosialhjelp.innsyn.vedlegg
22

3+
import kotlinx.coroutines.CancellationException
34
import kotlinx.coroutines.Dispatchers
45
import kotlinx.coroutines.async
56
import kotlinx.coroutines.awaitAll
67
import kotlinx.coroutines.coroutineScope
8+
import kotlinx.coroutines.currentCoroutineContext
9+
import kotlinx.coroutines.ensureActive
710
import kotlinx.coroutines.withTimeout
811
import no.nav.sbl.soknadsosialhjelp.vedlegg.JsonFiler
912
import no.nav.sbl.soknadsosialhjelp.vedlegg.JsonVedlegg
@@ -111,6 +114,7 @@ class VedleggOpplastingService(
111114
ettersendelsePdf.inputStream(),
112115
)
113116
} catch (e: Exception) {
117+
if (e is CancellationException) currentCoroutineContext().ensureActive()
114118
log.error("Generering av ettersendelse.pdf feilet.", e)
115119
throw e
116120
}

src/main/kotlin/no/nav/sosialhjelp/innsyn/vedlegg/virusscan/VirusScanner.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package no.nav.sosialhjelp.innsyn.vedlegg.virusscan
22

3+
import kotlinx.coroutines.CancellationException
34
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.currentCoroutineContext
6+
import kotlinx.coroutines.ensureActive
47
import kotlinx.coroutines.reactor.awaitSingleOrNull
58
import kotlinx.coroutines.withContext
69
import no.nav.sosialhjelp.innsyn.app.MiljoUtils.isRunningInProd
@@ -66,6 +69,7 @@ class VirusScanner(
6669
log.warn("Fant virus med status ${scanResult.result} i fil forsøkt opplastet")
6770
return true
6871
} catch (e: Exception) {
72+
if (e is CancellationException) currentCoroutineContext().ensureActive()
6973
log.warn("Kunne ikke scanne fil opplastet", e)
7074
return false
7175
}

0 commit comments

Comments
 (0)