Fix period backup crash due to the dead lock of std::lock_guard

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<std::mutex> 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
pull/14911/head
Don Ho 2024-03-28 18:25:30 +01:00
parent 30f48aae1e
commit bbeaafac8b
1 changed files with 1 additions and 1 deletions

View File

@ -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;
}