From b85720709c68853a60fef6d2794413efe9307fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Thu, 5 Sep 2024 19:06:38 +0200 Subject: [PATCH] ResourceLoader: Add last resort life-time insurance for tokens (cherry picked from commit 924d82999dfb0aed41336189b0b532d67c544eb8) --- core/io/resource_loader.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 87acecf9767d..0f1724da076c 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -321,6 +321,7 @@ Ref ResourceLoader::_load(const String &p_path, const String &p_origin } // This implementation must allow re-entrancy for a task that started awaiting in a deeper stack frame. +// The load task token must be manually re-referenced before this is called, which includes threaded runs. void ResourceLoader::_run_load_task(void *p_userdata) { ThreadLoadTask &load_task = *(ThreadLoadTask *)p_userdata; ThreadLoadTask *curr_load_task_backup = curr_load_task; @@ -450,6 +451,9 @@ void ResourceLoader::_run_load_task(void *p_userdata) { } } + // It's safe now to let the task go in case no one else was grabbing the token. + load_task.load_token->unreference(); + if (unlock_pending) { thread_load_mutex.unlock(); } @@ -600,6 +604,11 @@ Ref ResourceLoader::_load_start(const String &p_path, } } + // It's important to keep the token alive because until the load completes, + // which includes before the thread start, it may happen that no one is grabbing + // the token anymore so it's released. + load_task_ptr->load_token->reference(); + if (p_thread_mode == LOAD_THREAD_FROM_CURRENT) { // The current thread may happen to be a thread from the pool. WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->get_caller_task_id(); @@ -784,6 +793,7 @@ Ref ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Erro // resource loading that means that the task to wait for can be restarted here to break the // cycle, with as much recursion into this process as needed. // When the stack is eventually unrolled, the original load will have been notified to go on. + load_task.load_token->reference(); _run_load_task(&load_task); }