Skip to content

Commit

Permalink
Add logging around the message processing lock.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal authored and alan-signal committed Feb 25, 2021
1 parent 32aea8d commit 8f7f836
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Expand Up @@ -13,6 +13,10 @@ public enum DatabaseSessionLock implements SignalSessionLock {

INSTANCE;

public static final long NO_OWNER = -1;

private volatile long ownerThreadId = NO_OWNER;

@Override
public Lock acquire() {
SQLiteDatabase db = DatabaseFactory.getInstance(ApplicationDependencies.getApplication()).getRawDatabase();
Expand All @@ -23,9 +27,34 @@ public Lock acquire() {

db.beginTransaction();

ownerThreadId = Thread.currentThread().getId();

return () -> {
ownerThreadId = -1;
db.setTransactionSuccessful();
db.endTransaction();
};
}

/**
* Important: Only truly useful for debugging. Do not rely on this for functionality. There's tiny
* windows where this state might not be fully accurate.
*
* @return True if it's likely that some other thread owns this lock, and it's not you.
*/
public boolean isLikelyHeldByOtherThread() {
long ownerThreadId = this.ownerThreadId;
return ownerThreadId != -1 && ownerThreadId == Thread.currentThread().getId();
}

/**
* Important: Only truly useful for debugging. Do not rely on this for functionality. There's a
* tiny window where a thread may still own the lock, but the state we track around it has been
* cleared.
*
* @return The ID of the thread that likely owns this lock, or {@link #NO_OWNER} if no one owns it.
*/
public long getLikeyOwnerThreadId() {
return ownerThreadId;
}
}
Expand Up @@ -113,7 +113,14 @@ private Processor(@NonNull Context context) {

stopwatch.split("queue-check");

long ownerThreadId = DatabaseSessionLock.INSTANCE.getLikeyOwnerThreadId();
if (ownerThreadId != DatabaseSessionLock.NO_OWNER && ownerThreadId != Thread.currentThread().getId()) {
Log.i(TAG, "It is likely that some other thread has this lock. Owner: " + ownerThreadId + ", Us: " + Thread.currentThread().getId());
}

try (SignalSessionLock.Lock unused = DatabaseSessionLock.INSTANCE.acquire()) {
Log.i(TAG, "Acquired lock while processing message " + envelope.getTimestamp() + ".");

DecryptionResult result = MessageDecryptionUtil.decrypt(context, envelope);
stopwatch.split("decrypt");

Expand Down

0 comments on commit 8f7f836

Please sign in to comment.