Skip to content
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

Task/anr fix zimfile reader #3937

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

CalebKL
Copy link
Contributor

@CalebKL CalebKL commented Jul 15, 2024

Fixes #3929

This PR fixes the ANR thats being caused while trying to create the zimFileReader object. The UI was being blocked since we were creating the object on IO Thread but we are using runblocking which blocks the main thread. The solution to the problem was to suspend setZimFile and setZimFileDescriptor. Then tie the scope on lifecycleScope on the Fragments. When the zimFileReader is being created and it takes time for the initial load, the progressBar will be visible to the users and when Loading is complete, the progressBar will be Gone.
The solution doesn't block the Main Thread and instead switches the background work to a different thread(IO)

Screenshots

@MohitMaliFtechiz MohitMaliFtechiz self-requested a review July 15, 2024 17:34
Copy link
Collaborator

@MohitMaliFtechiz MohitMaliFtechiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CalebKL Thanks for the PR and your hard work. But there are some edge cases and crashes scenarios introduced we should fix them.

There is a crash when I frequently changed the zim files. Also, in UI there is old zim files loading when I am switching to other zim file which is bigger in size. I guess because of this, this crash is happening.

2024-07-16 14:20:19.873 12535-12609 libc                    org.kiwix.kiwixmobile                A  Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 12609 (ThreadPoolForeg)
2024-07-16 14:20:19.875 12535-12602 ZimFileReader           org.kiwix.kiwixmobile                E  Could not get Item for url = https://kiwix.app/I/Flag_of_Greece.svg.png.webp 
                                                                                                     original exception = java.lang.Exception: Cannot find entry
2024-07-16 14:20:19.876 12535-12602 ZimFileReader           org.kiwix.kiwixmobile                D  getting mimetype for https://kiwix.app/I/Flag_of_Greece.svg.png.webp = null
2024-07-16 14:20:19.877 12535-12821 ZimFileReader           org.kiwix.kiwixmobile                E  Could not get Item for url = https://kiwix.app/I/Political_slogan_by_Red_Guards_on_the_campus_of_Fudan_University_1976.jpg.webp 
                                                                                                     original exception = java.lang.Exception: Cannot find entry
2024-07-16 14:20:19.898 12535-15737 ZimFileReader           org.kiwix.kiwixmobile                E  Could not get Item for url = https://kiwix.app/I/Flag_of_Greece.svg.png.webp 
                                                                                                     original exception = java.lang.Exception: Cannot find entry
2024-07-16 14:20:19.904 12535-12535 AppCompatViewInflater   org.kiwix.kiwixmobile                I  app:theme is now deprecated. Please move to using android:theme instead.
2024-07-16 14:20:19.911 12535-12535 CubicBezierInterpolator org.kiwix.kiwixmobile                D  CubicBezierInterpolator  mControlPoint1x = 0.23, mControlPoint1y = 0.06, mControlPoint2x = 0.09, mControlPoint2y = 0.97
2024-07-16 14:20:19.912 12535-12535 CubicBezierInterpolator org.kiwix.kiwixmobile                D  CubicBezierInterpolator  mControlPoint1x = 0.6, mControlPoint1y = 0.9, mControlPoint2x = 0.8, mControlPoint2y = 1.0
2024-07-16 14:20:19.913 12535-12535 CubicBezierInterpolator org.kiwix.kiwixmobile                D  CubicBezierInterpolator  mControlPoint1x = 0.23, mControlPoint1y = 0.06, mControlPoint2x = 0.09, mControlPoint2y = 0.97
2024-07-16 14:20:19.913 12535-12535 CubicBezierInterpolator org.kiwix.kiwixmobile                D  CubicBezierInterpolator  mControlPoint1x = 0.6, mControlPoint1y = 0.9, mControlPoint2x = 0.8, mControlPoint2y = 1.0
2024-07-16 14:20:20.129 17250-17250 DEBUG                   pid-17250                            A  pid: 12535, tid: 12609, name: ThreadPoolForeg  >>> org.kiwix.kiwixmobile <<<
2024-07-16 14:20:20.132 17250-17250 DEBUG                   pid-17250                            A      #00 pc 0000000000010630  /data/app/org.kiwix.kiwixmobile-G5GGa4VbPmK5Z5zSGKhzQw==/base.apk (offset 0x71f000)
2024-07-16 14:20:20.132 17250-17250 DEBUG                   pid-17250                            A      #01 pc 000000000005be38  /data/app/org.kiwix.kiwixmobile-G5GGa4VbPmK5Z5zSGKhzQw==/oat/arm64/base.odex (offset 0x46000)

assetFileDescriptorList: List<AssetFileDescriptor>,
filePath: String? = null
) {
zimFileReader = runBlocking {
zimFileReader = withContext(Dispatchers.IO) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the compilation error in the ZimReaderContainer class.

// top margin for kiwix main app.
setTopMarginToWebViews(0)
selectTab(currentWebViewIndex)
lifecycleScope.launch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is some delay in the creation of the zimFileReader object e.g. for bigger zim files then previously saved tabs are lost.

You can reproduce it, by adding some delay in the creation of the ZimFileReader object.

@@ -1721,7 +1733,7 @@ abstract class CoreReaderFragment :
closeAllTabsButton?.isClickable = isClickable
}

private fun restoreDeletedTabs() {
private suspend fun restoreDeletedTabs() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a leg while restoring all tabs if closed(which is obvious since it is preparing the zimFileReader on the IO thread). But the user is not aware of it so we should show some progress/something so that the user can know about what is going on. Also, if we move to any other fragment while in this process then the application crashes.

XRecorder_16072024_143650.mp4
FATAL EXCEPTION: main
                                                                                                    Process: org.kiwix.kiwixmobile, PID: 18742
                                                                                                    java.util.NoSuchElementException: List is empty.
                                                                                                    	at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:418)
                                                                                                    	at org.kiwix.kiwixmobile.core.main.CoreReaderFragment.restoreDeletedTabs(CoreReaderFragment.kt:1746)
                                                                                                    	at org.kiwix.kiwixmobile.core.main.CoreReaderFragment.access$restoreDeletedTabs(CoreReaderFragment.kt:177)
                                                                                                    	at org.kiwix.kiwixmobile.core.main.CoreReaderFragment$restoreDeletedTabs$1.invokeSuspend(Unknown Source:18)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:808)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:101)
                                                                                                    	at android.os.Looper.loop(Looper.java:166)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:7425)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

