matomo-org / matomo-sdk-android

SDK for Android to measure your apps with Matomo. Works on Android phones, tablets, Fire TV sticks, and more!
BSD 3-Clause "New" or "Revised" License
388 stars 162 forks source link

EventDiskCache.writeEventFile fails if user clears cache on app while running #311

Closed kevinslashslash closed 3 years ago

kevinslashslash commented 3 years ago

Steps to reproduce 1) Enter airplane mode 2) Open Matomo Example App 3) Track Screen View 4) Press the home button At this point the piwik_cache directory is created and an event is stored there 5) App Info for Matomo Example App and Clear Cache 6) Reenter the Matomo Example app 7) Track Screen View 8) While watching the logcat press the home button

 E  java.io.FileNotFoundException: /data/user/0/org.matomo.demo/cache/piwik_cache/example.com/events_1626142384160: open failed: ENOENT (No such file or directory)
E      at libcore.io.IoBridge.open(IoBridge.java:497)
E      at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
E      at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
E      at java.io.FileWriter.<init>(FileWriter.java:90)
E      at org.matomo.sdk.dispatcher.EventDiskCache.writeEventFile(EventDiskCache.java:201)
E      at org.matomo.sdk.dispatcher.EventDiskCache.cache(EventDiskCache.java:116)
E      at org.matomo.sdk.dispatcher.EventCache.updateState(EventCache.java:51)
E      at org.matomo.sdk.dispatcher.DefaultDispatcher$1.run(DefaultDispatcher.java:201)
E      at java.lang.Thread.run(Thread.java:920)
E  Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
E      at libcore.io.Linux.open(Native Method)
E      at libcore.io.ForwardingOs.open(ForwardingOs.java:239)
E      at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273)
E      at libcore.io.ForwardingOs.open(ForwardingOs.java:239)
E      at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7682)
E      at libcore.io.IoBridge.open(IoBridge.java:483)
E      ... 8 more

The directory is created when EventDiskCache is instantiated but since clearing an app cache can be done without restarting the app it's not enough. I propose calling mCacheDir.mkdirs() in writeEventFile

d4rken commented 3 years ago

Good catch, was fixed by #312.