| 532 | } |
| 533 | |
| 534 | CancellationCode QueryStatus::cancelQuery(CancelReason reason, std::exception_ptr exception) |
| 535 | { |
| 536 | { |
| 537 | std::lock_guard<std::mutex> lock(cancel_mutex); |
| 538 | |
| 539 | if (is_killed) |
| 540 | return CancellationCode::CancelSent; |
| 541 | |
| 542 | LOG_TRACE(getLogger("ProcessList"), "Cancelling the query (reason: {})", reason); |
| 543 | |
| 544 | is_killed = true; |
| 545 | cancel_reason = reason; |
| 546 | cancellation_exception = exception; |
| 547 | } |
| 548 | |
| 549 | std::vector<ExecutorHolderPtr> executors_snapshot; |
| 550 | |
| 551 | { |
| 552 | /// Create a snapshot of executors under a mutex. |
| 553 | std::lock_guard lock(executors_mutex); |
| 554 | executors_snapshot.reserve(executors.size()); |
| 555 | for (const auto & [_, e] : executors) |
| 556 | executors_snapshot.push_back(e); |
| 557 | } |
| 558 | |
| 559 | /// We should call cancel() for each executor with unlocked executors_mutex, because |
| 560 | /// cancel() can try to lock some internal mutex that is already locked by query executing |
| 561 | /// thread, and query executing thread can call removePipelineExecutor and lock executors_mutex, |
| 562 | /// which will lead to deadlock. |
| 563 | /// Note that the size and the content of executors cannot be changed while |
| 564 | /// executors_mutex is unlocked, because: |
| 565 | /// 1) We don't allow adding new executors while cancelling query in addPipelineExecutor |
| 566 | /// 2) We don't actually remove executor holder from executors in removePipelineExecutor, |
| 567 | /// just mark that executor is invalid. |
| 568 | /// So, it's ok to use a snapshot created above under a mutex, it won't be any differ from actual executors. |
| 569 | for (const auto & e : executors_snapshot) |
| 570 | e->cancel(); |
| 571 | |
| 572 | return CancellationCode::CancelSent; |
| 573 | } |
| 574 | |
| 575 | void QueryStatus::throwProperExceptionIfNeeded(const UInt64 & max_execution_time_ms, const UInt64 & elapsed_ns) |
| 576 | { |
no test coverage detected