mirror of
https://github.com/electron/electron.git
synced 2026-05-02 03:00:22 -04:00
* fix: nodeIntegrationInWorker not working in AudioWorklet Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * fix: deadlock on Windows when destroying non-AudioWorklet worker contexts The previous change kept the WebWorkerObserver alive across ContextWillDestroy so the worker thread could be reused for the next context (AudioWorklet thread pooling, Chromium CL:5270028). This is correct for AudioWorklet but wrong for PaintWorklet and other worker types, which Blink does not pool — each teardown destroys the thread. For those worker types, ~NodeBindings was deferred to the thread-exit TLS callback. By that point set_uv_env(nullptr) had already run, so on Windows the embed thread was parked in GetQueuedCompletionStatus with a stale async_sent latch that swallowed the eventual WakeupEmbedThread() from ~NodeBindings. uv_thread_join then blocked forever, deadlocking renderer navigation. The worker-multiple-destroy crash case timed out on win-x64/x86/arm64 as a result. macOS/Linux (epoll/kqueue) don't have the latch and were unaffected. Plumb is_audio_worklet from WillDestroyWorkerContextOnWorkerThread into ContextWillDestroy. For non-AudioWorklet contexts, restore the pre-existing behavior of calling lazy_tls->Set(nullptr) at the end of the last-context cleanup so ~NodeBindings runs while the worker thread is still healthy. AudioWorklet continues to keep the observer alive so the next pooled context can share NodeBindings. Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: address review feedback Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * fix: stop embed thread before destroying environments in worker teardown FreeEnvironment (called via environments_.clear()) runs uv_run to drain handle close callbacks. On Windows, both that uv_run and the embed thread's PollEvents call GetQueuedCompletionStatus on the same IOCP handle. IOCP completions are consumed by exactly one waiter, so the embed thread can steal completions that FreeEnvironment needs, causing uv_run to block indefinitely. On Linux/Mac epoll_wait/kevent can wake multiple waiters for the same event so the race doesn't manifest. Add NodeBindings::StopPolling() which cleanly joins the embed thread without destroying handles or the loop, and allows PrepareEmbedThread + StartPolling to restart it later. Call StopPolling() in WebWorkerObserver::ContextWillDestroy before environments_.clear() so FreeEnvironment's uv_run is the only thread touching the IOCP. Split PrepareEmbedThread's handle initialization (uv_async_init, uv_sem_init) from thread creation via a new embed_thread_prepared_ flag so the handles survive across stop/restart cycles for pooled worklets while the embed thread itself can be recreated. Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: address outstanding feedback Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: update patches --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
// Copyright (c) 2017 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef ELECTRON_SHELL_RENDERER_WEB_WORKER_OBSERVER_H_
|
|
#define ELECTRON_SHELL_RENDERER_WEB_WORKER_OBSERVER_H_
|
|
|
|
#include <memory>
|
|
|
|
#include "base/containers/flat_set.h"
|
|
#include "v8/include/v8-forward.h"
|
|
|
|
namespace node {
|
|
|
|
class Environment;
|
|
|
|
} // namespace node
|
|
|
|
namespace electron {
|
|
|
|
class ElectronBindings;
|
|
class NodeBindings;
|
|
|
|
// Watches for WebWorker and insert node integration to it.
|
|
class WebWorkerObserver {
|
|
public:
|
|
WebWorkerObserver();
|
|
~WebWorkerObserver();
|
|
|
|
// Returns the WebWorkerObserver for current worker thread.
|
|
static WebWorkerObserver* GetCurrent();
|
|
// Creates a new WebWorkerObserver for a given context.
|
|
static WebWorkerObserver* Create();
|
|
|
|
// disable copy
|
|
WebWorkerObserver(const WebWorkerObserver&) = delete;
|
|
WebWorkerObserver& operator=(const WebWorkerObserver&) = delete;
|
|
|
|
void WorkerScriptReadyForEvaluation(v8::Local<v8::Context> context);
|
|
void ContextWillDestroy(v8::Local<v8::Context> context);
|
|
|
|
private:
|
|
// Full initialization for the first context on a thread.
|
|
void InitializeNewEnvironment(v8::Local<v8::Context> context);
|
|
// Share existing environment with a new context on a reused thread.
|
|
void ShareEnvironmentWithContext(v8::Local<v8::Context> context);
|
|
|
|
std::unique_ptr<NodeBindings> node_bindings_;
|
|
std::unique_ptr<ElectronBindings> electron_bindings_;
|
|
base::flat_set<std::shared_ptr<node::Environment>> environments_;
|
|
|
|
// Number of active contexts using the environment on this thread.
|
|
size_t active_context_count_ = 0;
|
|
};
|
|
|
|
} // namespace electron
|
|
|
|
#endif // ELECTRON_SHELL_RENDERER_WEB_WORKER_OBSERVER_H_
|