Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deadlock in Library::writeBookmarksToFile() #641

Closed
veloman-yunkan opened this issue Dec 5, 2021 · 0 comments · Fixed by #642
Closed

Deadlock in Library::writeBookmarksToFile() #641

veloman-yunkan opened this issue Dec 5, 2021 · 0 comments · Fixed by #642
Assignees
Milestone

Comments

@veloman-yunkan
Copy link
Collaborator

#636 introduced a deadlock in Library::writeBookmarksToFile().

Library::writeBookmarksToFile() calls LibXMLDumper::dumpLibXMLBookmark() under a lock

libkiwix/src/library.cpp

Lines 274 to 283 in 01ac0b2

bool Library::writeBookmarksToFile(const std::string& path) const
{
LibXMLDumper dumper(this);
std::string xml;
{
std::lock_guard<std::mutex> lock(m_mutex);
xml = dumper.dumpLibXMLBookmark();
};
return writeTextFile(path, xml);
}

LibXMLDumper::dumpLibXMLBookmark() calls the thread-safe function Library::getBookmarks() which is protected by the same mutex:

std::string LibXMLDumper::dumpLibXMLBookmark()
{
pugi::xml_document doc;
/* Add the library node */
pugi::xml_node bookmarksNode = doc.append_child("bookmarks");
if (library) {
for (auto& bookmark: library->getBookmarks()) {
handleBookmark(bookmark, bookmarksNode);
}
}
return nodeToString(bookmarksNode);
}

libkiwix/src/library.cpp

Lines 344 to 358 in 01ac0b2

const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const
{
if (!onlyValidBookmarks) {
return m_bookmarks;
}
std::vector<kiwix::Bookmark> validBookmarks;
auto booksId = getBooksIds();
std::lock_guard<std::mutex> lock(m_mutex);
for(auto& bookmark:m_bookmarks) {
if (std::find(booksId.begin(), booksId.end(), bookmark.getBookId()) != booksId.end()) {
validBookmarks.push_back(bookmark);
}
}
return validBookmarks;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants