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

Merged
merged 9 commits into from
Jan 27, 2025
Merged

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)
  • Removed the unnecessary creation of ZimFileReader when opening notes on the Notes screen. Refactored AddNoteDialog to use database values for performing note-related operations (e.g., view, edit, delete notes) instead of setting and using ZimFileReader.
  • Improved the tab closing and restoring process. Previously, when the user closed all tabs, the ZimFileReader was set to null. If the user restored the tabs, a new ZimFileReader was created, which was a resource-intensive operation, especially for large ZIM files.
  • Now, the ZimFileReader is not set to null while the "restore tab" snackbar is visible, allowing users to restore tabs without recreating the ZimFileReader.
  • Once the snackbar is dismissed, the ZimFileReader is set to null to free up resources since it is no longer required.
  • Made the convertToBookOnDisk method suspend, as it creates the ZimFileReader object to convert the files into Books.

By doing the above changes now our UI is working flawlessly as there is no extra ZilFileReader creation happens. See the below video.

screen-20250121-171439.mp4

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

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

@MohitMaliFtechiz
Copy link
Collaborator

MohitMaliFtechiz commented Nov 22, 2024

@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 #4104 which I have already fixed in #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: #4074.

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

@kelson42
Copy link
Collaborator

@MohitMaliFtechiz Can you please complete PR

@MohitMaliFtechiz
Copy link
Collaborator

MohitMaliFtechiz commented Dec 10, 2024

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

@kelson42
Copy link
Collaborator

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

@CalebKL
Copy link
Contributor Author

CalebKL commented Dec 11, 2024

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

Completing this ASAP

@CalebKL CalebKL mentioned this pull request Dec 11, 2024
@kelson42
Copy link
Collaborator

@MohitMaliFtechiz Please complete PR

@MohitMaliFtechiz
Copy link
Collaborator

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

@kelson42
Copy link
Collaborator

@MohitMaliFtechiz Please rebase

…restoring them.

* Improved the tab closing and restoring process. Previously, when the user closed all tabs, the `ZimFileReader` was set to null. If the user restored the tabs, a new `ZimFileReader` was created, which was a resource-intensive operation, especially for large ZIM files.
* Now, the `ZimFileReader` is not set to null while the "restore tab" snackbar is visible, allowing users to restore tabs without recreating the ZimFileReader.
* Once the snackbar is dismissed, the `ZimFileReader` is set to null to free up resources since it is no longer required.
…in note screen since now we are not creating the ZimFileReader on note screen.
…eReader object to convert the files into Books.

* Refactored the `StorageObserverTest` to align with this new change.
@MohitMaliFtechiz MohitMaliFtechiz force-pushed the task/anr-fix-zimfile-reader branch from d3c7656 to 053b6e0 Compare January 27, 2025 06:53
Copy link

codecov bot commented Jan 27, 2025

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 ⚠️
...java/org/kiwix/kiwixmobile/core/StorageObserver.kt 80.00% 2 Missing ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MohitMaliFtechiz
Copy link
Collaborator

@kelson42 Done.

@kelson42 kelson42 merged commit 8bd2393 into kiwix:main Jan 27, 2025
26 of 27 checks passed
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
4 participants