editor.putString(TAG_CURRENT_POSITIONS, "$positions")
editor.putInt(TAG_CURRENT_TAB, 0)
editor.apply()
dialogScope.launch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a delay in showing the notes dialog, if we open the note from Notes Screen(if note's zim file is not currently set in the zimReaderContainer).

@CalebKL
Copy link
Contributor Author

CalebKL commented Jul 18, 2024

@MohitMaliFtechiz When I switch to different zimFiles, I get this crash after numerous attempts.
I am not sure if its the same crash you were referring to earlier.

Cmdline: org.kiwix.kiwixmobile
2024-07-18 12:44:02.286  5611-5611  DEBUG                   pid-5611                             A  pid: 5387, tid: 5606, name: DefaultDispatch  >>> org.kiwix.kiwixmobile <<<
2024-07-18 12:44:02.286  5611-5611  DEBUG                   pid-5611                             A        #00 pc 0000000000010630  /data/app/~~2cz1tgEo0aQgZshuQoQHsg==/org.kiwix.kiwixmobile-ZpQbVVYb8qkV5KrowJ_Pfg==/base.apk!libzim_wrapper.so (offset 0x1b2a000) (Java_org_kiwix_libzim_Archive_getEntryByPath__Ljava_lang_String_2+120) (BuildId: bb96395f5068f1755a93f5daa08b7a934333f07e)
2024-07-18 12:44:02.286  5611-5611  DEBUG                   pid-5611                             A        #02 pc 00000000020ad728  /memfd:jit-cache (deleted) (offset 0x2000000) (org.kiwix.kiwixmobile.core.reader.ZimFileReader.getItem+376)
2024-07-18 12:44:02.286  5611-5611  DEBUG                   pid-5611                             A        #03 pc 000000000214569c  /memfd:jit-cache (deleted) (offset 0x2000000) (org.kiwix.kiwixmobile.core.reader.ZimFileReader.loadContent+204)
2024-07-18 12:44:02.286  5611-5611  DEBUG                   pid-5611                             A        #08 pc 0000000000008cd0  /data/data/org.kiwix.kiwixmobile/code_cache/.overlay/base.apk/classes13.dex (org.kiwix.kiwixmobile.core.reader.ZimFileReader.access$loadContent+0)

@MohitMaliFtechiz
Copy link
Collaborator

@CalebKL No it is not that crash i have uploaded the crash logs in the review comment.

Also, now on my device, it is crashing immediately when I switch to other zim files(I am using the old version of Android for testing Android 8).

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 11992 (DefaultDispatch)
2024-07-18 16:33:50.291 12077-12077 DEBUG                   pid-12077                            A  pid: 11734, tid: 11992, name: DefaultDispatch  >>> org.kiwix.kiwixmobile <<<
2024-07-18 16:33:50.294 12077-12077 DEBUG                   pid-12077                            A      #00 pc 0000000000010630  /data/app/org.kiwix.kiwixmobile-YaobaHgoDzW2aibsfWri3g==/base.apk (offset 0x71f000)
2024-07-18 16:33:50.294 12077-12077 DEBUG                   pid-12077                            A      #01 pc 000000000005be38  /data/app/org.kiwix.kiwixmobile-YaobaHgoDzW2aibsfWri3g==/oat/arm64/base.odex (offset 0x46000)

In the CoreReaderFragment there is a restore tabs functionality also for opening the previous zim file and restoring the previous tabs, and their article opens in the webView(if we are not opening the new zim file).

Currently, it is somehow restoring the previous zim file and tabs when we open the new file. However, it should not do it according to our openPageInBookFromNavigationArguments method's logic. Can you please check this? It is the main reason for these crashes.

@MohitMaliFtechiz
Copy link
Collaborator

@MohitMaliFtechiz When I switch to different zimFiles, I get this crash after numerous attempts.

This crash is newer, on which device you are testing.

@CalebKL CalebKL marked this pull request as draft July 18, 2024 11:40
@CalebKL
Copy link
Contributor Author

CalebKL commented Jul 18, 2024

@CalebKL No it is not that crash i have uploaded the crash logs in the review comment.

Also, now on my device, it is crashing immediately when I switch to other zim files(I am using the old version of Android for testing Android 8).

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 11992 (DefaultDispatch)
2024-07-18 16:33:50.291 12077-12077 DEBUG                   pid-12077                            A  pid: 11734, tid: 11992, name: DefaultDispatch  >>> org.kiwix.kiwixmobile <<<
2024-07-18 16:33:50.294 12077-12077 DEBUG                   pid-12077                            A      #00 pc 0000000000010630  /data/app/org.kiwix.kiwixmobile-YaobaHgoDzW2aibsfWri3g==/base.apk (offset 0x71f000)
2024-07-18 16:33:50.294 12077-12077 DEBUG                   pid-12077                            A      #01 pc 000000000005be38  /data/app/org.kiwix.kiwixmobile-YaobaHgoDzW2aibsfWri3g==/oat/arm64/base.odex (offset 0x46000)

In the CoreReaderFragment there is a restore tabs functionality also for opening the previous zim file and restoring the previous tabs, and their article opens in the webView(if we are not opening the new zim file).

Currently, it is somehow restoring the previous zim file and tabs when we open the new file. However, it should not do it according to our openPageInBookFromNavigationArguments method's logic. Can you please check this? It is the main reason for these crashes.

let me look into this as well

@CalebKL
Copy link
Contributor Author

CalebKL commented Jul 18, 2024

@MohitMaliFtechiz When I switch to different zimFiles, I get this crash after numerous attempts.

This crash is newer, on which device you are testing.

I got this crash while testing on my emulator, Pixel 7 pro API34 and also on Samsung S22+

@MohitMaliFtechiz
Copy link
Collaborator

@CalebKL Thanks, it is a native crash in libzim, but currently, our PR has many bugs and workarounds that can lead to this issue, so probably first we have to fix these things. @mgautierfr can have a better idea about this error when it occurs. Might be the data is corrupted at some point due to these scenarios.

@kelson42
Copy link
Collaborator

Any news here?

@CalebKL
Copy link
Contributor Author

CalebKL commented Jul 26, 2024

Any news here?

There are some fixes that I am working on from @MohitMaliFtechiz reviews. I will complete it ASAP, although this PR will need to wait until the native error(3956) causing the crash is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

org.kiwix.kiwixmobile.core.reader.ZimReaderContainer.setZimFile Input dispatching timed out
3 participants