LibWeb: Keep active platform timers alive during GC

Treat active Platform::Timer objects as event-loop roots so their GC
callbacks stay marked while the underlying Core::Timer can still fire.
Finalize unreachable timers by stopping the Core timer and dropping the
callback, preventing incremental sweep from leaving a timer with a raw
pointer to a swept GC::Function.
This commit is contained in:
Andreas Kling 2026-05-07 23:25:58 +02:00 committed by Andreas Kling
parent a4945a651f
commit d51d849943
2 changed files with 17 additions and 0 deletions

View file

@ -24,6 +24,18 @@ Timer::Timer()
Timer::~Timer() = default;
void Timer::finalize()
{
Base::finalize();
stop();
on_timeout = nullptr;
}
bool Timer::must_survive_garbage_collection() const
{
return is_active();
}
void Timer::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -22,7 +22,12 @@ public:
static GC::Ref<Timer> create_repeating(GC::Heap&, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler);
static GC::Ref<Timer> create_single_shot(GC::Heap&, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler);
static constexpr bool OVERRIDES_MUST_SURVIVE_GARBAGE_COLLECTION = true;
static constexpr bool OVERRIDES_FINALIZE = true;
virtual ~Timer();
virtual void finalize() override;
virtual bool must_survive_garbage_collection() const override;
void start();
void start(int interval_ms);