Vivart
Vivart

Reputation: 15313

main thread is waiting on sqlcipher cursor close

In my app we are doing applyBatch for around 2000 records in worker thread.

at the same time if i rotate the screen i am getting black screen.

"main@6280" prio=5 waiting java.lang.Thread.State: WAITING blocks main@6280 at java.lang.Object.wait(Object.java:-1) at java.lang.Thread.parkFor$(Thread.java:1220) - locked <0x18c5> (a java.lang.Object) at sun.misc.Unsafe.park(Unsafe.java:299) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158) at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810) at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:844) at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173) at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:196) at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:257) at net.sqlcipher.database.SQLiteDatabase.lock(SQLiteDatabase.java:490) at net.sqlcipher.database.SQLiteProgram.close(SQLiteProgram.java:294) at net.sqlcipher.database.SQLiteQuery.close(SQLiteQuery.java:136) at net.sqlcipher.database.SQLiteCursor.close(SQLiteCursor.java:510) at android.database.CursorWrapper.close(CursorWrapper.java:50) at android.database.CursorWrapper.close(CursorWrapper.java:50) at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:2512) at android.database.CursorWrapper.close(CursorWrapper.java:50)

I have tried withYieldAllowed but no luck. Any idea why main thread is waiting for cursor close and any solution for this porblem?

Upvotes: 1

Views: 597

Answers (1)

ceph3us
ceph3us

Reputation: 7474

at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:196) 
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:257) at 

debug and check which thread holds lock and why u got deadlock

when u do a db operation:

  • which thread is making db operations ?
  • are u using for that content provider or db helper ?
  • are u locking db or do u use some libs for it ?
  • do u use cursor loader ? if so do u call any method within which manipulates on activity thread?

No lock on ReentrantLock

Without lock

ReentrantLock locked for write by main thread

With lock from  main thread

to build an android app essentially you need to have a knowledge about concurrency, synchronization, locks:

https://docs.oracle.com/javase/tutorial/essential/concurrency/ https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html https://docs.oracle.com/javase/tutorial/essential/concurrency/liveness.html

start digging from there: net.sqlcipher.database.SQLiteDatabase.lock(SQLiteDatabase.java:490)

  • try batch transaction up into several smaller transactions
  • try to periodically execute SQLiteDatabase#yieldIfContendedSafely() to temporarily release the lock during long update transaction.
  • try disable the locks by calling SQLiteDatabase::setLockingEnabled(false) - that should disable Reentrant lock

Do you have this issue both on dalvik and art?

Upvotes: 1

Related Questions