Skip to content

How to detect if running in a webworker? #2148

Closed
@alex-caelus

Description

@alex-caelus

Summary

I'm developing a very simple module that need access to either the Window or WorkerGlobalScope object depending on if it is running in a webworker or in the rendering thread.

Currently I haven't been able to figure it out, see below for what I have tried and why it doesn't work.

The reason I'm doing this is mainly because when I run my tests with GECKODRIVER it is running in the main thread but in my application I'm using a webworker.

Additional Details

This is my code, which dosen't work as expected.

use js_sys::global;
use log::trace;
use wasm_bindgen::JsCast;
use web_sys::{Window, WorkerGlobalScope};

impl CSPRNG {
    fn get_u128(&self) -> u128 {
        trace!("CSPRNG::get_u128");
        let mut bytes = [0; 16];
        let g = global();
        if WorkerGlobalScope::instanceof(&g) {
            if let Ok(scope) = g.dyn_into::<WorkerGlobalScope>() {
                scope
                    .crypto()
                    .expect("Crypto API not supported?")
                    .get_random_values_with_u8_array(&mut bytes[..])
                    .expect("Failed to obtain secure randomness");
            } else {
                panic!("failed conversion to WorkerGlobalScope");
            }
        } else if Window::instanceof(&g) {
            if let Ok(scope) = g.dyn_into::<Window>() {
                scope
                    .crypto()
                    .expect("Crypto API not supported?")
                    .get_random_values_with_u8_array(&mut bytes[..])
                    .expect("Failed to obtain secure randomness");
            } else {
                panic!("failed conversion to Window");
            }
        } else {
            panic!("Unsuported global type");
        }

        u128::from_be_bytes(bytes)
    }
}

When running with GECKODRIVER

console.log div contained:
    wasm-bindgen: imported JS function that was not marked as `catch` threw an error:
    WorkerGlobalScope is not defined

    Stack:
    init/imports.wbg.__wbg_instanceof_WorkerGlobalScope_93324c9f5660e5b4<@http://127.0.0.1:57750/wasm-bindgen-test:585:28
    logError/<@http://127.0.0.1:57750/wasm-bindgen-test:214:22
    web_sys::features::gen_WorkerGlobalScope::__wbg_generated_const_WorkerGlobalScope::<impl wasm_bindgen::cast::JsCast for web_sys::features::gen_WorkerGlobalScope::WorkerGlobalScope>::instanceof::h244fb6a2ad7e30cd@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[1060]:0x6fc0b
    <tiny_csprng::wasm32::CSPRNG as tiny_csprng::TinyPRNG>::get_u128::h8b676c7ec7cc1a4e@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[49]:0x126c3
    tiny_csprng::wasm32::test::get_a_random_value::hf04acc041de00098@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[563]:0x5de40
    core::ops::function::FnOnce::call_once::ha728f62e5aef6998@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[1942]:0x7f56f
    wasm_bindgen_test::__rt::Context::execute_sync::{{closure}}::hb7527fde4ab876a3@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[666]:0x62c30
    <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h51512de833fb91a2@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[628]:0x610d0
    <wasm_bindgen_test::__rt::TestFuture<F> as core::future::future::Future>::poll::{{closure}}::{{closure}}::h48ac108cd24bdf66@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[833]:0x6935e
    wasm_bindgen::convert::closures::invoke0_mut::h15117140ce631b6d@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[687]:0x63b0b
    __wbg_adapter_33@http://127.0.0.1:57750/wasm-bindgen-test:332:10
    cb0@http://127.0.0.1:57750/wasm-bindgen-test:488:28
    window.__wbg_test_invoke@http://127.0.0.1:57750/:37:38
    init/imports.wbg.__wbg_wbgtestinvoke_40963ed10b2b93d6<@http://127.0.0.1:57750/wasm-bindgen-test:493:30
    handleError/<@http://127.0.0.1:57750/wasm-bindgen-test:338:22
    wasm_bindgen_test::__rt::__wbg_test_invoke::h895bf8f3be895f05@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[342]:0x4f776
    <wasm_bindgen_test::__rt::TestFuture<F> as core::future::future::Future>::poll::{{closure}}::h64d913946b5019d0@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[723]:0x652b6
    scoped_tls::ScopedKey<T>::set::h8e0b39b0ee232b48@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[455]:0x57ade
    <wasm_bindgen_test::__rt::TestFuture<F> as core::future::future::Future>::poll::h5e6ad99cdbcba28d@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[111]:0x30766
    <wasm_bindgen_test::__rt::ExecuteTests as core::future::future::Future>::poll::h61ef56113a3600cf@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[57]:0x1a79a
    wasm_bindgen_test::__rt::Context::run::{{closure}}::h96c89f6e1b39c00c@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[232]:0x4498c
    <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h2cbe4e48de8d0528@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[482]:0x59571
    wasm_bindgen_futures::future_to_promise::{{closure}}::{{closure}}::hef8d06aea6caac7c@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[117]:0x31e0e
    <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hfeeaadbe23eb316a@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[634]:0x61536
    wasm_bindgen_futures::task::singlethread::Task::run::haf3924a5ccdff81d@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[198]:0x403a9
    wasm_bindgen_futures::queue::QueueState::run_all::h8f257c8f713a39fb@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[250]:0x46b7a
    wasm_bindgen_futures::queue::Queue::new::{{closure}}::he00ae9c50479de2b@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[1115]:0x71250
    <dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h44430f1c3bd26b81@http://127.0.0.1:57750/wasm-bindgen-test_bg.wasm:wasm-function[540]:0x5cb66
    __wbg_adapter_20@http://127.0.0.1:57750/wasm-bindgen-test:236:10
    real@http://127.0.0.1:57750/wasm-bindgen-test:201:20

When running in webworker (in application)

The code above actually works, but let's say for instance that I reverse the order of the if-else statements above and try the conversion to Window first then I would expect the same error as above to be shown in the developer console. But that is not what happens, instead my application hangs silently.

Solution

If I could detect, at runtime, if I'm running in a webworker or not (perhaps by checking if WorkerGlobalScope is defined, somehow) then I could use that and avoid to trigger the faults above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions