Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScopeProvider.coroutineScope: Deliver lifecycle exceptions to CoroutineExceptionHandler instead of throwing at call-site. #632

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Jul 19, 2024

  1. Change ScopeProvider.coroutineScope throwing behavior when accessin…

    …g it before scope is active, or after scope is inactive.
    
    Instead of throwing at call site, we deliver `CoroutineScopeLifecycleException(message: String, cause: OutsideScopeException)` to the `CoroutineExceptionHandler` installed at `RibCoroutinesConfig.exceptionHandler`. Note `cause` is always non-null, and it's either `LifecycleNotStartedException` or `LifecycleEndedException`.
    
    ## Motivation
    
    Suppose you have an interactor that starts subscribing to an Rx stream asynchronously.
    
    ```kotlin
    class MyInteractor {
      override fun didBecomeActive() {
        runLater {
          someObservable.autoDispose(this).subscribe()
        }
      }
    }
    ```
    
    It is possible that this Rx subscription will be triggered after interactor's already inactive. In that case, subscription will be No-Op.
    
    This is not good code for a major reason: `Interactor` is attempting to do work after it's inactive. In other words, `runLater` does not respect the interactor lifecycle. Still, considering this is some legacy code being migrated from Rx to Coroutines, the new code would look like the following.
    
    ```kotlin
    class MyInteractor {
      override fun didBecomeActive() {
        runLater {
          someObservable.autoDispose(coroutineScope).subscribe()
        }
      }
    }
    ```
    
    Unlike the Rx counterpart, this code will sometimes fatally throw. If `ScopeProvider.coroutineScope` is called after the scope's inactive, it will throw `LifecycleEndedException` at call site.
    
    Calling `coroutineScope` outside of lifecycle bounds is always erroneous code. But in order to favour a smoother migration to Coroutines, and to avoid surprises of `val` throwing exceptions, we are changing current implementation to deliver the exception to the `CoroutineExceptionHandler` instead of throwing at call site.
    
    If some app decides to override the default behavior of crashing (in JVM/Android) in face of this erroneous code, one can customize `RibCoroutinesConfig.exceptionHandler` at app startup:
    
    ```kotlin
    RibCoroutinesConfig.exceptionHandler = CoroutineExceptionHandler { _, throwable ->
      when (throwable) {
        is CoroutineScopeLifecycleException -> log(throwable) // only log, don't re-throw.
        else -> throw throwable
      }
    }
    ```
    psteiger committed Jul 19, 2024
    Configuration menu
    Copy the full SHA
    b397f5f View commit details
    Browse the repository at this point in the history