-
Notifications
You must be signed in to change notification settings - Fork 567
Patch: Fix SQLiteCursor locking issue. #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello promeG, Thank you for your interest in SQLCipher for Android. We would prefer a pull request for changes like this, as it helps the integration process as well as provided proper attribution of the changes within the source tree. Also, it would be beneficial if there was test coverage for this type of change within the test suite. Thanks! |
Hi, dheerajb Sorry I can't find the problem based on your log, maybe you can try my modified libraries? I have uploaded them on: https://github.com/promeG/SQLCipher-Android-Fix-Deadlock-Patch/tree/master/libs |
Hey promeG you mentioned "I compared the lock method used in SQLCipher with AOSP4.0.1, and found that there are some difference." Can you please elaborate which file in AOSP you compared to? |
Hi dheerajb I went through with package "android.database.sqlite" in AOSP, and compared SQLCipher's SQLiteDatabase.java and SQLiteProgram.java. May be there are other classes which implement lock() improperly. An online refrence to AOSP code: Hope this will help you. |
Still no luck. I have one question. I understand we should avoid db operations in MAIN Thread, but a simple query should not cause an ANR. It was fine when we were not using SQLCipher. Now the same logic throws an ANR when we switched to SQLCipher. I am surprised why lock() is taking so much time to cause an ANR. "main" prio=5 tid=1 WAIT | group="main" sCount=1 dsCount=0 obj=0x41b79578 self=0x41ae6a60 | sysTid=6675 nice=0 sched=0/0 cgrp=apps handle=1074970620 | state=S schedstat=( 11494402761 2196695859 19554 ) utm=811 stm=337 core=3 at java.lang.Object.wait(Native Method)
|
Hello @dheerajb If you are able to recreate the issue in isolation, we would be glad to look at it further. Please feel free to create a test case within the SQLCipher for Android test suite and we can delve deeper into what the cause may be. Thanks! |
The issue seems to be the way the AOSP email client code interacts with SQLCipher. In my experience, especially when using Exchange ActiveSync support and a large mailbox with calendar and contact list support, it's not uncommon at startup for the e-mail SQL database to be accessed by several threads simultaneously performing both read and write operations. You can see the code that uses the SQL database here: In that scenario, replacing the Android standard SQLiteDatabase object by SQLCipher's occasionally leads to a deadlock with at least 10-15 threads deadlocked on the database object's lock. Because of the email client's design, which accesses the database from the UI thread in the Welcome activity, the deadlock eventually leads to an ANR. We ran into this problem when we attempted to integrate the AOSP e-mail client into our own application with the additional requirement that the database be encrypted. The issue occurred more often the more emails/contacts/calendar entries were present in the user's Exchange inbox and the slower the target handset was. |
Sorry was trying to edit my original post and deleted by mistake. Here goes the issue we are facing. Exactly as pointed out in the post. https://github.com/sqlcipher/android-database-sqlcipher /issues/97. Thanks draak567 for the insight. You are right we are getting in deadlcok often. Will keep posted. |
@developernotes : We are working on if we can isolate the issue. But the issue happens with google implementation of Email on android, so currently finding it difficult to isolate.
Can it be a dvm issue. Please see this link http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6865591. |
Hi I made following changes to the the SQLitedatbase.java. But the issue still exists. Can you please verify if this is a valid change (though our issue not resolved) orig: change 👍
|
Hi draak567 were you able to solve the issue when you tried integrating AOSP Email with your app?? |
I had tried this patch by @promeG , still we were in ANR. Now ANR is caused due to 👎 at java.lang.Object.wait(Native Method)
|
Hello @dheerajb SQLCipher for Android is being complied with serialized threading mode, within the Java library however, I would be curious if you removed the various |
We tried disabling the locks by calling SQLiteDatabase::setLockingEnabled(false). But we were not able to completely remove the "mLock" dependency. Since, lockForced() is making use of the same, and also we observed calls like "mLock.getHoldCount()" in beginTransaction(). So, we were not able to comment out the mLock. And also some times we observed that our UI was not updated. When we tried to analyze by collecting the dumpstate, we observed other threads (main thread was not involved) waiting for the lock (Similar to above pasted call stacks). UI was empty as the helper threads like Async tasks or Runnables which were trying to fetch data from the DB, were waiting on lock(). One point to note here is that we always dont get an ANR but our other threads/synctasks wait to acquire lock(). Any suggestions. |
Made the changes as suggested by you. Made sure we are building lib with serialized threading mode. Commented lock/unlock. After that as soon as we launch our App we get this error: 04-04 12:16:30.722: D/dalvikvm(8331): Trying to load lib /data/data/com.android.email/lib/libsqlcipher_android.so 0x42a766a8 |
It looks like mContentResolver.applyBatch is taking huge time and never releases the lock(). |
@dheerajb, try to periodically execute SQLiteDatabase#yieldIfContendedSafely() to temporarily release the lock during long update transaction. |
Did you figure out a solution to this issue? I am stuck with a very similar issue. |
@developernotes this issue is frequently reproduced in Lollipop, trying to isolate the root cause. |
I also have the same issue too. :-( Did anyone figure this out?? |
Hi , I am getting the following Exception trace "java.util.concurrent.TimeoutException: net.sqlcipher.database.SQLiteCompiledSql.finalize() timed out after 10 seconds" I looked into the Sqlcipher code and found the Reentrantlock causing problem while the GC running. Is there any way to fix this timeout issue? Will the setLockingEnabled(false) & the yieldIfContendedSafely() will work ? |
Hello @baskarsk Can you create a reproducible test in the SQLCipher for Android test suite? |
Thanks for the reply,. The issue is araising very rarely, i couldn't get the exact clue why it is happening. One debug scenario is , i left my device idle (Screen lock) for few hours and opening the app and doing a DB operation. From the below link you could find the exact statment on timeout Exception in Idle state.(Which matches my scenario too) Could you assist on this, meanwhile i run the Test Application and update on that. |
Hi , I am getting the following Exception trace "java.util.concurrent.TimeoutException: net.sqlcipher.database.SQLiteCompiledSql.finalize() timed out after 10 seconds" If I use "android.databse.sqlite" then app run normally. My Code:
|
Hi @GUNC The link that @baskarsk provided shows the Dalvik GC loop may likely be the cause of this. Since it appears you are inserting a rather large number of entries within a single transaction, you might consider batching those up into several smaller transactions to see if that helps address the issue. |
Thanks @developernotes |
Hi @GUNC I'm glad to hear that worked! |
Hi,@developernotes,i'm use net.zetetic:android-database-sqlcipher:3.5.1,sometimes we got anr,can you help me resolve the question,thanks. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157) |
Hi @qiyongqiang What version of SQLCipher for Android are you using? Are you able to recreate the issue as a unit test within the SQLCipher for Android test suite? |
@developernotes i'm use net.zetetic:android-database-sqlcipher:3.5.1, i can‘t recreate this issue as a unit test,this issue was found by bugly. are you some Suggestion? |
Hi @qiyongqiang I am not aware of what would cause that error to occur in your case. It would require further information such as the query that is attempting to run at the time, which is why I suggested you attempt to recreate the issue within the test suite. Also, it appears you are using a wrapper around SQLCipher for Android, you might try to rule that out as well. |
Hi, I have encountered deadlock problem when applying SQLCipher to my application.
There is a closed issue about it : #97
I compared the lock method used in SQLCipher with AOSP4.0.1, and found that there are some difference.
After doing some changes, I finally get rid of deadlock problem. Here is the patch: https://github.com/promeG/SQLCipher-Android-Fix-Deadlock-Patch
I hope it can help with other people who have the same problem.
SQLCipher is a great project, thank you for your hard work!
The text was updated successfully, but these errors were encountered: