Skip to content

Commit

Permalink
fix: handle CursorWindowAllocationException (#454)
Browse files Browse the repository at this point in the history
* fix: handle CursorWindowAllocationException when trying to get the database count

* fix: handle CursorWindowAllocationException when trying to get the database count

* refactor: RuntimeException exception handling logic

* chore: improve indentation in DBPersistentManager

* chore: remove unwanted files
  • Loading branch information
1abhishekpandey authored Jul 1, 2024
1 parent 445fc4a commit 5ddc570
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -510,6 +511,7 @@ private void acquireSemaphore() {
Thread.currentThread().interrupt();
}
}

private void waitTillMigrationsAreDone() {
if(migrationSemaphore.availablePermits() == 1 ){
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,8 +120,16 @@ private void deleteEventsWithoutAnonymousId(ArrayList<String> 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()) {
Expand Down

0 comments on commit 5ddc570

Please sign in to comment.