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

@ -2,31 +2,33 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/app/uv_task_runner.h"
#include <utility>
#include "base/location.h"
#include "base/time/time.h"
#include "shell/app/uv_task_runner.h"
namespace electron {
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_(loop) {}
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {}
UvTaskRunner::~UvTaskRunner() {
for (auto& iter : tasks_) {
uv_unref(reinterpret_cast<uv_handle_t*>(iter.first));
delete iter.first;
}
}
UvTaskRunner::~UvTaskRunner() = default;
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
base::OnceClosure task,
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;
uv_timer_init(loop_, timer);
uv_timer_start(timer, UvTaskRunner::OnTimeout, delay.InMilliseconds(), 0);
tasks_[timer] = std::move(task);
uv_timer_init(loop_, timer.get());
uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0);
tasks_.insert_or_assign(std::move(timer), std::move(task));
return true;
}
@ -40,22 +42,4 @@ bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
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