fix: delete UvTaskRunner's timers only after they're closed (#43598)

* 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:
trop[bot] 2024-09-06 11:18:52 -05:00 committed by GitHub
parent a461a95b60
commit 9da190f786
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 35 deletions

View file

@ -16,6 +16,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/memory/weak_ptr.h"
#include "base/types/to_address.h"
#include "gin/public/context_holder.h"
#include "gin/public/gin_embedders.h"
#include "shell/common/node_includes.h"
@ -54,11 +55,25 @@ template <typename T,
std::is_same<T, uv_udp_t>::value>::type* = nullptr>
class UvHandle {
public:
UvHandle() : t_(new T) {}
UvHandle() : t_{new T} {}
~UvHandle() { reset(); }
UvHandle(UvHandle&&) = default;
UvHandle& operator=(UvHandle&&) = default;
UvHandle(const UvHandle&) = delete;
UvHandle& operator=(const UvHandle&) = delete;
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_); }
// compare by handle pointer address
auto operator<=>(const UvHandle& that) const = default;
void reset() {
auto* h = handle();
if (h != nullptr) {
@ -76,6 +91,16 @@ class UvHandle {
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 {
public:
enum class BrowserEnvironment { kBrowser, kRenderer, kUtility, kWorker };