Skip to content

crypto is not available inside AudioWorkletGlobalScope #26522

Closed

Description

@pioug

Emscripten generates worklet-targeted bundle that crashes in AudioWorkletGlobalScope when random bytes are requested.

Repro: https://github.com/pioug/emscripten-worklet-randomfill

In the repro, a C call to getentropy() goes through Emscripten’s generic randomFill path, which emits a crypto.getRandomValues(...) call but crypto is not available in the audio worklet, so the generated bundle throws ReferenceError: crypto is not defined.

Might be a duplicate of #13224, but my findings point to a breaking change in 5a8d9e5 from Dec 31, 2024.

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 5.0.3-git
clang version 23.0.0git
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/emscripten/5.0.3/libexec/llvm/bin

Activity

  1. changed the title [-]Runtime uses crypto which is not available inside AudioWorkletGlobalScope[/-] [+]crypto is not available inside AudioWorkletGlobalScope[/+] on Mar 23, 2026
  2. sbc100 commented on Mar 23, 2026

    @sbc100
    Collaborator

    Yes, this is looks like its probably a duplicate of #13224.

    I'm curious about your findings pointing to #23247 though since that seems related to generate_uuid. Is your program calling generate_uuid?

  3. added 4 commits that reference this issue on Mar 23, 2026
    0c8a5a5
    19dc84b
    15ba984
    5d0310a
  4. pioug commented on Mar 24, 2026

    @pioug
    Author

    @sbc100 Ah yes, my original app uses generate_uuid. The repro highlights the usage of crypto in the worklet. It would be good if generate_uuid works too if it's a different code path.

    Thanks for working on it!

  5. added a commit that references this issue on Mar 24, 2026
    c9d9de9
  6. added a commit that references this issue on Mar 24, 2026
    f9274bc
  7. pioug commented on Mar 24, 2026

    @pioug
    Author

    @sbc100 Thanks for the quick fix! From what I understand, uuid_generate() won’t work as expected in a worklet unless we override initRandomFill. Is using a shim the recommended solution for now?

  8. sbc100 commented on Mar 24, 2026

    @sbc100
    Collaborator

    Could you open a new bug for uuid_generate in a wasm worker?

    I'm not really sure what the best solution would be there, since uuid_generate itself cannot fail. I guess it we can have it fall back from getentropy to just srand?

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