From 918318252ebbec74e1d824351df4a395de2f2762 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 24 Jan 2023 08:03:51 +0200 Subject: [PATCH] Fix coroutine cancellation handling in runCatching --- .../kotatsu/parsers/site/NetTruyenParser.kt | 7 +++++-- .../kotatsu/parsers/site/grouple/GroupleParser.kt | 2 +- .../org/koitharu/kotatsu/parsers/util/Result.kt | 13 +++++++++++++ .../koitharu/kotatsu/parsers/AuthCheckExtension.kt | 3 ++- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NetTruyenParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NetTruyenParser.kt index 29171eeb..afe8664f 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NetTruyenParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/NetTruyenParser.kt @@ -4,7 +4,9 @@ import androidx.collection.ArrayMap import androidx.collection.ArraySet import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import org.koitharu.kotatsu.parsers.* +import org.koitharu.kotatsu.parsers.MangaLoaderContext +import org.koitharu.kotatsu.parsers.MangaSourceParser +import org.koitharu.kotatsu.parsers.PagedMangaParser import org.koitharu.kotatsu.parsers.config.ConfigKey import org.koitharu.kotatsu.parsers.exception.NotFoundException import org.koitharu.kotatsu.parsers.model.* @@ -89,6 +91,7 @@ class NetTruyenParser(override val context: MangaLoaderContext) : val currentYear = calendar.get(Calendar.YEAR).toString().takeLast(2) "$relativeDate/$currentYear" } + 3 -> relativeDate else -> return 0L } @@ -124,7 +127,7 @@ class NetTruyenParser(override val context: MangaLoaderContext) : } val response = if (isSearching) { - val result = runCatching { context.httpGet(url) } + val result = runCatchingCancellable { context.httpGet(url) } val exception = result.exceptionOrNull() if (exception is NotFoundException) { return emptyList() diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt index 370944c4..006aac28 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/grouple/GroupleParser.kt @@ -306,7 +306,7 @@ internal abstract class GroupleParser( return context.httpPost(url, payload, headers) } - private suspend fun tryHead(url: String, headers: Headers): Boolean = runCatching { + private suspend fun tryHead(url: String, headers: Headers): Boolean = runCatchingCancellable { context.httpHead(url, headers).isSuccessful }.getOrDefault(false) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt new file mode 100644 index 00000000..17732922 --- /dev/null +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Result.kt @@ -0,0 +1,13 @@ +package org.koitharu.kotatsu.parsers.util + +import kotlinx.coroutines.CancellationException + +inline fun T.runCatchingCancellable(block: T.() -> R): Result { + return try { + Result.success(block()) + } catch (e: CancellationException) { + throw e + } catch (e: Throwable) { + Result.failure(e) + } +} \ No newline at end of file diff --git a/src/test/kotlin/org/koitharu/kotatsu/parsers/AuthCheckExtension.kt b/src/test/kotlin/org/koitharu/kotatsu/parsers/AuthCheckExtension.kt index d058e68e..0dfbe83f 100644 --- a/src/test/kotlin/org/koitharu/kotatsu/parsers/AuthCheckExtension.kt +++ b/src/test/kotlin/org/koitharu/kotatsu/parsers/AuthCheckExtension.kt @@ -4,6 +4,7 @@ import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.extension.BeforeAllCallback import org.junit.jupiter.api.extension.ExtensionContext import org.koitharu.kotatsu.parsers.model.MangaSource +import org.koitharu.kotatsu.parsers.util.runCatchingCancellable class AuthCheckExtension : BeforeAllCallback { @@ -22,7 +23,7 @@ class AuthCheckExtension : BeforeAllCallback { } private fun checkAuthorization(source: MangaSource, parser: MangaParserAuthProvider) = runTest { - runCatching { + runCatchingCancellable { parser.getUsername() }.onSuccess { username -> println("Signed in to ${source.name} as $username")