Skip to content

Add task cancellation - #18

Draft
asteurer wants to merge 3 commits into
bytecodealliance:mainfrom
asteurer:add-task-cancellation
Draft

asteurer wants to merge 3 commits into
bytecodealliance:mainfrom
asteurer:add-task-cancellation

Conversation

@asteurer

@asteurer asteurer commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #17.

Summary

The issue above identified a bug where a panic occurs when a future is cancelled and then written to, when it should be an error. While working on this, I also identified issues with streams permanently blocking after cancellation and added a fix for that as well.

Call outs

There were a couple of ways that we could have handled cancellation. One was to change the futures/streams API to return errors that would report whether a task was cancelled, but decided against this because it would introduce a breaking change and be more tedious for developers to use. I instead chose to use runtime.Goexit() because it seems to offer the simplest solution; however, I'm open to discussing this further.

Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com>
Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com>
Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com>
@asteurer

asteurer commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

@dicej When you get a chance, I'd appreciate another pair of eyes on this. If you'd like to try this out locally, I've drafted a PR with my changes to wit-bindgen: bytecodealliance/wit-bindgen#1779

@asteurer
asteurer marked this pull request as draft November 13, 2026 03:50

@dicej dicej left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for doing this!

Once again, I've hit the limits of my Go experience and don't know how surprising it would be to an application programmer for runtime.Goexit() to be called when e.g. calling an async import or reading/writing a stream/future.

My guess is that idiomatic support for cancellation will involve context's cancellation features, which will in turn require deeper bindings changes. What you have here seems like a reasonable first step, though.

cc @jfleitz or @dgryski, who both have way more Go experience than I do.

Comment thread wit/async/async.go
case EVENT_TASK_CANCELLED:
state.cancelled = true
for waitable, channel := range state.pending {
delete(state.pending, waitable)

@dicej dicej Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit, rather than delete the entries one-by-one, you could do state.pending = state.pending[:0] (EDIT per Damian's comment below: clear(state.pending)) after the loop, which is likely more efficient.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pending is a map; setting the length to 0 ([:0]) is only valid for slices. The efficient way to clear it is clear(state.pending) (which also works for slices, btw.). Note that if pending were a slice, pending[:0] sets the length to 0, but the entries are still referenced by the backing array and won't be garbage collected. This probably doesn't apply in this case, but good to know anyway.

@dgryski

dgryski commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Yes, wiring in context is the way to go. That will require plumbing a ctx context.Context argument as a first parameter to all functions that could potentially be cancelled. Feel free to tag me on any/all Go reviews. I might be able to pick up any coding tasks too, depending on my time.

@dicej

dicej commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@asteurer per Damian's confirmation that context is the proper way to handle cancellation, perhaps we should focus on that instead. If it's a big project, we could potentially address #17 with an ultra-minimal solution: handle EVENT_TASK_CANCELLED by simply ignoring it (e.g. with a TODO comment but no panic). The CM spec says the guest is always allowed to do that, in which case the caller just has to wait until the task returns normally. Not ideal, of course, but it would buy us more time to deliver the idiomatic solution.

What do you think?

@dicej

dicej commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

BTW, using context would also allow us to match imports calls to corresponding export calls, AKA guest task affinity.

This branch has not been deployed

No deployments
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.

Async export panics with todo when the caller cancels it

3 participants