Skip to content

Commit 3050002

Browse files
add a check to verify thread matching between the encryption thread and the thread where we release the attachment. If they match, use a dummy mutex instead of the actual dbb_thread_mutex to avoid a deadlock
1 parent b1c4ea9 commit 3050002

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/common/ThreadStart.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,14 @@ ThreadId Thread::getId()
219219
#endif
220220
}
221221

222+
bool Thread::isCurrent(InternalId iid)
223+
{
224+
return pthread_equal(iid, pthread_self());
225+
}
226+
222227
bool Thread::isCurrent()
223228
{
224-
return pthread_equal(internalId, pthread_self());
229+
return isCurrent(internalId);
225230
}
226231

227232
void Thread::sleep(unsigned milliseconds)
@@ -363,9 +368,14 @@ ThreadId Thread::getId()
363368
return GetCurrentThreadId();
364369
}
365370

371+
bool Thread::isCurrent(InternalId iid)
372+
{
373+
return GetCurrentThreadId() == iid;
374+
}
375+
366376
bool Thread::isCurrent()
367377
{
368-
return GetCurrentThreadId() == internalId;
378+
return isCurrent(internalId);
369379
}
370380

371381
void Thread::sleep(unsigned milliseconds)

src/common/ThreadStart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Thread
8787
static void sleep(unsigned milliseconds);
8888
static void yield();
8989

90+
static bool isCurrent(InternalId iid);
9091
bool isCurrent();
9192

9293
Thread()

src/jrd/CryptoManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ class CryptoManager final : public Firebird::PermanentStorage, public BarSync::I
301301
UCHAR getCurrentState(thread_db* tdbb) const;
302302
const char* getKeyName() const;
303303
const char* getPluginName() const;
304+
Thread::Handle getCryptThreadHandle() const
305+
{
306+
return cryptThreadId;
307+
}
304308

305309
private:
306310
enum IoResult {SUCCESS_ALL, FAILED_CRYPT, FAILED_IO};

src/jrd/jrd.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7755,14 +7755,21 @@ void release_attachment(thread_db* tdbb, Jrd::Attachment* attachment, XThreadEns
77557755

77567756
Sync sync(&dbb->dbb_sync, "jrd.cpp: release_attachment");
77577757

7758+
// dummy mutex is used to avoid races with crypto thread
7759+
XThreadMutex dummy_mutex;
7760+
XThreadEnsureUnlock dummyGuard(dummy_mutex, FB_FUNCTION);
7761+
77587762
// avoid races with special threads
77597763
// take into an account lock earlier taken in DROP DATABASE
77607764
XThreadEnsureUnlock threadGuard(dbb->dbb_thread_mutex, FB_FUNCTION);
77617765
XThreadEnsureUnlock* activeThreadGuard = dropGuard;
77627766
if (!activeThreadGuard)
77637767
{
7764-
threadGuard.enter();
7765-
activeThreadGuard = &threadGuard;
7768+
if (dbb->dbb_crypto_manager && Thread::isCurrent(dbb->dbb_crypto_manager->getCryptThreadHandle()))
7769+
activeThreadGuard = &dummyGuard;
7770+
else
7771+
activeThreadGuard = &threadGuard;
7772+
activeThreadGuard->enter();
77667773
}
77677774

77687775
sync.lock(SYNC_EXCLUSIVE);

0 commit comments

Comments
 (0)