Skip to content

[C#] Add cancel support for futures and streams#1580

Merged
dicej merged 3 commits intobytecodealliance:mainfrom
yowl:csharp-cancel-read
Apr 7, 2026
Merged

[C#] Add cancel support for futures and streams#1580
dicej merged 3 commits intobytecodealliance:mainfrom
yowl:csharp-cancel-read

Conversation

@yowl
Copy link
Copy Markdown
Collaborator

@yowl yowl commented Apr 4, 2026

This PR adds some support for canceling futures and streams..While dotnet Task are cancelable via tokens, that is not the design we want for components, so in this PR there is introduced a ComponentTask which is a merging of a Task and a TaskCompletionSource (and their generic counterparts) which adds a Cancel method.

In place of Rust's Result<> this uses a ComponentTask Result together with a CancelCode enum.
LiftingTaskCompletionSource is a placeholder for returning values from async functions. It requires the lifting code, but hopefully can be used as a starting point.

Changes all task continuations to be ExecuteSynchronously as we only have one thread, and this is more predicable. This should be removed if we get true multithreading.

The index suffix on imports is now created sequentially, no longer hard coded to 0.

Adds the runtime test, future-cancel-read

Clear up some debug
use same package location for codegen and runtime tests.
@yowl yowl force-pushed the csharp-cancel-read branch from 0dee73b to a87cb70 Compare April 6, 2026 00:55
@yowl yowl marked this pull request as ready for review April 6, 2026 01:04
@yowl yowl requested review from dicej and silesmo April 6, 2026 01:04
@yowl yowl changed the title WIP Csharp cancel read [C#] Add cancel support for futures and streams Apr 6, 2026
Copy link
Copy Markdown
Collaborator

@dicej dicej left a comment

Choose a reason for hiding this comment

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

While dotnet Task are cancelable via tokens, that is not the design we want for components

Can you elaborate on this? What makes CancellationToken a bad fit here?

}
else if (it.IsCanceled)
{
// readTask.SetCanceled();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this commented out?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'll delete this, we dont actually need to do anything in here, the cancellation is on the outer task. We do call Cancel on this inner task, but its just for correctness rather than anything functional.

bufferHandle = lowerPayload();

var status = new WaitableStatus(VTableWrite(bufferHandle == null ? IntPtr.Zero : bufferHandle.Value.AddrOfPinnedObject(), length));
canDrop = true; // We can only call drop once something has been written.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should set this back to false if the write is cancelled.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, done.

@yowl
Copy link
Copy Markdown
Collaborator Author

yowl commented Apr 7, 2026

While dotnet Task are cancelable via tokens, that is not the design we want for components

Can you elaborate on this? What makes CancellationToken a bad fit here?

Its a cooperative mechanism: the long running task periodically checks the token to see if a cancellation has been requested, while typically another thread will issue the cancel on the token, We don't have a loop in C# where we could periodically check the cancellation status of the token.

yowl and others added 2 commits April 6, 2026 19:46
Make sure writables are not dropped after cancellation.
Co-authored-by: Joel Dice <joel.dice@akamai.com>
Copy link
Copy Markdown
Collaborator

@dicej dicej left a comment

Choose a reason for hiding this comment

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

Thanks!

@dicej dicej added this pull request to the merge queue Apr 7, 2026
Merged via the queue into bytecodealliance:main with commit a55ad81 Apr 7, 2026
28 checks passed
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