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

Fix coroutine scoping of Redwood Treehouse UI #1932

Merged
merged 1 commit into from
Apr 3, 2024

Conversation

dellisd
Copy link
Collaborator

@dellisd dellisd commented Apr 3, 2024

Ensure that Zipline resources aren't closed until the UI's coroutine scope job completes.

This prevents a queued-up job from resuming(?) and crashing from a ZiplineException instead of gracefully handling a CancellationException when trying to collect a ZiplineService-backed flow.


  • CHANGELOG.md's "Unreleased" section has been updated, if applicable.

@@ -121,9 +130,12 @@ private class RedwoodZiplineTreehouseUi(
}

override fun close() {
coroutineScope.coroutineContext.job.invokeOnCompletion {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it

@@ -55,6 +59,11 @@ private class RedwoodZiplineTreehouseUi(
*/
override val scope = (treehouseUi as? ZiplineScoped)?.scope ?: ZiplineScope()

private val coroutineScope = CoroutineScope(
appLifecycle.coroutineScope.coroutineContext +
SupervisorJob(appLifecycle.coroutineScope.coroutineContext.job),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Doesn't this mean crashes in the composition won't correctly propagate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell the unhandled exceptions still propagate up to the appLifecycle scope's job and are handled by its exception handler--but you're right, a supervisor job is probably not correct here.

We just need some per-UI scope that can be cancelled independently of the app's overall scope.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry the "this" was meant solely to be about the SupervisorJob. The subscope makes sense 👍

@JakeWharton
Copy link
Collaborator

Also if you're fixing something please add a change log entry to accompany it.

@dellisd dellisd force-pushed the derekellis/2024-04-03/zipline-flows branch 2 times, most recently from af273db to efcb566 Compare April 3, 2024 15:26
Comment on lines 61 to 65
private val coroutineScope = CoroutineScope(
appLifecycle.coroutineScope.coroutineContext +
appLifecycle.coroutineScope.coroutineContext.job,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private val coroutineScope = CoroutineScope(
appLifecycle.coroutineScope.coroutineContext +
appLifecycle.coroutineScope.coroutineContext.job,
)
private val coroutineScope = CoroutineScope(
appLifecycle.coroutineScope.coroutineContext +
Job(appLifecycle.coroutineScope.coroutineContext.job),
)

I think you probably still want to put in a new Job which gets canceled. Or does creating a new CoroutineScope do that automatically? If so, you probably don't need the + ... part.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh woops, that's right. A new Job is only created if there's no Job in the context. This scope's job should be a child of the parent scope's job, but this is giving both scopes the same job. Will fix!

Ensure that Zipline resources aren't closed until the UI's coroutine scope job completes
@dellisd dellisd force-pushed the derekellis/2024-04-03/zipline-flows branch from efcb566 to bfc07fc Compare April 3, 2024 15:38
@dellisd dellisd merged commit 3218751 into trunk Apr 3, 2024
10 checks passed
@dellisd dellisd deleted the derekellis/2024-04-03/zipline-flows branch April 3, 2024 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants