soupslurpr / BeauTyXT

A beautiful, private, secure, and minimalistic Text, Markdown, and Typst editor.
https://beautyxt.app
ISC License
76 stars 6 forks source link

App crashes upon opening a Typst project folder #149

Open lrq3000 opened 1 month ago

lrq3000 commented 1 month ago

After working beautifully well for a couple of hours, the app started crashing every time I tried to open a Typst project folder.

I know a bit about Android development so I tried to debug with logcat but so far the only thing I can find is that this issue seems to be caused by a DeadObjectException caused by the rustService being called when it is not properly loaded. But this happens right away. I tried to modify the code to ensure it gets loaded, but this does not change anything (maybe I'm doing it wrong, I don't know anything about Rust).

Here is the full trace:

@workspace This app is an Android editor for Typst projects, a new kind of Markdown flavored markup language. When I try to open a Typst folder, the app crashes with the following error, help me debug it. Consider also that the app initially worked fine, but later started to crash with this error, even when the target folder is totally empty, whereas initially it would work fine.

2024-05-19 19:11:25.360 6403-6403 DEBUG pid-6403 A pid: 6310, tid: 6336, name: Binder:6310_3 >>> dev.soupslurpr.beautyxt:typst_project_view_model_rust_library_isolated_process:dev.soupslurpr.beau <<< 2024-05-19 19:11:26.050 5710-5710 AndroidRuntime pid-5710 E FATAL EXCEPTION: main Process: dev.soupslurpr.beautyxt, PID: 5710 android.os.DeadObjectException at android.os.BinderProxy.transactNative(Native Method) at android.os.BinderProxy.transact(BinderProxy.java:532) at D2.O.k(Unknown Source:17) at I2.a0.u(Unknown Source:7) at Q2.a.n(Unknown Source:7) at h3.F.run(Unknown Source:114) at h3.P.T(Unknown Source:23) at m3.a.d(Unknown Source:152) at h3.z.W(Unknown Source:11) at h3.a.i0(Unknown Source:80) at W2.j.m(Unknown Source:29) at I2.b0.onServiceConnected(Unknown Source:58) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2154) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2186) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8393) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) Suppressed: m3.f: [o0{Cancelling}@57b1494, Dispatchers.Main.immediate]

Thank you for making this awesome tool, I hope it can work on my device.

lrq3000 commented 1 month ago

Ok I think I know the culprit, it's the scoped storage management. I'm not entirely sure, but what I'm sure is that now I can systematically reproduce the exception, and also the app working fine, by doing the following:

  1. first, click on Create, then select Typst project, then create a new folder, then select it as the project's root. Now, the project works.
  2. close the app, and reopen it. Now click on Open, Typst Project, and then select the same folder you just created: it should work fine.
  3. Now close the app, reopen it, then click on Create, Typst project, and create a new folder somewhere else.
  4. Now if you try to Open, Typst project and reopen the 2nd folder, it will work fine. But if you try to reopen the first folder we created earlier, it will crash.

This is typical of an issue with fetching the permissions to access and modify files with scoped storage. I may be able to help but your codebase is hard for me to understand because of the Rust calls and background process.

I suggest to use the SimpleStorage library to avoid such tricky errors, that's what I did to improve some other projects in the past such as Super Productivity.

lrq3000 commented 1 month ago

Bingo, this was the issue! I'm making a PR right now.

soupslurpr commented 1 month ago

Odd, I can't reproduce this issue with the steps you provided. Could you inform me of the device you're using and OS?

lrq3000 commented 1 month ago

Android 10, the SAF show these issues only from Android 10 (SDK version 29) and up because Google made it much more difficult to use it and much less permissive. Normally you should be able to reproduce it in the Android Studio emulator using Android 10 or above.

soupslurpr commented 1 month ago

@lrq3000 I'm using the emulator using the "Pixel 7 API 34" device and it's still not working, I made a screen recording and posted to the YouTube video linked below just in case I'm doing something wrong.

https://youtu.be/C5eV1zPnfIQ

lrq3000 commented 1 month ago

Thank you for posting the video, so my instructions seem to be wrong. I'm not sure under which conditions the app can lose the permissions to a folder it created, but it happened randomly to me just by waiting less than 24h.

Instead, try the following:

This should normally crash.

lrq3000 commented 1 month ago

I just tested on Android 14 API 34 (Pixel 8) in an emulator, if you try to access a folder you did not create with the app, you either get asked again to select a folder, or you get a crash, like on Android 10.

