From bbeaafac8bde911d3268f2d40b90329feb5c57f4 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Thu, 28 Mar 2024 18:25:30 +0100 Subject: [PATCH] Fix period backup crash due to the dead lock of std::lock_guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The crash occurs because the thread terminates the task prematurely due to PostMessage’s nature. As a result, FileManager::backupCurrentBuffer() is always executed by the main thread, leading to a deadlock ( due to "std::lock_guard lock(backup_mutex);") on the 2nd main thread’s entry and causing the crash. Here the explanation: "If lock is called by a thread that already owns the mutex, the behavior is undefined: for example, the program may deadlock." ref: https://en.cppreference.com/w/cpp/thread/mutex/lock Using SendMessage instead of PostMessage ensures that the thread executes the task from the beginning to the end and keeps the mutex until the entire operation is terminated. Therefore, the race condition is prevented by the mutex lock while the 2nd thread tries to access the same code/zone. Fix #14906, close #14917 --- PowerEditor/src/Notepad_plus.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 25701d0d9..7987d529a 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -8470,7 +8470,7 @@ DWORD WINAPI Notepad_plus::backupDocument(void * /*param*/) if (!isSnapshotMode) break; - ::PostMessage(Notepad_plus_Window::gNppHWND, NPPM_INTERNAL_SAVEBACKUP, 0, 0); + ::SendMessage(Notepad_plus_Window::gNppHWND, NPPM_INTERNAL_SAVEBACKUP, 0, 0); } return TRUE; }