Skip to content

Allow isolating tinyio Loops#21

Open
aburgm wants to merge 5 commits intopatrick-kidger:devfrom
aburgm:armin/isolate
Open

Allow isolating tinyio Loops#21
aburgm wants to merge 5 commits intopatrick-kidger:devfrom
aburgm:armin/isolate

Conversation

@aburgm
Copy link

@aburgm aburgm commented Feb 16, 2026

So that an exception in an isolated group of coroutines does not propagate to "outer" coroutines.

This is a follow-up of #20 against the dev branch.

This allows to isolate them from each other so that an exception in a
nested group does not affect coroutines running outside.
Copy link
Owner

@patrick-kidger patrick-kidger left a comment

Choose a reason for hiding this comment

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

Nit aside LGTM!

Do you want to try this in your downstream use-case + confirm that it works there, before we merge it here? Then we can be sure that this isn't missing some detail.

Actually also, I think I've thought of an even neater API:

try:
    return (yield from _nest(...)), True
except BaseException as e:
    return e, False

and then the consuming code can do more complicated logic on the exception. Would that be preferable do you think?

@aburgm
Copy link
Author

aburgm commented Feb 27, 2026

Nit aside LGTM!

Do you want to try this in your downstream use-case + confirm that it works there, before we merge it here? Then we can be sure that this isn't missing some detail.

Sorry for the delay in getting back on this. I fixed the nit and added the exception_group parameter to isolate. Also can confirm it seems to work for me downstream.

Actually also, I think I've thought of an even neater API:

try:
    return (yield from _nest(...)), True
except BaseException as e:
    return e, False

and then the consuming code can do more complicated logic on the exception. Would that be preferable do you think?

So I'm guessing we can't just let the exception propagate up. I do like that the current approach kind of forces/encourages me to return something in case of an exception... I also don't think there is any limitation with the current logic, if I have def cleanup(e): yield; return e I get essentially the same behavior as with your suggestion, no? That said, no strong opinion really. Happy to change it if you think it's neater.

Copy link
Owner

@patrick-kidger patrick-kidger left a comment

Choose a reason for hiding this comment

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

So I've tried writing out a couple of test-cases and as a result, I do think the approach I have in #21 (review) is slightly neater overall. So let's go with that!

(In particular this is better for try/finally patterns, when we want to perform some cleanup regardless of success or failure, and this way we don't need to pipe the same logic into two different locations: after the isolate, and inside the cleanup.)

# This `yield from` is load bearing! We must not allow the tinyio event loop to
# interpose itself between the exception arising out of `fn(*news)`, and the
# current stack frame. Otherwise we would get a `CancelledError` here instead.
return (yield from _nest(fn(*news), exception_group=exception_group)), True
Copy link
Owner

Choose a reason for hiding this comment

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

I don't understand the exception_group parameter. I think the caller is already free to provide any other kwargs they like, just by closing over them in fn. (e.g. functools.partial). Why special-case this one?

Copy link
Author

Choose a reason for hiding this comment

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

This is a parameter passed to _nest (and ultimately to tinyio.Loop().runtime()). It's not passed to fn. So unless I'm missing something closing over them in fn doesn't achieve that.

@aburgm
Copy link
Author

aburgm commented Mar 1, 2026

So I've tried writing out a couple of test-cases and as a result, I do think the approach I have in #21 (review) is slightly neater overall. So let's go with that!

I think you are right, it does simplify things a bit as well. Changed.

@aburgm aburgm requested a review from patrick-kidger March 4, 2026 02:31
@patrick-kidger patrick-kidger mentioned this pull request Mar 4, 2026
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.

2 participants