Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on [FMDatabaseQueue inDatabase:] #547

Open
dabing1022 opened this issue Nov 8, 2016 · 5 comments
Open

Crash on [FMDatabaseQueue inDatabase:] #547

dabing1022 opened this issue Nov 8, 2016 · 5 comments

Comments

@dabing1022
Copy link

dabing1022 commented Nov 8, 2016

#14 Thread
SIGSEGV
SEGV_ACCERR
0 libsqlite3.dylib	sqlite3_log + 43412
1 libsqlite3.dylib	sqlite3_log + 43284
2 libsqlite3.dylib	sqlite3_log + 37456
3 libsqlite3.dylib	sqlite3_rekey + 115140
4 libsqlite3.dylib	sqlite3_log + 7904
5 libsqlite3.dylib	sqlite3_exec + 10664
6 libsqlite3.dylib	sqlite3_exec + 8960
7 libsqlite3.dylib	sqlite3_exec + 5424
8 libsqlite3.dylib	sqlite3_exec + 2852
9 libsqlite3.dylib	sqlite3_exec + 2008
10 Demo	-**[FMDatabase executeQuery:withArgumentsInArray:orDictionary:orVAList:]** (FMDatabase.m:761)
11 Demo	-**[FMDatabase executeQuery:]** (FMDatabase.m:885)
12 Demo	__48-[IMDataBaseTableProxy excuteQuery:resultBlock:]_block_invoke (IMDataBaseTableProxy.m:368)
13 Demo	__30-**[FMDatabaseQueue inDatabase:]**_block_invoke (FMDatabaseQueue.m:152)
14 libdispatch.dylib	__dispatch_client_callout + 16
15 libdispatch.dylib	__dispatch_barrier_sync_f_invoke + 84
16 Demo	-**[FMDatabaseQueue inDatabase:]** (FMDatabaseQueue.m:166)
···
23 libdispatch.dylib	__dispatch_call_block_and_release + 24
24 libdispatch.dylib	__dispatch_client_callout + 16
25 libdispatch.dylib	__dispatch_queue_override_invoke + 732
26 libdispatch.dylib	__dispatch_root_queue_drain + 572
27 libdispatch.dylib	__dispatch_worker_thread3 + 124
28 libsystem_pthread.dylib	_pthread_wqthread + 1288
#0 Thread
}
0 libsystem_kernel.dylib	0x000000018261816c mach_msg_trap + 8
1 libsystem_kernel.dylib	0x0000000182617fdc mach_msg + 72
2 CoreFoundation	0x0000000183613cec ___CFRunLoopServiceMachPort + 192
3 CoreFoundation	0x0000000183611908 ___CFRunLoopRun + 1132
4 CoreFoundation	0x0000000183540048 CFRunLoopRunSpecific + 444
5 GraphicsServices	0x0000000184fc6198 GSEventRunModal + 180
6 UIKit	0x000000018952c2fc -[UIApplication _run] + 684
7 UIKit	0x0000000189527034 UIApplicationMain + 208
8 Demo	0x00000001004ec280 main (main.m:14) 
9 libdyld.dylib	0x00000001825245b8 _start + 4
#1 Thread
}
0 libsystem_kernel.dylib	0x0000000182636a88 __workq_kernreturn + 8
1 libsystem_pthread.dylib	0x00000001826fa35c _pthread_wqthread + 1452
#2 Thread
}
0 libsystem_kernel.dylib	0x0000000182636a88 __workq_kernreturn + 8
1 libsystem_pthread.dylib	0x00000001826fa178 _pthread_wqthread + 968
#3 Thread
}
0 libsystem_kernel.dylib	0x0000000182636a88 __workq_kernreturn + 8
1 libsystem_pthread.dylib	0x00000001826fa35c _pthread_wqthread + 1452
#4 com.apple.uikit.eventfetch-thread
}
0 libsystem_kernel.dylib	0x000000018261816c mach_msg_trap + 8
1 libsystem_kernel.dylib	0x0000000182617fdc mach_msg + 72
2 CoreFoundation	0x0000000183613cec ___CFRunLoopServiceMachPort + 192
3 CoreFoundation	0x0000000183611908 ___CFRunLoopRun + 1132
4 CoreFoundation	0x0000000183540048 CFRunLoopRunSpecific + 444
5 Foundation	0x0000000184051a8c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
6 Foundation	0x000000018407257c -[NSRunLoop(NSRunLoop) runUntilDate:] + 96
7 UIKit	0x0000000189ea1a2c -[UIEventFetcher threadMain] + 136
8 Foundation	0x000000018414f47c ___NSThread__start__ + 1024
9 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
10 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#5 Thread
}
0 libsystem_kernel.dylib	0x0000000182636314 __semwait_signal + 8
1 libsystem_c.dylib	0x000000018255423c nanosleep + 212
2 libc++.1.dylib	0x0000000182065994 _ZNSt3__111this_thread9sleep_forERKNS_6chrono8durationIxNS_5ratioILl1ELl1000000000EEEEE + 80
3 JavaScriptCore	0x0000000187df2384 void std::__1::this_thread::sleep_for<long long, std::__1::ratio<1l, 1000l> >(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> > const&) + 76
4 JavaScriptCore	0x0000000187df1484 bmalloc::waitUntilFalse(std::__1::unique_lock<bmalloc::StaticMutex>&, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >, bool&) + 128
5 JavaScriptCore	0x0000000187df12c0 bmalloc::Heap::scavenge(std::__1::unique_lock<bmalloc::StaticMutex>&, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> >) + 60
6 JavaScriptCore	0x0000000187df1160 bmalloc::Heap::concurrentScavenge() + 80
7 JavaScriptCore	0x0000000187df2470 bmalloc::AsyncTask<bmalloc::Heap, void (bmalloc::Heap::*)()>::threadRunLoop() + 92
8 JavaScriptCore	0x0000000187df239c std::__1::__shared_ptr_emplace<std::__1::mutex, std::__1::allocator<std::__1::mutex> >::~__shared_ptr_emplace() 
9 JavaScriptCore	0x0000000187df264c void* std::__1::__thread_proxy<std::__1::tuple<void (*)(bmalloc::AsyncTask<bmalloc::Heap, void (bmalloc::Heap::*)()>*), bmalloc::AsyncTask<bmalloc::Heap, void (bmalloc::Heap::*)()>*> >(void*) + 92
10 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
11 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#6 Thread
}
0 libsystem_kernel.dylib	0x0000000182636a88 __workq_kernreturn + 8
1 libsystem_pthread.dylib	0x00000001826fa178 _pthread_wqthread + 968
#7 Thread
}
0 libsystem_kernel.dylib	0x000000018261a428 fsync + 8
1 Foundation	0x0000000184067c24 __NSWriteDataToFileWithExtendedAttributes + 692
2 Demo	0x0000000100c785f8 ___77-[BLYDataManager persistData:ofType:enableNotifications:withCompletionBlock:]_block_invoke + 56
3 Foundation	0x000000018412a754 ___NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
4 Foundation	0x000000018406f2c8 -[NSBlockOperation main] + 96
5 Foundation	0x000000018405f8c4 -[__NSOperationInternal _start:] + 620
6 Foundation	0x000000018412cb00 ___NSOQSchedule_f + 228
7 libdispatch.dylib	0x00000001824f11c0 __dispatch_client_callout + 16
8 libdispatch.dylib	0x00000001824ff444 __dispatch_queue_serial_drain + 928
9 libdispatch.dylib	0x00000001824f49a8 __dispatch_queue_invoke + 652
10 libdispatch.dylib	0x000000018250138c __dispatch_root_queue_drain + 572
11 libdispatch.dylib	0x00000001825010ec __dispatch_worker_thread3 + 124
12 libsystem_pthread.dylib	0x00000001826fa2b8 _pthread_wqthread + 1288
#8 com.apple.NSURLConnectionLoader
}
0 libsystem_kernel.dylib	0x000000018261816c mach_msg_trap + 8
1 libsystem_kernel.dylib	0x0000000182617fdc mach_msg + 72
2 CoreFoundation	0x0000000183613cec ___CFRunLoopServiceMachPort + 192
3 CoreFoundation	0x0000000183611908 ___CFRunLoopRun + 1132
4 CoreFoundation	0x0000000183540048 CFRunLoopRunSpecific + 444
5 CFNetwork	0x0000000183d31fd0 +[NSURLConnection(Loader) _resourceLoadLoop:] + 336
6 Foundation	0x000000018414f47c ___NSThread__start__ + 1024
7 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
8 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#9 Thread
}
0 libsystem_kernel.dylib	0x0000000182636a88 __workq_kernreturn + 8
1 libsystem_pthread.dylib	0x00000001826fa35c _pthread_wqthread + 1452
#10 MainRunloopMonitor
}
0 libsystem_kernel.dylib	0x00000001826181c0 semaphore_timedwait_trap + 8
1 libdispatch.dylib	0x0000000182503784 __dispatch_semaphore_wait_slow + 112
2 Demo	0x0000000100c7eb4c -[BLYMainRunloopMonitorManager monitorThreadRun] + 172
3 Foundation	0x000000018414f47c ___NSThread__start__ + 1024
4 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
5 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#11 AFNetworking
}
0 libsystem_kernel.dylib	0x000000018261816c mach_msg_trap + 8
1 libsystem_kernel.dylib	0x0000000182617fdc mach_msg + 72
2 CoreFoundation	0x0000000183613cec ___CFRunLoopServiceMachPort + 192
3 CoreFoundation	0x0000000183611908 ___CFRunLoopRun + 1132
4 CoreFoundation	0x0000000183540048 CFRunLoopRunSpecific + 444
5 Foundation	0x0000000184051a8c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
6 Foundation	0x00000001840a6210 -[NSRunLoop(NSRunLoop) run] + 88
7 Demo	0x000000010066ef84 +[AFURLConnectionOperation networkRequestThreadEntryPoint:] (AFURLConnectionOperation.m:176) 
8 Foundation	0x000000018414f47c ___NSThread__start__ + 1024
9 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
10 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#12 Thread
}
0 libsystem_kernel.dylib	0x0000000182636a88 __workq_kernreturn + 8
1 libsystem_pthread.dylib	0x00000001826fa35c _pthread_wqthread + 1452
#13 Thread
}
0 libsystem_kernel.dylib	0x0000000182619530 pread + 8
1 libsqlite3.dylib	0x0000000183be7954 sqlite3_rekey + 4324
2 libsqlite3.dylib	0x0000000183b56ff4 sqlite3_snprintf + 14468
3 libsqlite3.dylib	0x0000000183b78b84 sqlite3_log + 3656
4 libsqlite3.dylib	0x0000000183bda520 sqlite3_backup_step + 3780
5 libsqlite3.dylib	0x0000000183bf62a4 sqlite3_rekey + 64052
6 libsqlite3.dylib	0x0000000183ba9160 sqlite3_get_table + 12880
7 libsqlite3.dylib	0x0000000183bf5c80 sqlite3_rekey + 62480
8 libsqlite3.dylib	0x0000000183b97904 sqlite3_step + 37256
9 libsqlite3.dylib	0x0000000183b8e9d4 sqlite3_step + 600
10 Demo	0x0000000100ab5ccc -[FMResultSet nextWithError:] (FMResultSet.m:155) 
···
13 Demo	0x0000000100ab4dec __30-**[FMDatabaseQueue inDatabase:]**_block_invoke (FMDatabaseQueue.m:152) 
14 libdispatch.dylib	0x00000001824f11c0 __dispatch_client_callout + 16
15 libdispatch.dylib	0x00000001824fe860 __dispatch_barrier_sync_f_invoke + 84
16 Demo	0x0000000100ab4d5c -**[FMDatabaseQueue inDatabase:]** (FMDatabaseQueue.m:166) 
···
21 libdispatch.dylib	0x00000001824f1200 __dispatch_call_block_and_release + 24
22 libdispatch.dylib	0x00000001824f11c0 __dispatch_client_callout + 16
23 libdispatch.dylib	0x00000001824ffab4 __dispatch_queue_override_invoke + 732
24 libdispatch.dylib	0x000000018250138c __dispatch_root_queue_drain + 572
25 libdispatch.dylib	0x00000001825010ec __dispatch_worker_thread3 + 124
26 libsystem_pthread.dylib	0x00000001826fa2b8 _pthread_wqthread + 1288
#15 WebThread
}
0 libsystem_kernel.dylib	0x000000018261816c mach_msg_trap + 8
1 libsystem_kernel.dylib	0x0000000182617fdc mach_msg + 72
2 CoreFoundation	0x0000000183613cec ___CFRunLoopServiceMachPort + 192
3 CoreFoundation	0x0000000183611908 ___CFRunLoopRun + 1132
4 CoreFoundation	0x0000000183540048 CFRunLoopRunSpecific + 444
5 WebCore	0x00000001880f045c RunWebThread(void*) + 456
6 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
7 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#16 Thread
}
0 libsystem_kernel.dylib	0x0000000182635e1c __psynch_cvwait + 8
1 libsystem_pthread.dylib	0x00000001826fb9c0 __pthread_cond_wait + 640
2 Foundation	0x0000000184128d6c -[__NSOperationInternal _waitUntilFinished:] + 132
3 Foundation	0x00000001840bb4b4 -[NSOperationQueue waitUntilAllOperationsAreFinished] + 264
···
9 Foundation	0x000000018405f8c4 -[__NSOperationInternal _start:] + 620
10 Foundation	0x000000018412cb00 ___NSOQSchedule_f + 228
11 libdispatch.dylib	0x00000001824f11c0 __dispatch_client_callout + 16
12 libdispatch.dylib	0x00000001824ff444 __dispatch_queue_serial_drain + 928
13 libdispatch.dylib	0x00000001824f49a8 __dispatch_queue_invoke + 652
14 libdispatch.dylib	0x000000018250138c __dispatch_root_queue_drain + 572
15 libdispatch.dylib	0x00000001825010ec __dispatch_worker_thread3 + 124
16 libsystem_pthread.dylib	0x00000001826fa2b8 _pthread_wqthread + 1288
#17 Thread
}
0 libsystem_kernel.dylib	0x00000001826368e8 __ulock_wait + 8
1 libdispatch.dylib	0x00000001825040d8 __dispatch_ulock_wait + 48
2 libdispatch.dylib	0x0000000182504200 __dispatch_thread_event_wait_slow + 36
3 libdispatch.dylib	0x0000000182501df8 __dispatch_barrier_sync_f_slow + 236
4 �Demo	0x0000000100ab4d5c -**[FMDatabaseQueue inDatabase:]** (FMDatabaseQueue.m:166) 
···
10 libdispatch.dylib	0x00000001824f1200 __dispatch_call_block_and_release + 24
11 libdispatch.dylib	0x00000001824f11c0 __dispatch_client_callout + 16
12 libdispatch.dylib	0x00000001824ffab4 __dispatch_queue_override_invoke + 732
13 libdispatch.dylib	0x000000018250138c __dispatch_root_queue_drain + 572
14 libdispatch.dylib	0x00000001825010ec __dispatch_worker_thread3 + 124
15 libsystem_pthread.dylib	0x00000001826fa2b8 _pthread_wqthread + 1288
#18 GCDAsyncSocket-CFStream
}
0 libsystem_kernel.dylib	0x000000018261816c mach_msg_trap + 8
1 libsystem_kernel.dylib	0x0000000182617fdc mach_msg + 72
2 CoreFoundation	0x0000000183613cec ___CFRunLoopServiceMachPort + 192
3 CoreFoundation	0x0000000183611908 ___CFRunLoopRun + 1132
4 CoreFoundation	0x0000000183540048 CFRunLoopRunSpecific + 444
5 Foundation	0x0000000184051a8c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
6 Demo	0x00000001006ee518 +[GCDAsyncSocket cfstreamThread] (GCDAsyncSocket.m:6907) 
7 Foundation	0x000000018414f47c ___NSThread__start__ + 1024
8 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
9 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#19 com.apple.CFSocket.private
}
0 libsystem_kernel.dylib	0x000000018263623c __select + 8
1 CoreFoundation	0x000000018361ab84 ___CFSocketManager + 640
2 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
3 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#20 JIT Worklist Worker Thread
}
0 libsystem_kernel.dylib	0x0000000182635e1c __psynch_cvwait + 8
1 libsystem_pthread.dylib	0x00000001826fb9c0 __pthread_cond_wait + 640
2 libc++.1.dylib	0x00000001820253ec _ZNSt3__118condition_variable4waitERNS_11unique_lockINS_5mutexEEE + 56
3 JavaScriptCore	0x0000000187ddae58 _ZN3WTF10ParkingLot21parkConditionallyImplEPKvRKNS_12ScopedLambdaIFbvEEERKNS3_IFvvEEENSt3__16chrono10time_pointINSD_12steady_clockENSD_8durationIxNSC_5ratioILl1ELl1000000000EEEEEEE + 2016
4 JavaScriptCore	0x0000000187bb36fc JSC::JITWorklist::runThread() + 192
5 JavaScriptCore	0x0000000187bb3b4c WTF::Vector<WTF::RefPtr<JSC::JITWorklist::Plan>, 32ul, WTF::CrashOnOverflow, 16ul>::expandCapacity(unsigned long, WTF::RefPtr<JSC::JITWorklist::Plan>*) 
6 JavaScriptCore	0x0000000187524dac WTF::threadEntryPoint(void*) + 212
7 JavaScriptCore	0x0000000187524cbc WTF::wtfThreadEntryPoint(void*) + 24
8 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
9 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body 
#21 WTF Parallel Helper Thread
}
0 libsystem_kernel.dylib	0x0000000182635e1c __psynch_cvwait + 8
1 libsystem_pthread.dylib	0x00000001826fb9c0 __pthread_cond_wait + 640
2 libc++.1.dylib	0x00000001820253ec _ZNSt3__118condition_variable4waitERNS_11unique_lockINS_5mutexEEE + 56
3 JavaScriptCore	0x0000000187ddae58 _ZN3WTF10ParkingLot21parkConditionallyImplEPKvRKNS_12ScopedLambdaIFbvEEERKNS3_IFvvEEENSt3__16chrono10time_pointINSD_12steady_clockENSD_8durationIxNSC_5ratioILl1ELl1000000000EEEEEEE + 2016
4 JavaScriptCore	0x0000000187dda450 WTF::ParallelHelperPool::waitForClientWithTask(WTF::Locker<WTF::LockBase> const&) + 288
5 JavaScriptCore	0x0000000187dda1f0 WTF::ParallelHelperPool::helperThreadBody() + 76
6 JavaScriptCore	0x0000000187524dac WTF::threadEntryPoint(void*) + 212
7 JavaScriptCore	0x0000000187524cbc WTF::wtfThreadEntryPoint(void*) + 24
8 libsystem_pthread.dylib	0x00000001826fc850 __pthread_body + 240
9 libsystem_pthread.dylib	0x00000001826fc760 __pthread_body

