hpoul / file_picker_writable

Flutter plugin to choose files which can be read, referenced and written back at a later time.
https://pub.dev/packages/file_picker_writable
MIT License
17 stars 13 forks source link

Unhandled exception when reading file on Android #6

Closed amake closed 4 years ago

amake commented 4 years ago

I had a crash report in Google Play Console caused by file_picker_writable trying to read a non-existent file (see https://github.com/amake/orgro/issues/10).

The trace was

java.io.FileNotFoundException:
  at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel (DatabaseUtils.java:149)
  at android.content.ContentProviderProxy.openTypedAssetFile (ContentProviderProxy.java:705)
  at android.content.ContentResolver.openTypedAssetFileDescriptor (ContentResolver.java:1694)
  at android.content.ContentResolver.openAssetFileDescriptor (ContentResolver.java:1510)
  at android.content.ContentResolver.openInputStream (ContentResolver.java:1194)
  at codeux.design.filepicker.file_picker_writable.FilePickerWritableImpl$copyContentUriAndReturnFileInfo$2.invokeSuspend (FilePickerWritableImpl.java:154)
  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (BaseContinuationImpl.java:8)
  at kotlinx.coroutines.DispatchedTask.run (DispatchedTask.java:93)
  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.java)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask (CoroutineScheduler.java:14)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker (CoroutineScheduler.java:28)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.java)

Unfortunately the coroutines ruin the trace, so I can't see how exactly execution got to FilePickerWritableImpl#copyContentUriAndReturnFileInfo. But there is some code path that doesn't properly catch exceptions.

I think one problem might be that you use launch to start coroutines in several places, but have the try/catch outside of launch. Uncaught exceptions in launched coroutines aren't forwarded to the parent scope and will crash the app on Android (link).

hpoul commented 4 years ago

You are right about launch.. no idea why i missed that.. although there are still two launch calls in FilePickerWritablePlugin they don't look very dangerous.. thanks for your PR #7 .. i improved a bit upon it, by simply moving the launch one level up so there is still just one try/catch.. i think this should accomplish about the same: https://github.com/hpoul/file_picker_writable/pull/8/files - any thoughts?

amake commented 4 years ago

Looks good to me. I'll close my PR then.

amake commented 4 years ago

This was addressed by #8