aaronpk / Overland-iOS

📌 GPS logger for iOS devices
https://overland.p3k.app
Apache License 2.0
599 stars 93 forks source link

Fix crash when ending a trip #148

Closed aaronpk closed 6 months ago

aaronpk commented 8 months ago

This has happened to me and at least one other beta tester. No data is lost, but the app quits and you have to open it again to end the trip.

aaronpk commented 6 months ago

From the crash logs:

Thread 0 name:
Thread 0 Crashed:
0   libsqlite3.dylib                0x00000001d17e9ce0 sqlite3WhereBegin + 1568 (sqlite3.c:173465)
1   libsqlite3.dylib                0x00000001d17e2b50 sqlite3Select + 14644 (sqlite3.c:156203)
2   libsqlite3.dylib                0x00000001d17ceb88 yy_reduce + 1852 (sqlite3.c:181198)
3   libsqlite3.dylib                0x00000001d17cd714 sqlite3RunParser + 996 (sqlite3.c:183939)
4   libsqlite3.dylib                0x00000001d17ccdbc sqlite3Prepare + 420 (sqlite3.c:148143)
5   libsqlite3.dylib                0x00000001d17cca88 sqlite3LockAndPrepare + 224 (sqlite3.c:148221)
6   Overland                        0x00000001002329b4 -[_LOLDatabaseAccessor initWithDatabase:collection:] + 384 (LOLDatabase.m:95)
7   Overland                        0x000000010023279c -[LOLDatabase accessCollection:withBlock:] + 76 (LOLDatabase.m:54)
8   Overland                        0x0000000100229368 -[GLManager numberOfLocationsInQueue:] + 132 (GLManager.m:351)
9   Overland                        0x0000000100231448 -[TrackingViewController refreshView] + 1704 (TrackingViewController.m:289)
10  Foundation                      0x00000001a7ad7d8c __NSFireTimer + 96 (NSTimer.m:280)
11  CoreFoundation                  0x00000001a84a33ec __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32 (CFRunLoop.c:1797)
12  CoreFoundation                  0x00000001a84a3094 __CFRunLoopDoTimer + 1004 (CFRunLoop.c:2404)
13  CoreFoundation                  0x00000001a842cc84 __CFRunLoopDoTimers + 288 (CFRunLoop.c:2562)
14  CoreFoundation                  0x00000001a8429c9c __CFRunLoopRun + 1856 (CFRunLoop.c:3122)
15  CoreFoundation                  0x00000001a8429478 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
16  GraphicsServices                0x00000001eb9aa4f8 GSEventRunModal + 164 (GSEvent.c:2196)
17  UIKitCore                       0x00000001aa84d62c -[UIApplication _run] + 888 (UIApplication.m:3685)
18  UIKitCore                       0x00000001aa84cc68 UIApplicationMain + 340 (UIApplication.m:5270)
19  Overland                        0x000000010023413c main + 80 (main.m:14)
20  dyld                            0x00000001cb162dcc start + 2240 (dyldMain.cpp:1269)

Thread 3 name:
Thread 3:
0   libsystem_kernel.dylib          0x00000001efb6298c fsync + 8 (:-1)
1   libsqlite3.dylib                0x00000001d18107ec unixSync + 212 (sqlite3.c:44568)
2   libsqlite3.dylib                0x00000001d18197e0 syncJournal + 496 (sqlite3.c:68032)
3   libsqlite3.dylib                0x00000001d18103ac sqlite3PagerCommitPhaseOne + 392 (sqlite3.c:70315)
4   libsqlite3.dylib                0x00000001d1800bbc sqlite3BtreeCommitPhaseOne + 172 (sqlite3.c:80893)
5   libsqlite3.dylib                0x00000001d1800784 vdbeCommit + 1136 (sqlite3.c:93911)
6   libsqlite3.dylib                0x00000001d17d9898 sqlite3VdbeHalt + 1352 (sqlite3.c:94321)
7   libsqlite3.dylib                0x00000001d17fab8c sqlite3VdbeExec + 43532 (sqlite3.c:103464)
8   libsqlite3.dylib                0x00000001d17ef900 sqlite3_step + 964 (sqlite3.c:97535)
9   libsqlite3.dylib                0x00000001d17cc624 sqlite3_exec + 368 (sqlite3.c:141865)
10  Overland                        0x0000000100232d14 -[_LOLDatabaseAccessor done] + 108 (LOLDatabase.m:149)
11  Overland                        0x00000001002327c8 -[LOLDatabase accessCollection:withBlock:] + 120 (LOLDatabase.m:56)
12  Overland                        0x000000010022b0f8 -[GLManager writeTripToDB:steps:] + 116 (GLManager.m:739)
13  Overland                        0x000000010022ae80 __34-[GLManager endTripFromAutopause:]_block_invoke + 112 (GLManager.m:700)
14  CoreMotion                      0x00000001b53f59b8 __61-[CMPedometerProxy _handleQueryResponse:onQueue:withHandler:]_block_invoke_2 + 52 (CMPedometer.mm:392)
15  libdispatch.dylib               0x00000001b03eb6a8 _dispatch_call_block_and_release + 32 (init.c:1530)
16  libdispatch.dylib               0x00000001b03ed300 _dispatch_client_callout + 20 (object.m:561)
17  libdispatch.dylib               0x00000001b03f4894 _dispatch_lane_serial_drain + 748 (queue.c:3885)
18  libdispatch.dylib               0x00000001b03f53c4 _dispatch_lane_invoke + 380 (queue.c:3976)
19  libdispatch.dylib               0x00000001b0400004 _dispatch_root_queue_drain_deferred_wlh + 288 (queue.c:6913)
20  libdispatch.dylib               0x00000001b03ff878 _dispatch_workloop_worker_thread + 404 (queue.c:6507)
21  libsystem_pthread.dylib         0x0000000212417964 _pthread_wqthread + 288 (pthread.c:2629)
22  libsystem_pthread.dylib         0x0000000212417a04 start_wqthread + 8 (:-1)

It looks like the crash is because two separate threads are accessing the sqlite DB at the same time. One thread is writing the trip to the DB, the other is reading the DB to update the UI.

aaronpk commented 6 months ago

Another crash report:

Thread 0 name:
Thread 0 Crashed:
0   libsqlite3.dylib                0x00000001c66f4dc8 sqlite3VdbeExec + 19528 (sqlite3.c:105869)
1   libsqlite3.dylib                0x00000001c66ef900 sqlite3_step + 964 (sqlite3.c:97535)
2   Overland                        0x000000010277ebfc -[FMResultSet nextWithError:] + 40 (FMResultSet.m:165)
3   Overland                        0x000000010275681c -[GLManager currentTripDistance] + 244 (GLManager.m:621)
4   Overland                        0x000000010275dcd0 -[TrackingViewController updateTripState] + 724 (TrackingViewController.m:365)
5   UIKitCore                       0x000000019fa29398 -[UIApplication sendAction:to:from:forEvent:] + 100 (UIApplication.m:5583)
6   UIKitCore                       0x000000019f3cbce8 -[UIControl sendAction:to:forEvent:] + 112 (UIControl.m:909)
7   UIKitCore                       0x000000019f3cc060 -[UIControl _sendActionsForEvents:withEvent:] + 324 (UIControl.m:980)
8   UIKitCore                       0x000000019f3c9684 -[UIButton _sendActionsForEvents:withEvent:] + 124 (UIButton.m:4160)
9   UIKitCore                       0x000000019f3cb190 -[UIControl touchesEnded:withEvent:] + 400 (UIControl.m:672)
10  UIKitCore                       0x000000019ee18554 -[UIWindow _sendTouchesForEvent:] + 776 (UIWindow.m:3319)
11  UIKitCore                       0x000000019ee17ef8 -[UIWindow sendEvent:] + 3204 (UIWindow.m:3641)
12  UIKitCore                       0x000000019ee17178 -[UIApplication sendEvent:] + 560 (UIApplication.m:12641)
13  UIKitCore                       0x000000019edd9ab4 __dispatchPreprocessedEventFromEventQueue + 6492 (UIEventDispatcher.m:2571)
14  UIKitCore                       0x000000019edd7db8 __processEventQueue + 5544 (UIEventDispatcher.m:2913)
15  UIKitCore                       0x000000019eeaac34 __eventFetcherSourceCallback + 160 (UIEventDispatcher.m:2944)
16  CoreFoundation                  0x000000019ca1911c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CFRunLoop.c:1957)
17  CoreFoundation                  0x000000019ca18398 __CFRunLoopDoSource0 + 176 (CFRunLoop.c:2001)
18  CoreFoundation                  0x000000019ca16b4c __CFRunLoopDoSources0 + 244 (CFRunLoop.c:2038)
19  CoreFoundation                  0x000000019ca15888 __CFRunLoopRun + 828 (CFRunLoop.c:2955)
20  CoreFoundation                  0x000000019ca15468 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
21  GraphicsServices                0x00000001e08964f8 GSEventRunModal + 164 (GSEvent.c:2196)
22  UIKitCore                       0x000000019ee42004 -[UIApplication _run] + 888 (UIApplication.m:3685)
23  UIKitCore                       0x000000019ee41640 UIApplicationMain + 340 (UIApplication.m:5270)
24  Overland                        0x000000010276013c main + 80 (main.m:14)
25  dyld                            0x00000001c0066dcc start + 2240 (dyldMain.cpp:1269)

Thread 4 name:
Thread 4:
0   libsystem_kernel.dylib          0x00000001e4a21a74 stat + 8
1   libsqlite3.dylib                0x00000001c6723fd4 unixFileControl + 844 (sqlite3.c:45832)
2   libsqlite3.dylib                0x00000001c6715f08 sqlite3PagerClose + 292 (sqlite3.c:67861)
3   libsqlite3.dylib                0x00000001c671598c sqlite3BtreeClose + 356 (sqlite3.c:79558)
4   libsqlite3.dylib                0x00000001c6737b08 sqlite3LeaveMutexAndCloseZombie + 252 (sqlite3.c:186081)
5   libsqlite3.dylib                0x00000001c6741b88 sqlite3Close + 780 (sqlite3.c:185986)
6   Overland                        0x0000000102778bd8 -[FMDatabase close] + 48 (FMDatabase.m:245)
7   Overland                        0x0000000102757194 -[GLManager writeTripToDB:steps:] + 272 (GLManager.m:777)
8   Overland                        0x0000000102756e80 __34-[GLManager endTripFromAutopause:]_block_invoke + 112 (GLManager.m:700)
9   CoreMotion                      0x00000001a9b09708 __61-[CMPedometerProxy _handleQueryResponse:onQueue:withHandler:]_block_invoke_2 + 52 (CMPedometer.mm:392)
10  libdispatch.dylib               0x00000001a49e06a8 _dispatch_call_block_and_release + 32 (init.c:1530)
11  libdispatch.dylib               0x00000001a49e2300 _dispatch_client_callout + 20 (object.m:561)
12  libdispatch.dylib               0x00000001a49e9894 _dispatch_lane_serial_drain + 748 (queue.c:3885)
13  libdispatch.dylib               0x00000001a49ea3c4 _dispatch_lane_invoke + 380 (queue.c:3976)
14  libdispatch.dylib               0x00000001a49f5004 _dispatch_root_queue_drain_deferred_wlh + 288 (queue.c:6913)
15  libdispatch.dylib               0x00000001a49f4878 _dispatch_workloop_worker_thread + 404 (queue.c:6507)
16  libsystem_pthread.dylib         0x000000020721f964 _pthread_wqthread + 288 (pthread.c:2629)
17  libsystem_pthread.dylib         0x000000020721fa04 start_wqthread + 8
aaronpk commented 6 months ago

Opening the sqlite DB in serialized mode seems to be the simplest fix: https://www.sqlite.org/threadsafe.html