Skip to content

fix(scheduler): improve async task lifetime safety#436

Open
ReallocAll wants to merge 14 commits into
EndstoneMC:developfrom
ReallocAll:fix/scheduler-task-lifetime
Open

fix(scheduler): improve async task lifetime safety#436
ReallocAll wants to merge 14 commits into
EndstoneMC:developfrom
ReallocAll:fix/scheduler-task-lifetime

Conversation

@ReallocAll

Copy link
Copy Markdown
Member

Improves async scheduler task lifetime safety during plugin cancellation and reload.

  • Prevents queued and running async callbacks from outliving C++ plugins
  • Waits for async work to finish before unloading plugin libraries
  • Fixes async task cancellation, exception cleanup, and running-state reporting
  • Makes internal task scheduling safe across BDS worker threads
  • Handles executor enqueue failures without leaving pending task counts stuck
  • Adds regression tests for async cancellation and cross-thread scheduling

Follow-up to #351.

Copilot AI review requested due to automatic review settings July 16, 2026 17:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (...) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants