Skip to content

Futures should be individually cancellable #520

Description

@kainino0x

Futures can currently be cancelled only by dropping the instance. It would be useful to be able cancel them individually.

As an example, if you write a C++ RAII wrapper around a WGPUFuture (say, myapp::WebGPUFuture<wgpu::ComputePipeline>) and you drop it, there's no way to ensure the callback gets called except to wait for the future. If we had a way to cancel futures like void wgpuInstanceCancelFuture(WGPUFuture)1, you could do that instead - basically, call the callback immediately and ignore/abort any ongoing work.

Right now we already have the InstanceDropped status, I think we could genericize this to Canceled/Cancelled2 and use it for this too.

Footnotes

  1. This function could have a return value indicating whether it actually got canceled, though it seems probably not necessary because you'll get the status via the callback. ↩

  2. though I dislike this word because its spelling is ambiguous, similar to adapter vs adaptor ↩

Activity

  1. added
    !discussNeeds discussion (at meeting or online)
    asyncAsynchronous operations and callbacks
    on Mar 14, 2025
  2. lokokung commented on Mar 14, 2025

    @lokokung
    Collaborator

    I like the idea of using Cancelled instead of InstanceDropped! Do you think we can just move forwards with this, and I'll start implementing it? Or did you want to wait and discuss?

    I think the rename of the enum could probably be fine without further discussion, but maybe whether we have CancelFuture we can wait and discuss since that would be non-breaking anyways?

  3. kainino0x commented on Mar 14, 2025

    @kainino0x
    CollaboratorAuthor

    I agree, having a more generic name is better and we should just go ahead with it. Though I'd love if we had a word that didn't have ambiguous spelling.

    Based on ngrams I'd probably choose Cancelled despite many claims that Canceled is the American spelling (I don't think that's really true, ngrams shows it close to even).
    https://books.google.com/ngrams/graph?content=canceled%2Ccancelled&year_start=1800&year_end=2022&corpus=en&smoothing=3&case_insensitive=false

  4. Kangz commented on Mar 17, 2025

    @Kangz
    Collaborator

    Strong +1 on this proposal.

  5. kainino0x commented on Mar 20, 2025

    @kainino0x
    CollaboratorAuthor

    Mar 20 meeting:

    • KN: Cleanup a future without dropping the whole instance.
    • CF: Don't understand why the described RAII wrapper should work like that. This would cause the callback to be called in the destructor. Possibly dangerous if using exceptions.
    • LK: TBH don't understand either.
    • CF: Seems like improper usage of the API. [Don't see when you would actually need to do this.]
    • …
    • KN: Wouldn't be able to really cancel all operations. Just cancel the callback, let the work finish in the background. Some concern that if WaitAny is happening on another thread, won't always be able to interrupt it. We'd call the callback immediately on cancel, but WaitAny would keep going and not call the callback.
      • (EDIT: This is not a concern with instance drop because you can't drop the last external ref to the instance while any thread is inside instance->WaitAny)
    • …
    • LK: We could add this after V1.
    • KN/LK: We'll need to investigate if it's possible to refactor this RAII wrapper thing so it either ASSERTs it's already complete in the destructor, or uses refcounting or weakref to make it so it's OK for the callback to be called later after the application drops its own ref to the RAII wrapper.
      • KN: Still feel like since dropping the instance can cancel futures, there should be a way to cancel them individually, but I don't have a specific use case for it.
    • KN: OK to rename InstanceDropped statuses to Cancelled?
    • CF: Sure.
  6. kainino0x commented on Mar 20, 2025

    @kainino0x
    CollaboratorAuthor

    tl;dr: we weren't convinced of the need for this - and of course we know we can add it post-v1, which I forgot to minute.

    @Kangz any concern?

  7. added
    non-breakingDoes not require a breaking change (that would block V1.0)
    and removed
    !discussNeeds discussion (at meeting or online)
    non-breakingDoes not require a breaking change (that would block V1.0)
    on Mar 20, 2025
  8. Kangz commented on Mar 24, 2025

    @Kangz
    Collaborator

    Sounds good especially since we can non-breaking add it postv1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    asyncAsynchronous operations and callbacksnon-breakingDoes not require a breaking change (that would block V1.0)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions