Skip to content

Add exception handling in task_worker_loop to prevent worker thread termination#7658

Open
Copilot wants to merge 5 commits intomainfrom
copilot/add-try-catch-task-do-task
Open

Add exception handling in task_worker_loop to prevent worker thread termination#7658
Copilot wants to merge 5 commits intomainfrom
copilot/add-try-catch-task-do-task

Conversation

Copy link
Contributor

Copilot AI commented Feb 10, 2026

Task: Add try-catch block around do_task() in worker.h

  • Add necessary include for logging (ds/internal_logger.h)
  • Add try-catch block around task->do_task() call
  • Use task->get_name() for descriptive logging in catch handlers
  • Handle both std::exception and unknown exceptions
  • Create test to verify exception handling
  • Build and verify the changes work correctly (all 13 tests passed, 265/265 assertions)
  • Code review completed (no issues found)
  • Security check completed (no vulnerabilities)
  • Address review feedback: add <exception> include to worker.h
  • Fix test synchronization with bounded timeout (1 second max wait)
  • Format all changes with clang-format
Original prompt

Apply the suggestion from this comment, within the task system itself:
#7630 (comment)

Add a try catch block around the call to do_task() in src/tasks/worker.h to prevent exceptions escaping further. Use task->get_name() for descriptive logging.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: eddyashton <6000239+eddyashton@users.noreply.github.com>
Copilot AI changed the title [WIP] Add try catch block around do_task call in worker.h Add exception handling in task_worker_loop to prevent worker thread termination Feb 10, 2026
Copilot AI requested a review from eddyashton February 10, 2026 14:24
@eddyashton eddyashton marked this pull request as ready for review February 10, 2026 14:26
@eddyashton eddyashton requested a review from a team as a code owner February 10, 2026 14:26
Copilot AI review requested due to automatic review settings February 10, 2026 14:26
Copy link
Contributor

Copilot AI left a comment

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 improves the task system’s robustness by preventing exceptions thrown from BaseTask::do_task() from escaping the worker loop and terminating worker threads, and adds a regression test to ensure subsequent tasks still run after failures.

Changes:

  • Add worker-level try/catch around task->do_task() with logging for std::exception and unknown exceptions.
  • Add a doctest verifying the worker continues processing after tasks throw.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/tasks/worker.h Adds exception boundary + failure logging around task execution to keep worker threads alive.
src/tasks/test/basic_tasks.cpp Adds a regression test covering continued execution after exceptions.

@eddyashton
Copy link
Member

@copilot apply changes based on the comments in this thread, and format all changes with clang-format.

…nization, format code

Co-authored-by: eddyashton <6000239+eddyashton@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 10, 2026

@copilot apply changes based on the comments in this thread, and format all changes with clang-format.

Applied all review feedback and formatted with clang-format (23843eb). All 13 tests pass with 265/265 assertions.

catch (const std::exception& e)
{
LOG_FAIL_FMT(
"{} task failed with exception: {}", task->get_name(), e.what());
Copy link
Member

Choose a reason for hiding this comment

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

@eddyashton I think we need to at least trigger an orderly shutdown, or more likely abort in these situations, because we have a number of places in the code that throw an std::logic_error that is not particularly recoverable, and that may leave shared or thread local state that's fundamentally broken.

I think we should also try to print a stack trace here, at least when debug logging is enabled.

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.

3 participants

Comments