Skip to content

Reconsider "in parallel"? #389

Open
Open

Description

@annevk

From WebKit's source code:

The 11 December 2014 version of the specification suggests we should perform the following task asynchronously:
https://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-importKey
It is not beneficial for less time consuming operations. Therefore, we perform it synchronously.

This came up in #368 first.

It's probably worth writing some tests to see when the promises are getting resolved relative to the running task.

Activity

  1. annevk commented on Dec 19, 2024

    @annevk
    MemberAuthor

    And also, it seems good to investigate this in general before building on top of the current API shape.

  2. twiss commented on Dec 19, 2024

    @twiss
    Member

    The way I interpreted @davidben's suggestion in #368 (comment) is not to change the API shape, but rather to do something like:

    1. Let promise be a new Promise.
    2. Return promise and queue a microtask with the remaining steps.
    3. ...
    4. Resolve promise with result.

    Given the loose definition of running in parallel I think an implementation is already free to implement stuff as above today?

    However, we could also change the spec text to the above (in the cases where running in parallel doesn't matter). That way, we don't have to bother with task queues, while not introducing any observable difference.

    In any case, I don't think it's strictly necessary to change the API if we want to run things synchronously. We could add a variant of the functions that's required to run synchronously and returns the result instead of a Promise, for the convenience of callers, as requested in #167 at some point, but that's a slightly orthogonal question.


    It's probably worth writing some tests to see when the promises are getting resolved relative to the running task.

    This I do agree with :)

    If there are already implementations that resolve the Promise before returning it, then perhaps it also doesn't matter as much exactly how we define the above, though it might still be nice to be consistent with everything else.

  3. annevk commented on Dec 19, 2024

    @annevk
    MemberAuthor

    There's an observable difference between queueing a task and queuing a microtask/resolving the promise immediately. Always queueing a task (and not caring about blocking) could be considered about equivalent to going in parallel and queueing a task and the difference might not be test-observable, but it's not clear this is what implementations actually do. And it's also not clear if that's what we want going forward if we were to do something new.

  4. twiss commented on Dec 19, 2024

    @twiss
    Member

    OK yeah, I guess it'd need to be a full task and not a microtask, then. But yeah I agree it'd be good to test this first.

  5. twiss commented on Dec 19, 2024

    @twiss
    Member

    However, for #73 (comment) specifically (which indirectly triggered this discussion), we still need to return a Promise when hashing an AsyncIterable, even if we want to do the hashing on the main thread. So, creating a new API for synchronous hashing (and other operations) actually wouldn't help us there, so I think it's fairly orthogonal.

  6. annevk commented on Dec 19, 2024

    @annevk
    MemberAuthor

    It would impact queue a task vs resolve immediately, I suspect.

  7. twiss commented on Dec 19, 2024

    @twiss
    Member

    We can't resolve immediately if we have to wait for the AsyncIterable to produce data; so it'd essentially force us to queue a task as we're doing now, I imagine.

  8. davidben commented on Dec 19, 2024

    @davidben
    Collaborator

    The way I interpreted @davidben's suggestion in #368 (comment) is not to change the API shape, but rather to do something like: [...]

    Huh? We've talked at length about this, so to suggest that I want to keep WebCrypto's API shape is quite surprising. WebCrypto's API shape is bad and we absolutely should be re-evaluating it before building on it. It does not match best practices for either cryptography APIs or JavaScript APIs in general.

    • It was a mistake to make WebCrypto asynchronous. Even if we mitigate it by resolving the promise immediately, we still pay for task or microtask overhead (and inefficiencies up the stack), as well as ergonomics problems, which is significant at the scale of these operations. It also makes it hard for, say, a Wasm module to outsource its cryptography to WebCrypto, if you want to take advantage of CPU acceleration or the underlying library's side channel protections.

    • It was a mistake to make WebCrypto take algorithms by objects with strings. We have a perfectly serviceable way to name APIs (crypto.subtle.sha256 or whatever) and didn't need to invent a meta one.

    • It was a mistake to make WebCrypto impossible to feature-detect. To feature-detect an algorithm in WebCrypto, you have to actually perform it. This is completely inconsistent with how many protocols work, where you have to advertise ahead of time what you support. As a result, feature detection requires you perform an extra operation for no reason.

    • It was a mistake to jam all of WebCrypto into a common set of verbs. See all the problems we've had around truncation and pretending ECDH and HKDF were the same operation.

    Instead, we could have just built a very natural, straightfoward set of functions that go from bytes to bytes, matching what everyone else does. For example hashing could simply have been a family of crypto.subtle.sha256(), crypto.subtle.sha384(), etc. functions. And if we wanted streaming, the standard, low-level API would be something like new crypto.subtle.SHA256Context() giving back an object with update() and final() methods.

    Of course, that still doesn't answer what to do with the current API. The current API must be retained for compatibility, in which case we probably need to mitigate its badness as much as we can. The mitigation will be imperfect, so we still need to build a replacement API, but I agree that just running the operations immediately is a good idea.

    How much to enshrine in the spec, I'm not sure. There are a lot of open questions in my mind around classifying operations as fast or slow (which we really shouldn't have to do), etc. And Chromium does not currently run them immediately (but I think we should). So we could simply make the spec admit both implementations, or we could go change the spec now. The latter runs the risk that we'll find we changed the spec prematurely, which has been a problem here before. Though that Safari already does this is definitely promising.

  9. twiss commented on Dec 19, 2024

    @twiss
    Member

    Yes, I'm aware that you think the API is bad and was a mistake :) All of your points are valid. However, since you said (in the comment I linked)

    I think we probably should make Chromium run it on the same thread (which further demonstrates that this was just a bad API because now we're paying for task overhead for no reason)

    I read that as talking specifically about running the functions as currently defined on the same thread, and then schedule a task to resolve the Promise which that API prescribes to return. Otherwise, if we change the API shape the comment wouldn't make any sense since we wouldn't be paying for the overhead of queuing a task :)

    Of course, that still doesn't answer what to do with the current API. The current API must be retained for compatibility, in which case we probably need to mitigate its badness as much as we can. The mitigation will be imperfect, so we still need to build a replacement API, but I agree that just running the operations immediately is a good idea.

    How much to enshrine in the spec, I'm not sure. There are a lot of open questions in my mind around classifying operations as fast or slow (which we really shouldn't have to do), etc. And Chromium does not currently run them immediately (but I think we should). So we could simply make the spec admit both implementations, or we could go change the spec now. The latter runs the risk that we'll find we changed the spec prematurely, which has been a problem here before. Though that Safari already does this is definitely promising.

    The spec already allows running things on the main thread (although it does require queuing a task to resolve the promise indeed). So if you want to experiment with running stuff on the main thread that can be done without any changes to the spec. I'd propose we start with that before we consider changing the spec or (even more drastically) creating an entirely new API.

  10. davidben commented on Dec 19, 2024

    @davidben
    Collaborator

    Oh, no implementation work we do to save this API will change the need to make a new WebCrypto API if we want a performant and usable way for the web platform to get at cryptography. We don't need to block that question on anything.

  11. twiss commented on Dec 19, 2024

    @twiss
    Member

    Sure, I just don't think it's very useful to mix every single issue with "no, we need to throw everything away and start over". The question of whether we want to run the current functions on the main thread is somewhat orthogonal from whether we want a new synchronous API. And the question of streaming is again orthogonal because we can't hash an AsyncIterable synchronously anyway.

    Also, creating an entirely new API seems fairly out of scope for the current charter of the WebAppSec WG, which says:

    The WG may adopt well-supported proposals from incubation for maintenance of the Web Cryptography API

    (emphasis mine). So, if you want to advocate for creating a new API, please also propose to expand the charter.

  12. twiss commented on Jul 30, 2025

    @twiss
    Member

    Getting back to the original question, I (finally) wrote some tests to check the current behavior in a sampling of functions. The results are as follows:

    • generateKey
      • on success:
        • Chrome and Firefox schedule a task ✅
        • Safari schedules a task for RSA keys ✅, but resolves immediately for all other algorithms ❌
      • on failure:
        • Chrome behaves per spec ✅
        • Safari and Firefox always reject immediately (which is correct for some errors ✅ but errors coming from the algorithm-specific operation require scheduling a task per spec ❌)
    • importKey
      • on success:
        • Chrome schedules a task ✅
        • Firefox schedules a task for asymmetric keys ✅, but resolves immediately for symmetric keys ❌
        • Safari always resolves immediately ❌
      • on failure:
        • Chrome always schedules a task to reject (which is correct for some errors ✅ but a wrong algorithm identifier requires immediate rejection per spec ❌)
        • Safari and Firefox always reject immediately (which is correct for some errors ✅ but errors coming from the algorithm-specific operation require scheduling a task per spec ❌)
    • digest
      • on success: all implementations schedule a task ✅
      • on failure: all implementations reject immediately ✅
    • sign/verify (only tested EdDSA and HMAC for now)
      • on success: all implementations schedule a task ✅
      • on failure: not tested yet
    • encrypt/decrypt
      • on success: all implementations schedule a task ✅
      • on failure: not tested yet
    • other functions not tested yet

    (where "immediately" is indistinguishable from "in a microtask").

    So, we can probably say that for generateKey and importKey, it doesn't matter for web compatibility whether a task is scheduled or not. For the other functions, I also find it hard to imagine that changing this would break anything, but all implementations do it correctly (for the functions & algorithms I tested), so maybe it's not worth the risk of changing it?

    That being said, for #73, I think it might simplify the spec and implementation if we could do the operation in a microtask.
    Perhaps the simplest thing to do would be to schedule a microtask but then say that the implementation may spin the event loop in the cryptographic operation to turn it into a regular task?

    Alternatively, if we care about achieving consistency here, I can make a PR with the tests to wpt, and file implementation issues. But, I'm not really sure it's worth the effort to try to align very strictly here.

    Let me know what you think :)

  13. annevk commented on Jul 30, 2025

    @annevk
    MemberAuthor

    I think this is something we need to eventually converge on, one way or another. Everything that's observable will at some point be relied upon by somebody.

  14. davidben commented on Jul 30, 2025

    @davidben
    Collaborator

    I've got a change in the pipeline to make Chromium run most operations synchronously, with the exception of RSA keygen and PBKDF2 (because the iteration count can be unreasonably high).

    But I don't think this is the sort of thing that needs to be enshrined in the spec. Asynchronous operations will unavoidably resolve unpredictably. Making this asynchronous was, of course, a mistake, but we should be spending our effort on fixing that.

  15. annevk commented on Jul 30, 2025

    @annevk
    MemberAuthor

    What should not be enshrined? I think we absolutely need to be clear on whether we resolve from the current task or whether we queue a task to resolve. That's kind of the basics of event loop integration.

    (And if we continue to use "in parallel" we can only use the latter.)

  16. panva commented on Aug 5, 2025

    @panva
    Member

    @twiss can you share the tests?

  17. twiss commented on Aug 5, 2025

    @twiss
    Member

    @panva: web-platform-tests/wpt#54144

    @davidben: After the change you're planning, do you still schedule a task to resolve the promise? If so, no spec change is needed. "in parallel" doesn't require running on a separate thread, AFAIU, and I'd agree that we shouldn't start requiring that.
    If however you want to resolve the promise immediately or in a microtask, then that's technically a breaking change as Anne pointed out (although I think it's unlikely to break anything in practice). And, it would require a spec change, so then it's not a question of enshrining something in the spec that currently isn't defined, but rather loosening the spec to allow that (or reaching an agreement to change it everywhere, of course).

  18. annevk commented on Sep 8, 2025

    @annevk
    MemberAuthor

    I was reminded that the inputs can be quite large in size. From that perspective "in parallel" + "queue a task" is probably the responsible way to go. (Though implementations could still opt to just "queue a task" and not do anything in parallel.) We can test this by checking that a timer task runs earlier.

  19. davidben commented on Sep 8, 2025

    @davidben
    Collaborator

    Array.sort also can have large inputs, and for loops can have many iterations, but we don't think the entire mechanism should be async. Rather, we say it's the JS developer's responsibility that, if they need to do significant CPU-bound work, that they need to maintain interactivity somehow, e.g. by running it a worker.

    (For rough orders of magnitude on the underlying primitives (note that WebCrypto'S API design mistakes adds overhead, so WebCrypto benchmarks aren't meaningful), my machine here computes SHA-256 at 2.4GB/s, AES-128-GCM at 7 GB/s, P-256 signing at 62,000 qps, P-256 verify at 24,000 qps. More constrained devices will no doubt be slower, but there's a lot of margin here.)

    The Chrome change resolves fast primitives in the next microtask. I think ideally the spec would admit either. Not great, but it reflects mistakes in how the API was designed:

    If we exposed this CPU-bound operation like a normal synchronous API, possibly forcing select slow primitives to a worker thread, callers would have been written to those cost expectations. However, because we made an async API, and the browser (at significant relative overhead) often just scheduled everything on a worker thread, callers were written to a very different cost expectation. So it may well be that we're forced to, say, automatically move some primitive async based on input size. I hope not, however. I expect this API just isn't used enough to have much legacy.

  20. smaug---- commented on Sep 9, 2025

    @smaug----

    I think ideally the spec would admit either.

    that is likely no go. Specs need to be rather precise whether something is asynchronous or not (microtasks aren't really async). Scheduling differences lead often to webcompat issues.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions