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

* fix: free UvTaskRunner timers only after they are closed

* refactor: UvTaskRunner now holds UvHandles
This commit is contained in:
Charles Kerr 2024-09-06 07:16:56 -05:00 committed by GitHub
parent 25f4691e78
commit cc5aa65cb4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 35 deletions

View file

@ -15,6 +15,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 "uv.h" // NOLINT(build/include_directory)
@ -58,11 +59,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) {
@ -80,6 +95,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 };