fix: uv_walk crash on web worker close (#24436)
* fix: uv_walk crash on web worker close * Use DCHECK_EQ
This commit is contained in:
parent
2aeaca6f7b
commit
6cfbee9f34
2 changed files with 22 additions and 18 deletions
|
@ -96,25 +96,25 @@ ELECTRON_DESKTOP_CAPTURER_MODULE(V)
|
|||
namespace {
|
||||
|
||||
void stop_and_close_uv_loop(uv_loop_t* loop) {
|
||||
// Close any active handles
|
||||
uv_stop(loop);
|
||||
uv_walk(
|
||||
loop,
|
||||
[](uv_handle_t* handle, void*) {
|
||||
if (!uv_is_closing(handle)) {
|
||||
uv_close(handle, nullptr);
|
||||
}
|
||||
},
|
||||
nullptr);
|
||||
int error = uv_loop_close(loop);
|
||||
|
||||
// Run the loop to let it finish all the closing handles
|
||||
// NB: after uv_stop(), uv_run(UV_RUN_DEFAULT) returns 0 when that's done
|
||||
for (;;)
|
||||
if (!uv_run(loop, UV_RUN_DEFAULT))
|
||||
break;
|
||||
while (error) {
|
||||
uv_run(loop, UV_RUN_DEFAULT);
|
||||
uv_stop(loop);
|
||||
uv_walk(
|
||||
loop,
|
||||
[](uv_handle_t* handle, void*) {
|
||||
if (!uv_is_closing(handle)) {
|
||||
uv_close(handle, nullptr);
|
||||
}
|
||||
},
|
||||
nullptr);
|
||||
uv_run(loop, UV_RUN_DEFAULT);
|
||||
error = uv_loop_close(loop);
|
||||
}
|
||||
|
||||
DCHECK(!uv_loop_alive(loop));
|
||||
uv_loop_close(loop);
|
||||
DCHECK_EQ(error, 0);
|
||||
}
|
||||
|
||||
bool g_is_initialized = false;
|
||||
|
@ -274,6 +274,7 @@ NodeBindings::~NodeBindings() {
|
|||
// Quit the embed thread.
|
||||
embed_closed_ = true;
|
||||
uv_sem_post(&embed_sem_);
|
||||
|
||||
WakeupEmbedThread();
|
||||
|
||||
// Wait for everything to be done.
|
||||
|
@ -284,7 +285,7 @@ NodeBindings::~NodeBindings() {
|
|||
uv_close(reinterpret_cast<uv_handle_t*>(&dummy_uv_handle_), nullptr);
|
||||
|
||||
// Clean up worker loop
|
||||
if (uv_loop_ == &worker_loop_)
|
||||
if (in_worker_loop())
|
||||
stop_and_close_uv_loop(uv_loop_);
|
||||
}
|
||||
|
||||
|
@ -501,7 +502,8 @@ void NodeBindings::WakeupMainThread() {
|
|||
}
|
||||
|
||||
void NodeBindings::WakeupEmbedThread() {
|
||||
uv_async_send(&dummy_uv_handle_);
|
||||
if (!in_worker_loop())
|
||||
uv_async_send(&dummy_uv_handle_);
|
||||
}
|
||||
|
||||
// static
|
||||
|
|
|
@ -58,6 +58,8 @@ class NodeBindings {
|
|||
|
||||
uv_loop_t* uv_loop() const { return uv_loop_; }
|
||||
|
||||
bool in_worker_loop() const { return uv_loop_ == &worker_loop_; }
|
||||
|
||||
protected:
|
||||
explicit NodeBindings(BrowserEnvironment browser_env);
|
||||
|
||||
|
|
Loading…
Reference in a new issue