To do that, I created a folder (typsttest) using the native Files app, then created a markdown file using BeauTyXT or another text editor outside of the folder I created, then copied it into the folder (so that it doesn't have a lock on it), then rename it to be main.typ , then try to open the folder typsttest.

(In a previous message, I stated that an error message was displayed, but that's not the case, the message was just shown on any non user folders in the folder picker dialog, which is irrelevant for this issue).

lrq3000 commented 1 month ago

Update: my PR does not fix the crash on Android 14. Something must have changed in the SAF... bummer. I'm going to try to fix that.

lrq3000 commented 1 month ago

Update2: in fact it's not an issue with my PR but with my emulator/compilation: since I reused the libraries you provided instead of compiling my own, it seems I can only target ARM_64, not x86_64 (which is what the emulator uses). I tried running ARM_64 but it is extremely slow and does not boot up because of the lack of Intel HAXM. The alternative is to use Genymotion which works fine with ARM_64 images, but it currently is incompatible with the latest version of Android Studio...

TL;DR: I can only run my APK on ARM64, so I can't test in emulators. You will have to test my PR please, or wait a month when I'll have a bit more time to try to compile everything. I believe my PR works fine.

soupslurpr commented 1 month ago

@lrq3000 It seems UPX breaks the x86_64 library (in emulators only?), I think I'll have to stop using it since it isn't very reliable. Try skipping compressing the library with UPX.

I pushed a commit to remove the usage of UPX in useful_commands.txt.

lrq3000 commented 1 month ago

Oh i didn't even know it was possible to compress Android libraries with UPX! I will do that once i will recompile the libs.

On my real device, I cannot reproduce the error. It's the same as in the emulator (deadobject) but I can't reproduce it whereas I could before. Maybe it's because my OS remembers the permissions I gave.

Note that the error I opened this issue here for started before I started coding anything, I in fact fired up Android Studio to try to debug why the app was suddenly not working anymore.

I highly doubt I will be the only one to run into the issue. I'll keep the original app for now to see if it happees again.

22 mai 2024 20:29:18 soupslurpr @.***>:

@lrq3000[https://github.com/lrq3000] It seems UPX breaks the x86_64 library, I think I'll have to stop using it since it isn't very reliable. Try skipping compressing the library with UPX.

— Reply to this email directly, view it on GitHub[https://github.com/soupslurpr/BeauTyXT/issues/149#issuecomment-2125484552], or unsubscribe[https://github.com/notifications/unsubscribe-auth/AAIRFXT7TN2KMVLR3DLMCKTZDTPX5AVCNFSM6AAAAABH6YH62GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRVGQ4DINJVGI]. You are receiving this because you were mentioned. [Image de pistage][https://github.com/notifications/beacon/AAIRFXTEZYDHVM73DXQKI3DZDTPX5A5CNFSM6AAAAABH6YH62GWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTT6WBJAQ.gif]

soupslurpr commented 1 month ago

Could you tell me the specific device or specific OS (like Samsung One UI 5.0 or Google Pixel 8 Android 14 etc)

lrq3000 commented 1 month ago

It's a Huawei P40 Pro Plus with Android 10. But I think this is not specific to my phone, as I think I cannot reproduce anymore because now the app has write permissions that the OS remembers. I will test with another phone later on which I never installed BeauTyXT but I am too busy currently, I'll follow up in about two weeks (from the 17th June onward).

If I cannot reproduce the issue then, I will modify my PR to extract the other features I implemented (eg, background preview refresh in another thread, etc).

soupslurpr commented 1 month ago

The background preview refresh doesn't work like you might think, it still blocks other functions in the Rust library. It only makes it asynchronous in Kotlin but it gets blocked when calling another Rust function anyways. So when trying to type text it still blocks it. It has no effect.

The proper way to implement it would be to make it async on the Rust side. Which I'm not sure can be done with an isolatedProcess service. I haven't tried.

However, I don't think it's needed at all if refreshing only the page(s) you actually see in the preview render is something viable to implement.

lrq3000 commented 1 month ago

Did you try my build? Because for me it works fine, the preview is indeed refreshed in a non blocking way.

I have a a 4 pages document with 2 includes and it is extremely slow, i think this can be considered a small document.

soupslurpr commented 1 month ago

I tried the latest build posted on your fork, typing out the characters is still blocked on the preview refreshing.

lrq3000 commented 1 month ago

That is very strange. Are you still using the same emulator setup you cited above? I will try to recompile according to your updated instructions and see if there is maybe a difference due to Android OS versions.

soupslurpr commented 1 month ago

I'm using a real device, the Pixel 7 on GrapheneOS (Android 14)

lrq3000 commented 1 month ago

Ok thank you, i will try to make an emulator although sometimes there are differences with real devices, but at leart it will be more reproducible (as I am also using a real device since I could not run in the emulator before )

I'll keep you updated from the 17th june onward.

lrq3000 commented 2 weeks ago

Just to let you know that I will only be able to restart working on this in mid/end of July onward, as I have an urgent project to finish for my work before.

I remain committed to help in developing BeauTyXT as it will be my main tool to write my PhD thesis (I am already using it for this purpose!). So I will continue adding features/improving stuff along the way during the whole upcoming academic year.

soupslurpr commented 2 weeks ago

Thank you, but in the current implementation of those PRs, they will probably not be accepted.

I can provide specific details once you ask about some certain features and what they need to achieve to be accepted, okay?