Ixam97 / CarStatsViewer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: Concurrent Modification Exception when writing trip data

Ixam97 opened this issue · comments

02.06.2024 10:33:05.642 E: [NEO] Car Stats Viewer has crashed!
 java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.next(ArrayList.java:860)
	at com.ixam97.carStatsViewer.dataProcessor.DataProcessor$updateDrivingDataPoint$1.invokeSuspend(DataProcessor.kt:851)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@661e7de, Dispatchers.IO]
02.06.2024 20:18:31.008 E: FATAL ERROR! Writing trips was not successful: java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.next(ArrayList.java:860)
	at com.ixam97.carStatsViewer.dataProcessor.DataProcessor.writeTripsToDatabase(DataProcessor.kt:861)
	at com.ixam97.carStatsViewer.dataProcessor.DataProcessor.access$writeTripsToDatabase(DataProcessor.kt:36)
	at com.ixam97.carStatsViewer.dataProcessor.DataProcessor$updateDrivingDataPoint$1.invokeSuspend(DataProcessor.kt:517)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)

I have made some small code changes with 53ac6f0 in an attempt to fix this issue, in the hopes this fix does not create other issues. Some users are heavily affected by this and can't even launch the app because of this crash.

Feedback on this issue suggests that it may be caused by very large distances in a single trip. Currently I have no way to validate this, but I'll keep an eye on this and think about some possible optimizations in this regard.

To limit the effects of long trips, I have limited the number of driving points loaded by default to 2000 in 498e41e. This should improve performance when it comes to adding new data to a trip while driving by reducing the data that has to be copied.

This is a quick improvement, but taking a closer look at the current way ongoing trips are handled it's clear that this is quite cumbersome and error prone. Perspectively I will replace the list of sessions with a class that contains them and makes it more robust. #231 Should also profit from this.