Calling ensureActive instead of re-throwing CancellationException in suspendRunCatching

オープン
#1,876 コメント 5 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
kotlin
領域
mobile

調査の方向性

core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/SyncUtilities.kt の 58 行目付近にある suspendRunCatching から始めます。issue で説明されている coroutine のキャンセル動作を確認し、完成した動作が現在の coroutine のキャンセルと、失敗として報告されたキャンセルを区別していることを検証します。

索引モデルが issue の本文から書いたものです。

説明

In suspendRunCatching, a CancellationException gets re-thrown:

https://github.com/android/nowinandroid/blob/689ef92e41427ab70f82e2c9fe59755441deae92/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/SyncUtilities.kt#L58

However, according to this post (further acknowledged here), it's more advisable to ensureActive() instead. Maybe like this?

private suspend fun <T> suspendRunCatching(block: suspend () -> T): Result<T> = try {
    Result.success(block())
} catch (cancellationException: CancellationException) {
    currentCoroutineContext().ensureActive() // if _our_ coroutine was cancelled - respect the cancellation
    Result.failure(cancellationException) // otherwise, just treat it as a failure
} catch (exception: Exception) {
    Log.i(
        "suspendRunCatching",
        "Failed to evaluate a suspendRunCatchingBlock. Returning failure Result",
        exception,
    )
    Result.failure(exception)
}

What do you think?

主要言語
Kotlin
スター
22.1k
フォーク
4.7k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

android/nowinandroid のほかの issue

android/nowinandroid の issue をすべて見る

似ている issue

Kotlin の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。