diff --git a/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java b/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java index 98982e6d..7d95073e 100644 --- a/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java +++ b/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java @@ -410,7 +410,7 @@ int getDeviceModeWithProcessedPendingEventsRecordCount() { private int getCountForCommand(String sql) { // initiate count int count = -1; - + Cursor cursor = null; try { // get readable database instance if (!persistence.isAccessible()) { @@ -419,7 +419,6 @@ private int getCountForCommand(String sql) { } RudderLogger.logDebug(String.format(Locale.US, "DBPersistentManager: getDBRecordCount: countSQL: %s", sql)); - Cursor cursor; synchronized (DB_LOCK) { cursor = persistence.rawQuery(sql, null); } @@ -433,12 +432,14 @@ private int getCountForCommand(String sql) { } else { RudderLogger.logInfo("DBPersistentManager: getDBRecordCount: DB is empty"); } - // release cursor - cursor.close(); - } catch (SQLiteDatabaseCorruptException ex) { RudderLogger.logError(ex); ReportManager.reportError(ex); + } finally { + if (cursor != null) { + // release cursor + cursor.close(); + } } return count; @@ -510,6 +511,7 @@ private void acquireSemaphore() { Thread.currentThread().interrupt(); } } + private void waitTillMigrationsAreDone() { if(migrationSemaphore.availablePermits() == 1 ){ return; diff --git a/core/src/main/java/com/rudderstack/android/sdk/core/RudderCloudModeManager.java b/core/src/main/java/com/rudderstack/android/sdk/core/RudderCloudModeManager.java index d0d19b8e..fcee7169 100644 --- a/core/src/main/java/com/rudderstack/android/sdk/core/RudderCloudModeManager.java +++ b/core/src/main/java/com/rudderstack/android/sdk/core/RudderCloudModeManager.java @@ -10,6 +10,7 @@ import com.rudderstack.android.sdk.core.util.MessageUploadLock; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -119,8 +120,16 @@ private void deleteEventsWithoutAnonymousId(ArrayList messages, ArrayLis * check if the number of events in the db crossed the dbCountThreshold then delete the older events which are in excess. */ private void maintainDBThreshold() { - // get current record count from db - int recordCount = dbManager.getDBRecordCount(); + int recordCount = 0; + try { + // get current record count from db + recordCount = dbManager.getDBRecordCount(); + } + // Added RuntimeException in order to catch CursorWindowAllocationException (this requires API level 33 and above). + catch (RuntimeException ex) { + RudderLogger.logError("CloudModeManager: maintainDBThreshold: Exception while fetching count from DB due to: " + Arrays.toString(ex.getStackTrace())); + ReportManager.reportError(ex); + } RudderLogger.logDebug(String.format(Locale.US, "CloudModeManager: getPayloadFromMessages: DBRecordCount: %d", recordCount)); // if record count exceeds threshold count, remove older events if (recordCount > config.getDbCountThreshold()) {