Skip to content

Commit

Permalink
Fix period backup crash due to the dead lock of std::lock_guard
Browse files Browse the repository at this point in the history
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
  • Loading branch information
donho committed Mar 29, 2024
1 parent 30f48aa commit bbeaafa
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit bbeaafa

Please sign in to comment.