Skip to content

Commit

Permalink
Add check for internal users around group lock ordering.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal authored and nicholas-signal committed Jun 28, 2023
1 parent 9d97921 commit b042945
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Expand Up @@ -18,4 +18,8 @@ public Lock acquire() {
LOCK.lock();
return LOCK::unlock;
}

public boolean isHeldByCurrentThread() {
return LOCK.isHeldByCurrentThread();
}
}
Expand Up @@ -137,7 +137,7 @@ public static void migrate(@NonNull Context context, @NonNull RecipientId recipi
public static void performLocalMigration(@NonNull Context context, @NonNull GroupId.V1 gv1Id) throws IOException
{
Log.i(TAG, "Beginning local migration! V1 ID: " + gv1Id, new Throwable());
try (Closeable ignored = GroupsV2ProcessingLock.acquireGroupProcessingLock()) {
try (Closeable ignored = GroupsV2ProcessingLock.acquireGroupProcessingLock(1000)) {
if (SignalDatabase.groups().groupExists(gv1Id.deriveV2MigrationGroupId())) {
Log.w(TAG, "Group was already migrated! Could have been waiting for the lock.", new Throwable());
return;
Expand Down
Expand Up @@ -4,6 +4,9 @@

import org.signal.core.util.ThreadUtil;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.crypto.ReentrantSessionLock;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.util.FeatureFlags;

import java.io.Closeable;
import java.util.concurrent.TimeUnit;
Expand All @@ -17,15 +20,25 @@ public final class GroupsV2ProcessingLock {
private GroupsV2ProcessingLock() {
}

private static final Lock lock = new ReentrantLock();
private static final ReentrantLock lock = new ReentrantLock();

@WorkerThread
public static Closeable acquireGroupProcessingLock() throws GroupChangeBusyException {
if (FeatureFlags.internalUser()) {
if (!lock.isHeldByCurrentThread()) {
if (SignalDatabase.inTransaction()) {
throw new AssertionError("Tried to acquire the group lock inside of a database transaction!");
}
if (ReentrantSessionLock.INSTANCE.isHeldByCurrentThread()) {
throw new AssertionError("Tried to acquire the group lock inside of the ReentrantSessionLock!!");
}
}
}
return acquireGroupProcessingLock(5000);
}

@WorkerThread
static Closeable acquireGroupProcessingLock(long timeoutMs) throws GroupChangeBusyException {
public static Closeable acquireGroupProcessingLock(long timeoutMs) throws GroupChangeBusyException {
ThreadUtil.assertNotMainThread();

try {
Expand Down

0 comments on commit b042945

Please sign in to comment.