Skip to content

Commit

Permalink
Merge pull request #4231 from MechanicalSoftworks/forking-race-condition
Browse files Browse the repository at this point in the history
Fix race condition surrounding DB reopening during fork
  • Loading branch information
rouault authored Aug 31, 2024
2 parents ad98ddf + 6fcc657 commit e2fd35d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/iso19111/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,23 @@ SQLiteHandleCache::getHandle(const std::string &path, PJ_CONTEXT *ctx) {
#ifdef REOPEN_SQLITE_DB_AFTER_FORK
if (firstTime_) {
firstTime_ = false;
pthread_atfork(nullptr, nullptr,
[]() { SQLiteHandleCache::get().invalidateHandles(); });
pthread_atfork(
[]() {
// This mutex needs to be acquired by 'invalidateHandles()'.
// The forking thread needs to own this mutex during the fork.
// Otherwise there's an opporunity for another thread to own
// the mutex during the fork, leaving the child process unable
// to acquire the mutex in invalidateHandles().
SQLiteHandleCache::get().sMutex_.lock();
},
[]() {
SQLiteHandleCache::get().sMutex_.unlock();
},
[]() {
SQLiteHandleCache::get().sMutex_.unlock();
SQLiteHandleCache::get().invalidateHandles();
}
);
}
#endif

Expand Down

0 comments on commit e2fd35d

Please sign in to comment.