fix: delete UvTaskRunner's timers only after they're closed (#43597)
* fix: free UvTaskRunner timers only after they are closed Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: UvTaskRunner now holds UvHandles Co-authored-by: Charles Kerr <charles@charleskerr.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
parent
60264f30d8
commit
d92770a275
3 changed files with 42 additions and 35 deletions
|
@ -2,31 +2,33 @@
|
||||||
// Use of this source code is governed by the MIT license that can be
|
// Use of this source code is governed by the MIT license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "shell/app/uv_task_runner.h"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "base/location.h"
|
#include "base/location.h"
|
||||||
#include "base/time/time.h"
|
#include "base/time/time.h"
|
||||||
#include "shell/app/uv_task_runner.h"
|
|
||||||
|
|
||||||
namespace electron {
|
namespace electron {
|
||||||
|
|
||||||
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_(loop) {}
|
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {}
|
||||||
|
|
||||||
UvTaskRunner::~UvTaskRunner() {
|
UvTaskRunner::~UvTaskRunner() = default;
|
||||||
for (auto& iter : tasks_) {
|
|
||||||
uv_unref(reinterpret_cast<uv_handle_t*>(iter.first));
|
|
||||||
delete iter.first;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
|
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
|
||||||
base::OnceClosure task,
|
base::OnceClosure task,
|
||||||
base::TimeDelta delay) {
|
base::TimeDelta delay) {
|
||||||
auto* timer = new uv_timer_t;
|
auto on_timeout = [](uv_timer_t* timer) {
|
||||||
|
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->tasks_;
|
||||||
|
if (auto iter = tasks.find(timer); iter != tasks.end())
|
||||||
|
std::move(tasks.extract(iter).mapped()).Run();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto timer = UvHandle<uv_timer_t>{};
|
||||||
timer->data = this;
|
timer->data = this;
|
||||||
uv_timer_init(loop_, timer);
|
uv_timer_init(loop_, timer.get());
|
||||||
uv_timer_start(timer, UvTaskRunner::OnTimeout, delay.InMilliseconds(), 0);
|
uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0);
|
||||||
tasks_[timer] = std::move(task);
|
tasks_.insert_or_assign(std::move(timer), std::move(task));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,22 +42,4 @@ bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
|
||||||
return PostDelayedTask(from_here, std::move(task), delay);
|
return PostDelayedTask(from_here, std::move(task), delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
|
||||||
void UvTaskRunner::OnTimeout(uv_timer_t* timer) {
|
|
||||||
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->tasks_;
|
|
||||||
const auto iter = tasks.find(timer);
|
|
||||||
if (iter == std::end(tasks))
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::move(iter->second).Run();
|
|
||||||
tasks.erase(iter);
|
|
||||||
uv_timer_stop(timer);
|
|
||||||
uv_close(reinterpret_cast<uv_handle_t*>(timer), UvTaskRunner::OnClose);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void UvTaskRunner::OnClose(uv_handle_t* handle) {
|
|
||||||
delete reinterpret_cast<uv_timer_t*>(handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
#include "base/memory/raw_ptr.h"
|
#include "base/memory/raw_ptr.h"
|
||||||
#include "base/task/single_thread_task_runner.h"
|
#include "base/task/single_thread_task_runner.h"
|
||||||
#include "uv.h" // NOLINT(build/include_directory)
|
#include "shell/common/node_bindings.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class Location;
|
class Location;
|
||||||
|
@ -38,12 +38,10 @@ class UvTaskRunner : public base::SingleThreadTaskRunner {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
~UvTaskRunner() override;
|
~UvTaskRunner() override;
|
||||||
static void OnTimeout(uv_timer_t* timer);
|
|
||||||
static void OnClose(uv_handle_t* handle);
|
|
||||||
|
|
||||||
raw_ptr<uv_loop_t> loop_;
|
raw_ptr<uv_loop_t> loop_;
|
||||||
|
|
||||||
std::map<uv_timer_t*, base::OnceClosure> tasks_;
|
std::map<UvHandle<uv_timer_t>, base::OnceClosure, UvHandleCompare> tasks_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "base/memory/raw_ptr.h"
|
#include "base/memory/raw_ptr.h"
|
||||||
#include "base/memory/raw_ptr_exclusion.h"
|
#include "base/memory/raw_ptr_exclusion.h"
|
||||||
#include "base/memory/weak_ptr.h"
|
#include "base/memory/weak_ptr.h"
|
||||||
|
#include "base/types/to_address.h"
|
||||||
#include "gin/public/context_holder.h"
|
#include "gin/public/context_holder.h"
|
||||||
#include "gin/public/gin_embedders.h"
|
#include "gin/public/gin_embedders.h"
|
||||||
#include "uv.h" // NOLINT(build/include_directory)
|
#include "uv.h" // NOLINT(build/include_directory)
|
||||||
|
@ -58,11 +59,25 @@ template <typename T,
|
||||||
std::is_same<T, uv_udp_t>::value>::type* = nullptr>
|
std::is_same<T, uv_udp_t>::value>::type* = nullptr>
|
||||||
class UvHandle {
|
class UvHandle {
|
||||||
public:
|
public:
|
||||||
UvHandle() : t_(new T) {}
|
UvHandle() : t_{new T} {}
|
||||||
~UvHandle() { reset(); }
|
~UvHandle() { reset(); }
|
||||||
|
|
||||||
|
UvHandle(UvHandle&&) = default;
|
||||||
|
UvHandle& operator=(UvHandle&&) = default;
|
||||||
|
|
||||||
|
UvHandle(const UvHandle&) = delete;
|
||||||
|
UvHandle& operator=(const UvHandle&) = delete;
|
||||||
|
|
||||||
T* get() { return t_; }
|
T* get() { return t_; }
|
||||||
|
T* operator->() { return t_; }
|
||||||
|
const T* get() const { return t_; }
|
||||||
|
const T* operator->() const { return t_; }
|
||||||
|
|
||||||
uv_handle_t* handle() { return reinterpret_cast<uv_handle_t*>(t_); }
|
uv_handle_t* handle() { return reinterpret_cast<uv_handle_t*>(t_); }
|
||||||
|
|
||||||
|
// compare by handle pointer address
|
||||||
|
auto operator<=>(const UvHandle& that) const = default;
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
auto* h = handle();
|
auto* h = handle();
|
||||||
if (h != nullptr) {
|
if (h != nullptr) {
|
||||||
|
@ -80,6 +95,16 @@ class UvHandle {
|
||||||
RAW_PTR_EXCLUSION T* t_ = {};
|
RAW_PTR_EXCLUSION T* t_ = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper for comparing UvHandles and raw uv pointers, e.g. as map keys
|
||||||
|
struct UvHandleCompare {
|
||||||
|
using is_transparent = void;
|
||||||
|
|
||||||
|
template <typename U, typename V>
|
||||||
|
bool operator()(U const& u, V const& v) const {
|
||||||
|
return base::to_address(u) < base::to_address(v);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class NodeBindings {
|
class NodeBindings {
|
||||||
public:
|
public:
|
||||||
enum class BrowserEnvironment { kBrowser, kRenderer, kUtility, kWorker };
|
enum class BrowserEnvironment { kBrowser, kRenderer, kUtility, kWorker };
|
||||||
|
|
Loading…
Reference in a new issue