solrudev / Ackpine

Android package installer library
https://solrudev.github.io/Ackpine/
Apache License 2.0
50 stars 7 forks source link

Foreign Key Constraint Failure #68

Closed samjoshai closed 1 day ago

samjoshai commented 4 days ago

Hi there! Thanks for writing such a nice wrapper for the PackageInstaller API. Occasionally, when launching an install session with the session.await() call, the following exception is thrown: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)

Is this a known issue?

Thank you!

solrudev commented 4 days ago

@samjoshai Can you pinpoint the exact conditions in which the exception occurs? It'd be great if you provided a reproduction sample or at least the full exception stack trace.

samjoshai commented 4 days ago

@solrudev Ah yes of course. I know better than to posit such an abstract question. My apologies! Here is the full stack trace: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY) android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:940) at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790) at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:89) at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.kt:42) at ru.solrudev.ackpine.impl.database.dao.NativeSessionIdDao_Impl.setNativeSessionId(NativeSessionIdDao_Impl.java:49) at ru.solrudev.ackpine.impl.installer.session.SessionBasedInstallSession.persistNativeSessionId(SessionBasedInstallSession.kt:293) at ru.solrudev.ackpine.impl.installer.session.SessionBasedInstallSession.prepare(SessionBasedInstallSession.kt:139) at ru.solrudev.ackpine.impl.session.AbstractSession.launch$lambda$3(AbstractSession.kt:159) at ru.solrudev.ackpine.impl.session.AbstractSession.$r8$lambda$r7LANzByLTn5O4ddmFnq5gTMbDA(Unknown Source:0) at ru.solrudev.ackpine.impl.session.AbstractSession$$ExternalSyntheticLambda3.run(D8$$SyntheticClass:0) at ru.solrudev.ackpine.helpers.SerialExecutor$Task.run(SerialExecutor.kt:45) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:920)

As for reproducing the issue, it seems to happen randomly. I can perform many successful installations, then once in a while it fails with the exception above. Here is the configuration for the session: val newSession = installer.createSession(uri) { confirmation = Confirmation.IMMEDIATE installerType = InstallerType.SESSION_BASED name = fileName requireUserAction = false }

solrudev commented 4 days ago

@samjoshai I think I've identified a likely cause for this issue. Please test the 0.6.2-SNAPSHOT version (hosted on Sonatype snapshots repository) and get back with feedback.

solrudev commented 4 days ago

@samjoshai Sorry, after further analysis I figured that the change I've made in the snapshot version most probably wouldn't fix the issue. I'll investigate more.

solrudev commented 3 days ago

@samjoshai Please try the updated 0.6.2-SNAPSHOT version now. I introduced some synchronization to try to fix race condition which may lead to the foreign key constraint failure.

samjoshai commented 3 days ago

With the updated changes, I have gotten the exception three times out of around 40 or so installations. It does seems less prevalent. I am just going back and forth between installing a few apps, relaunching the updater from Android Studio, and installing a few apps. It does seem less frequent now.

solrudev commented 3 days ago

@samjoshai Have you tried the first iteration of the snapshot version before? If so, make sure that 0.6.2-20240629.153659-2 is the version used, because Gradle may have cached previous SNAPSHOT version and not updated it.

samjoshai commented 3 days ago

@solrudev With the version:

0.6.2-20240629.153659-2

I seem the get the issue frequently. Have you experienced this behavior before, or am I the only one observing this? I am starting to wonder if I am launching the session's coroutine in a poor place. Though, I should not have so many successful installations if this was the case.

solrudev commented 3 days ago

@samjoshai Please, try updated snapshot version 0.6.2-20240630.104729-3. Now I think races leading to the SQLiteConstraintException should be gone.

Have you experienced this behavior before, or am I the only one observing this?

I can't really remember, maybe one or two times or none at all. That's because I never experienced heavy concurrent usage of the library. Sample app doesn't have this usage scenario.

samjoshai commented 2 days ago

@solrudev I think that did the trick. I haven't gotten the exception so far. I will reach out to let you know if it happens again though. Thank you for your assistance, and thank you for creating this library! When can these changes be expected on a release version of the library?

solrudev commented 2 days ago

@samjoshai

I think that did the trick. I haven't gotten the exception so far.

That's great to know!

When can these changes be expected on a release version of the library?

New version will be released in a couple of days.

solrudev commented 1 day ago

If you have time, please take the survey and share your feedback about developer experience with Ackpine. Will be much appreciated!

Survey: https://forms.gle/REgYhPQJ19VoveeVA

samjoshai commented 22 hours ago

@solrudev Thank you again for your assistance with this. Apologies for having to ask, but what version of the library should be imported with these merged changes now? Thank you.

solrudev commented 22 hours ago

@samjoshai

what version of the library should be imported with these merged changes now?

0.7.0 version including the fix was released today.