Repository navigation
Prevent use-after-free in BackgroundSchedulePool::TaskInfo by tracking pool shutdown - #80069
Conversation
0e1666d to
46b205a
Compare
|
@serxa Quite a bit confused about the CI error. Most of them seems unrelated. |
Yes, CI is currently experiencing issues. We are working on reducing the number of flaky tests. Don't worry about unrelated tests at the moment. Let me review the code... |
| // This prevents any further use of the pool (e.g., cancelDelayedTask) from within | ||
| // BackgroundSchedulePoolTaskInfo::deactivate(), avoiding use-after-free errors. | ||
| for (auto & task : delayed_tasks) | ||
| task.second->pool_shutdown = true; |
There was a problem hiding this comment.
Here we access pool_shutdown without locking the corresponding schedule_mutex. This is a data race.
|
Although it is a viable approach presented in this PR. Could we just ensure that the TaskInfo object never lives longer than the pool? Because if TaskInfo is destroyed earlier, we have a dangling reference. I'm not sure 100%, but I believe the pool was designed to never be outlived by TaskInfo. So maybe we should better fix the usage pattern instead to avoid a dangling reference in the first place? |
|
@serxa
Initially, I attempted the first approach, trying to ensure that the pool destructs all tasks directly. However, I encountered a logical paradox:
So if we require the pool to destruct all tasks, we contradict the fact that tasks can exist without the pool initially holding them — which is a valid and necessary part of the current lifecycle. That led me to re-read your comment carefully and reinterpret "TaskInfo object never lives longer than the pool`" in a slightly different way:
Under this model, it is perfectly safe for TaskInfo to outlive the pool as long as it's deactivated, and this aligns with the design goal of avoiding dangling references and use-after-free. To support this, I verified that all methods in TaskInfo already check the deactivated flag, so calling Let me know if this interpretation makes sense. I'm happy to align the implementation accordingly. |
|
All failed tests are unrelated and flaky |
|
I have to change a changelog category because bugfix category implies a fixed tests or new tests added (it blocks merging). There were no fixed tests and it is perfectly fine for such a fix. |
|
@VicoWu could you please trigger with empty commit |
|
|
|
Failed performance tests are unrelated |
There are multiple issues in the BackgroundSchedulePool, due to BackgroundSchedulePoolTaskInfo holds raw reference to the pool, which is wrong for multiple reasons, and there is no way to fix it properly because we do not track all tasks in the BackgroundSchedulePool (even inactive), and eventually this may lead to crash (during server shutdown of background pools). There were multiple attempts to fix it: - ClickHouse#80069 - It introduces deadlock [1] - And a UB due to usuing foreach and erase inside - But the main reason is that it does not fix all the issues, i.e. active() after the pool has been destroyed still leads to UB [1]: https://s3.amazonaws.com/clickhouse-test-reports/PRs/81398/c07d7ee6eb7c3d1d0c65649ea4b57f881571599c//stress_test_amd_debug/gdb.log - previous version of this patch set - Introduces lock order inversion [2] and [3], since I tried the apprach that ClickHouse#80069 was been using - clean all jobs in the destructor of the pool - But, when it came to the test I found out that while clickhouse-server may use this pool correctly it is hard to do so without knowing how to, so after this I converted the raw reference of the BackgroundSchedulePool in BackgroundSchedulePoolTaskInfo to weak_ptr, to avoid any UBs [2]: https://s3.amazonaws.com/clickhouse-test-reports/PRs/81473/a0f20c2ad7efc19d698ceb0281e0a122ebef457e//stress_test_amd_tsan/stderr.log [3]: https://s3.amazonaws.com/clickhouse-test-reports/PRs/81473/6404951bfb5156b00a4aa5302d1c0ca8272a2c56//stress_test_amd_tsan/stderr.log Also I've encapsulated the BackgroundSchedulePool and BackgroundSchedulePoolTaskInfo more, to avoid any misuse.
This patch adds a
pool_shutdownflag toBackgroundSchedulePool::TaskInfoto indicate when the owningBackgroundSchedulePoolis being destroyed. This prevents callingcancelDelayedTask()on an already-destroyed pool from withinTaskInfo::deactivate().Also:
pool_shutdown = truefor all delayed tasks during pool destruction.pool_shutdownwithschedule_mutexand grouped it with other task state flags.This prevents a use-after-free crash in rare cases when a delayed task
is still pending while the BackgroundSchedulePool is shutting down.
Already reported in issue #80066
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Prevent use-after-free in BackgroundSchedulePool::TaskInfo by tracking pool shutdown
Documentation entry for user-facing changes