Skip to content

Commit 3fb6a7b

Browse files
authored
CBL-8160 : Timeout when closing the database with an active MultipeerReplicator running (#470)
Problems : Closing the database with an active MultipeerReplicator running failed with the timeout error. Three related issues contributed to this bug: 1. Incorrect CountDownLatch reset in AbstractDatabase.shutdown() : When close() returns a BUSY error, the latch was reset to 2 under the assumption that a new process would be found in verifyActiveProcesses(). This assumption is not always valid (e.g. a background thread is still accessing the database during close). As a result, the latch never reaches zero and close() is never retried correctly. 2. MultipeerReplicator.onSyncStatusChanged reports inactive early (fixed in EE repo) : MultipeerReplicator.onSyncStatusChanged marked the process offline immediately but deferred unregisterProcess to an async callback . As a result, AbstractDatabase could treat the process as stopped, attempt to close the database too early, and receive BUSY from LiteCore. (fixed in EE repo) 3. Deadlock when close() is called from the Android main thread (fixed in EE repo) : Waiting for all active processes to unregister blocks the main thread, while shutdownConflictResolverService is also scheduled on the main thread (default executor), causing a deadlock. Fixes Simplified the shutdown logic by splitting it into two independent phases, which also eliminates the problematic CountDownLatch reset: Phase 1 — Drain active processes: Shut down all active processes, then wait for them to finish or time out (10 secs). Phase 2 — Close the database: Attempt close(); if BUSY is returned, wait briefly (2 secs) and retry (max 5 retries). Previously, the two phases were interleaved in a single loop: shut down processes → wait → close → on BUSY, reset latch to 2 and repeat from the top (max 5 retries). This coupling was the root cause of the latch never reaching zero.
1 parent 8ae6fd2 commit 3fb6a7b

1 file changed

Lines changed: 21 additions & 26 deletions

File tree

common/main/java/com/couchbase/lite/AbstractDatabase.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ abstract class AbstractDatabase extends BaseDatabase
9191

9292
private static final LogDomain DOMAIN = LogDomain.DATABASE;
9393

94-
private static final int DB_CLOSE_WAIT_SECS = 6; // > Core replicator timeout
95-
private static final int DB_CLOSE_MAX_RETRIES = 5; // random choice: wait for 5 replicators
94+
// Max time to wait for active processes to finish.
95+
private static final int DB_CLOSE_PROCESS_TIMEOUT_SECS = 10;
96+
// Backoff between BUSY retries on close.
97+
private static final int DB_CLOSE_RETRY_DELAY_SECS = 2;
98+
// Max number of BUSY retries before giving up.
99+
private static final int DB_CLOSE_MAX_RETRIES = 5;
100+
// Max time to wait for executors to drain.
96101
private static final int EXECUTOR_CLOSE_MAX_WAIT_SECS = 5;
97102

98103
static class ActiveProcess<T> {
@@ -1224,7 +1229,6 @@ private void verifyActiveProcesses() {
12241229
for (ActiveProcess<?> process: processes) { Log.d(DOMAIN, " processes: %s", process); }
12251230
}
12261231

1227-
@SuppressWarnings("PMD.NPathComplexity")
12281232
private void shutdown(boolean failIfClosed, Fn.ConsumerThrows<C4Database, LiteCoreException> onShut)
12291233
throws CouchbaseLiteException {
12301234
final C4Database c4Db;
@@ -1251,33 +1255,24 @@ private void shutdown(boolean failIfClosed, Fn.ConsumerThrows<C4Database, LiteCo
12511255
}
12521256

12531257
try {
1254-
for (int i = 0; ; i++) {
1255-
verifyActiveProcesses();
1258+
verifyActiveProcesses();
1259+
if (!closeLatch.await(DB_CLOSE_PROCESS_TIMEOUT_SECS, TimeUnit.SECONDS)) {
1260+
throw new CouchbaseLiteException("Shutdown failed", CBLError.Domain.CBLITE, CBLError.Code.BUSY);
1261+
}
12561262

1257-
if ((i >= DB_CLOSE_MAX_RETRIES) && (closeLatch.getCount() > 0)) {
1258-
throw new CouchbaseLiteException("Shutdown failed", CBLError.Domain.CBLITE, CBLError.Code.BUSY);
1263+
for (int i = 0; ; i++) {
1264+
try {
1265+
synchronized (getDbLock()) { onShut.accept(c4Db); }
1266+
break;
12591267
}
1260-
1261-
if (closeLatch.await(DB_CLOSE_WAIT_SECS, TimeUnit.SECONDS)) {
1262-
try {
1263-
synchronized (getDbLock()) { onShut.accept(c4Db); }
1264-
break;
1265-
}
1266-
catch (LiteCoreException e) {
1267-
if ((e.getDomain() != C4Constants.ErrorDomain.LITE_CORE)
1268-
|| (e.getCode() != C4Constants.LiteCoreError.BUSY)) {
1269-
throw CouchbaseLiteException.convertException(e);
1270-
}
1268+
catch (LiteCoreException e) {
1269+
if (i >= DB_CLOSE_MAX_RETRIES
1270+
|| e.getDomain() != C4Constants.ErrorDomain.LITE_CORE
1271+
|| e.getCode() != C4Constants.LiteCoreError.BUSY) {
1272+
throw CouchbaseLiteException.convertException(e);
12711273
}
1274+
Thread.sleep(TimeUnit.SECONDS.toMillis(DB_CLOSE_RETRY_DELAY_SECS));
12721275
}
1273-
1274-
// If we get here then, despite the fact that it appears to us that all
1275-
// active processes have been stopped, LiteCore has other ideas.
1276-
// We have no way of finding out what LiteCore thinks, other than waiting
1277-
// a bit and trying the again. Since verifyActiveProcess will count down
1278-
// the latch, we need a new one with a count of at least 2 in order to force
1279-
// a wait
1280-
closeLatch = new CountDownLatch(2);
12811276
}
12821277
}
12831278
catch (InterruptedException ignore) { }

0 commit comments

Comments
 (0)