From b4aed421dea79096cfd43bd46228e5ac289f3742 Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Tue, 15 Mar 2022 15:33:43 +0000 Subject: [PATCH] Reset THreadPool's thread memory tracker parent to global after S3 upload task is finished. --- src/Disks/S3/DiskS3.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Disks/S3/DiskS3.cpp b/src/Disks/S3/DiskS3.cpp index 5d61285981b..29aaa461b71 100644 --- a/src/Disks/S3/DiskS3.cpp +++ b/src/Disks/S3/DiskS3.cpp @@ -272,6 +272,17 @@ std::unique_ptr DiskS3::writeFile(const String & path, SCOPE_EXIT_SAFE( if (thread_group) CurrentThread::detachQueryIfNotDetached(); + + /// After we detached from the thread_group, parent for memory_tracker inside ThreadStatus will be reset to it's parent. + /// Typically, it may be changes from Process to User. + /// Usually it could be ok, because thread pool task is executed before user-level memory tracker is destroyed. + /// However, thread could stay alive inside the thread pool, and it's ThreadStatus as well. + /// When, finally, we destroy the thread (and the ThreadStatus), + /// it can use memory tracker in the ~ThreadStatus in order to alloc/free untracked_memory,\ + /// and by this time user-level memory tracker may be already destroyed. + /// + /// As a work-around, reset memory tracker to total, which is always alive. + CurrentThread::get().memory_tracker.setParent(&total_memory_tracker); ); callback(); });