FMDB Version: 2.5.0
I'm sorry that I cannot reproduce the bug on Xcode or on my test devices, this bug is logged from Bugly SDK and about 1200 users met this bug.

Any ideas, guys?

@dabing1022
Copy link
Author

dabing1022 commented Nov 8, 2016

I know

Using a single instance of FMDatabase from multiple threads at once is a bad idea. It has always been OK to make a FMDatabase object per thread. Just don't share a single instance across threads, and definitely not across multiple threads at the same time. Bad things will eventually happen and you'll eventually get something to crash, or maybe get an exception, or maybe meteorites will fall out of the sky and hit your Mac Pro. This would suck.

So don't instantiate a single FMDatabase object and use it across multiple threads.

Instantiate a single FMDatabaseQueue and use it across multiple threads. The FMDatabaseQueue object will synchronize and coordinate access across the multiple threads

I followed this rule to code, but it's strange to crash because of this bug. Any suggestions will be nice 👍

@ccgus
Copy link
Owner

ccgus commented Nov 8, 2016

Thread 14 and 13 are both doing rekeys at the same time- are you using two FMDatabaseQueues? You should be using a single one to access a single database.

@billymeltdown
Copy link

@dabing1022 just so you're aware we (Zetetic) have been discouraging the use of sqlite3_rekey in favor of attaching a new database with the desired encryption options and using sqlcipher_export() to migrate the contents and schema of the original db into the new one: https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/

TL;DR

$ ./sqlcipher plaintext.db
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'newkey';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;

@groue
Copy link

groue commented Nov 8, 2016

@billymeltdown A side question for you: what is the difference between sqlite3_rekey, sqlcipher_export, and the Online Backup API? It looks like they can all provide the same result. And yet some sqlite3_rekey is discouraged, sqlcipher_export is encouraged, and sqlite3_backup_init is not even mentioned anywhere in the SQLCipher documentation (despite its high affordance). It would be nice if you (Zetetic) would be clearer on the subject. We (library developers) need to know the consequence when we provide APIs around SQLCipher.

@dabing1022
Copy link
Author

@ccgus Maybe there are two FMDatabaseQueues, I'll check out my codes. Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants