fix(scheduler): improve async task lifetime safety#436
Open
ReallocAll wants to merge 14 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR strengthens async scheduler task lifetime safety during plugin cancellation/reload, aiming to prevent async callbacks from outliving unloaded C++ plugin libraries (follow-up to #351).
Changes:
- Adds async-task draining on plugin clear/unload (
waitForAsyncTasks) and improves cancellation cleanup to release plugin callback references promptly. - Improves thread-safety/robustness in scheduling (atomic tick tracking, safe task capture into executor, corrected async running-state reporting).
- Extends test coverage for async cancellation, cross-thread scheduling, and executor waiting behavior.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_thread_pool_executor.cpp | Adds coverage for zero-thread construction behavior and executor wait() semantics. |
| tests/test_scheduler.cpp | Adds regression tests for cross-thread scheduling, async lifecycle/cancellation, and exception recovery. |
| src/endstone/core/scheduler/thread_pool_executor.h | Tracks pending tasks and adds wait() to support draining queued async work. |
| src/endstone/core/scheduler/thread_pool_executor.cpp | Ensures at least one worker thread, implements wait(), and updates worker bookkeeping. |
| src/endstone/core/scheduler/task.h | Initializes plugin_ to nullptr for safer default state. |
| src/endstone/core/scheduler/scheduler.h | Adds waitForAsyncTasks() and makes current_tick_ atomic. |
| src/endstone/core/scheduler/scheduler.cpp | Improves cancellation cleanup, async running-state, cross-thread safety, and adds async wait support. |
| src/endstone/core/scheduler/async_task.h | Adds a condition variable and wait() for async task completion. |
| src/endstone/core/scheduler/async_task.cpp | Makes cancellation/start bookkeeping safer and adds task completion signaling. |
| src/endstone/core/plugin/plugin_manager.cpp | Waits for async work to finish after disabling plugins and before clearing/unloading them. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
19
to
24
| #include <atomic> | ||
| #include <functional> | ||
| #include <future> | ||
| #include <stdexcept> | ||
| #include <thread> | ||
| #include <vector> |
Comment on lines
15
to
+18
| #include "endstone/core/scheduler/thread_pool_executor.h" | ||
|
|
||
| #include <algorithm> | ||
|
|
Comment on lines
271
to
274
| } | ||
| else { | ||
| executor_.submit([&task]() { task->run(); }); | ||
| executor_.submit([task]() { task->run(); }); | ||
| } |
Comment on lines
44
to
+50
| catch (std::exception &e) { | ||
| exception = e; | ||
| getOwner()->getLogger().warning("Plugin {} generated an exception while executing task {}: {}", | ||
| getOwner()->getName(), getTaskId(), e.what()); | ||
| } | ||
| catch (...) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improves async scheduler task lifetime safety during plugin cancellation and reload.
Follow-up to #351.