kiwix-android icon indicating copy to clipboard operation
kiwix-android copied to clipboard

Task/anr fix zimfile reader

Open CalebKL opened this issue 1 year ago • 8 comments

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

CalebKL avatar Jul 15 '24 15:07 CalebKL

@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)

CalebKL avatar Jul 18 '24 10:07 CalebKL

@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 avatar Jul 18 '24 11:07 MohitMaliFtechiz

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

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

MohitMaliFtechiz avatar Jul 18 '24 11:07 MohitMaliFtechiz

@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 avatar Jul 18 '24 11:07 CalebKL

@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+

CalebKL avatar Jul 18 '24 11:07 CalebKL

@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.

MohitMaliFtechiz avatar Jul 18 '24 12:07 MohitMaliFtechiz

Any news here?

kelson42 avatar Jul 26 '24 10:07 kelson42

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.

CalebKL avatar Jul 26 '24 10:07 CalebKL

@CalebKL I have found some serious issues in our implementation, especially when working with Coroutines(in a multithreading environment). With the main thread, there are no issues because the implementation was made according to the main thread. But now we are moving to use the Coroutines in our code. The issue we are facing in this PR is related to https://github.com/kiwix/kiwix-android/issues/4104 which I have already fixed in https://github.com/kiwix/kiwix-android/pull/4105.

In the meantime, we have also improved our code. So now, we have a better coroutine implementation to create an archive object on the IO thread. Also, we have improved the UI side to properly show users the progress when the archive object is in the creation process: https://github.com/kiwix/kiwix-android/pull/4074.

So now, you can start working on my review comments.

MohitMaliFtechiz avatar Nov 22 '24 11:11 MohitMaliFtechiz

@MohitMaliFtechiz Can you please complete PR

kelson42 avatar Dec 10 '24 05:12 kelson42

@kelson42, @CalebKL has already started working on this issue. So let him complete this PR.

MohitMaliFtechiz avatar Dec 10 '24 06:12 MohitMaliFtechiz

5 months without activity, this is too long by far, it should be completed.

kelson42 avatar Dec 11 '24 05:12 kelson42

5 months without activity, this is too long by far, it should be completed.

Completing this ASAP

CalebKL avatar Dec 11 '24 06:12 CalebKL

@MohitMaliFtechiz Please complete PR

kelson42 avatar Jan 18 '25 07:01 kelson42

@CalebKL I have made the necessary changes, you can test and give your feedback.

MohitMaliFtechiz avatar Jan 21 '25 17:01 MohitMaliFtechiz

@MohitMaliFtechiz Please rebase

kelson42 avatar Jan 26 '25 21:01 kelson42

Codecov Report

Attention: Patch coverage is 64.70588% with 12 lines in your changes missing coverage. Please review.

Project coverage is 56.73%. Comparing base (948b8cc) to head (053b6e0). Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
.../kiwix/kiwixmobile/core/main/CoreReaderFragment.kt 50.00% 7 Missing and 3 partials :warning:
...java/org/kiwix/kiwixmobile/core/StorageObserver.kt 80.00% 2 Missing :warning:
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3937      +/-   ##
============================================
+ Coverage     56.68%   56.73%   +0.04%     
- Complexity     1527     1532       +5     
============================================
  Files           313      313              
  Lines         13490    13510      +20     
  Branches       1693     1695       +2     
============================================
+ Hits           7647     7665      +18     
- Misses         4678     4681       +3     
+ Partials       1165     1164       -1     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jan 27 '25 07:01 codecov[bot]

@kelson42 Done.

MohitMaliFtechiz avatar Jan 27 '25 09:01 MohitMaliFtechiz