ccgus / fmdb

A Cocoa / Objective-C wrapper around SQLite
Other
13.84k stars 2.76k forks source link

FMDBQueue and SQLite Database is locked #444

Closed nawar closed 8 years ago

nawar commented 8 years ago

I've been having this issue with FMDBQueue dispatch_sync(_queue) queue as it is not respecting the FIFO order that is supposed to be having and allowing a write from a different thread, to have a lock on the database while a another write, from another different thread, will be getting the "Database is locked" error.

Example:

Thread 23: executeUpdate() ==> (FMDBDatabaseBusyHandler) Thread 19: executeUpdate() ==> (Can't access the database because it is locked)

I am using FMDB user version 2.5 and I am using inDatabase to run the block of the SQL needed to be executed.

So I am wondering why the serial queue of FMDBQueue is not allowing Thread 23 to finish then allow Thread 19 to finish next. I am not sure if this is an issue with the implementation.

ccgus commented 8 years ago

Can you show us all the stack traces?

nawar commented 8 years ago

Of course. Here's the backtrace which I pulled right before the break point at this section

    - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args {
      .
      . 
      .
       // wtf?
   if (_logsErrors) {  // ====> Break Point
        NSLog(@"Unknown error calling sqlite3_step (%d: %s) eu", rc, sqlite3_errmsg(_db));
        NSLog(@"DB Query: %@", sql);
    }

Here's the trace (bt in lldb):

     thread #1: tid = 0x26d042, 0x38d63fbc libsystem_kernel.dylib`mach_msg_trap + 20, queue = 'com.apple.main-thread'
    frame #0: 0x38d63fbc libsystem_kernel.dylib`mach_msg_trap + 20
    frame #1: 0x38d63dbc libsystem_kernel.dylib`mach_msg + 40
    frame #2: 0x26e5c48c CoreFoundation`__CFRunLoopServiceMachPort + 136
    frame #3: 0x26e5a812 CoreFoundation`__CFRunLoopRun + 1050
    frame #4: 0x26dad0d8 CoreFoundation`CFRunLoopRunSpecific + 516
    frame #5: 0x26dacecc CoreFoundation`CFRunLoopRunInMode + 108
    frame #6: 0x30122af8 GraphicsServices`GSEventRunModal + 160
    frame #7: 0x2b0362dc UIKit`UIApplicationMain + 144
    frame #8: 0x0013458c Yako`main + 164 at AppDelegate.swift:44

  thread #2: tid = 0x26d094, 0x38d79320 libsystem_kernel.dylib`kevent_qos + 24, queue = 'com.apple.libdispatch-manager'
    frame #0: 0x38d79320 libsystem_kernel.dylib`kevent_qos + 24
    frame #1: 0x016e45f6 libdispatch.dylib`_dispatch_mgr_invoke + 254
    frame #2: 0x016d59ce libdispatch.dylib`_dispatch_mgr_thread + 38

  thread #10: tid = 0x26d0b6, 0x38d63fbc libsystem_kernel.dylib`mach_msg_trap + 20, name = 'com.apple.NSURLConnectionLoader'
    frame #0: 0x38d63fbc libsystem_kernel.dylib`mach_msg_trap + 20
    frame #1: 0x38d63dbc libsystem_kernel.dylib`mach_msg + 40
    frame #2: 0x26e5c48c CoreFoundation`__CFRunLoopServiceMachPort + 136
    frame #3: 0x26e5a812 CoreFoundation`__CFRunLoopRun + 1050
    frame #4: 0x26dad0d8 CoreFoundation`CFRunLoopRunSpecific + 516
    frame #5: 0x26dacecc CoreFoundation`CFRunLoopRunInMode + 108
    frame #6: 0x26719d9e CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 486
    frame #7: 0x27c7036c Foundation`__NSThread__start__ + 1144
    frame #8: 0x38e16c7e libsystem_pthread.dylib`_pthread_body + 138
    frame #9: 0x38e16bf2 libsystem_pthread.dylib`_pthread_start + 110
    frame #10: 0x38e14a08 libsystem_pthread.dylib`thread_start + 8

  thread #11: tid = 0x26d0bf, 0x38d77f14 libsystem_kernel.dylib`__select + 20, name = 'com.apple.CFSocket.private'
    frame #0: 0x38d77f14 libsystem_kernel.dylib`__select + 20
    frame #1: 0x26e61930 CoreFoundation`__CFSocketManager + 572
    frame #2: 0x38e16c7e libsystem_pthread.dylib`_pthread_body + 138
    frame #3: 0x38e16bf2 libsystem_pthread.dylib`_pthread_start + 110
    frame #4: 0x38e14a08 libsystem_pthread.dylib`thread_start + 8

  thread #16: tid = 0x26d1da, 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #0: 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x38e14e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
    frame #2: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #17: tid = 0x26d1de, 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #0: 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x38e14e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
    frame #2: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

* thread #18: tid = 0x26d1e2, 0x000b8636 Yako`-[FMDatabase executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:](self=0x15f50a90, _cmd="executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:", sql=0x15d12c50, outErr=0x00000000, arrayArgs=0x15de7500, dictionaryArgs=0x00000000, args=0x00000000) + 2802 at FMDatabase.m:1057, queue = 'fmdb.<FMDatabaseQueue: 0x15f9e640>'
  * frame #0: 0x000b8636 Yako`-[FMDatabase executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:](self=0x15f50a90, _cmd="executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:", sql=0x15d12c50, outErr=0x00000000, arrayArgs=0x15de7500, dictionaryArgs=0x00000000, args=0x00000000) + 2802 at FMDatabase.m:1057
    frame #1: 0x000b8ad2 Yako`-[FMDatabase executeUpdate:withArgumentsInArray:](self=0x15f50a90, _cmd="executeUpdate:withArgumentsInArray:", sql=0x15d12c50, arguments=0x15de7500) + 114 at FMDatabase.m:1113
    frame #2: 0x00198174 Yako`function signature specialization <Arg[3] = Dead, Arg[4] = Value Promoted from InOut, Arg[5] = Dead, Arg[6] = Value Promoted from InOut> of Yako.MasterModel.(db=0x15f50a90, success=false, sql="INSERT INTO comments (comment_msisdn,comment,status,ts,bid,cid,media_url) VALUES('16477000159','',7,'1449254871','228427','8728','[\"https://dc.yakoapp.com//static/img/img_5661dfd736974141161691e254986e10d780b160856aa21b8d40dd.jpg\"]')", values=7 values, testingLastId=nil, lastId=nil, type=Comments) -> (type : Yako.MasterModel.Tables, params : Swift.Dictionary<Swift.String, Swift.AnyObject>) -> Swift.Optional<Swift.Int>).(closure #1) + 648 at MasterModel.swift:265
    frame #3: 0x001920ec Yako`_TPA__TTSf2n_n_n_d_i_d_i_n_n_n_n_n___TFFC4Yako11MasterModel4saveFS0_FT4typeOS0_6Tables6paramsGVSs10DictionarySSPSs9AnyObject___GSqSi_U_FGSQCSo10FMDatabase_T_ + 352 at MasterModel.swift:0
    frame #4: 0x001822f0 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) + 20 at AddressBookModel.swift:0
    frame #5: 0x00192174 Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) + 76 at MasterModel.swift:0
    frame #6: 0x00182324 Yako`reabstraction thunk helper from @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) + 48 at AddressBookModel.swift:0
    frame #7: 0x00182390 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) + 100 at AddressBookModel.swift:0
    frame #8: 0x000af73a Yako`__30-[FMDatabaseQueue inDatabase:]_block_invoke(.block_descriptor=0x40194774) + 94 at FMDatabaseQueue.m:150
    frame #9: 0x016d3d02 libdispatch.dylib`_dispatch_client_callout + 22
    frame #10: 0x016dd4fa libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 102
    frame #11: 0x000af6b6 Yako`-[FMDatabaseQueue inDatabase:](self=0x15f9e640, _cmd="inDatabase:", block=(Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) at AddressBookModel.swift)) + 242 at FMDatabaseQueue.m:147
    frame #12: 0x00191bec Yako`Yako.MasterModel.save (type=Comments, params=Swift.Dictionary<Swift.String, AnyObject> @ 0x40194b48, self=0x15f6df60)(type : Yako.MasterModel.Tables, params : Swift.Dictionary<Swift.String, Swift.AnyObject>) -> Swift.Optional<Swift.Int> + 4616 at MasterModel.swift:277
    frame #13: 0x001d683c Yako`Yako.CommentsModel.save (bid="228427", cid="8728", comment="", commentMsisdn="16477000159", timestamp="1449254871", mediaUrl=Some, commentType=imageComment, nid=nil, self=0x15f6df60)(bid : Swift.Optional<Swift.String>, cid : Swift.String, comment : Swift.Optional<Swift.String>, commentMsisdn : Swift.String, timestamp : Swift.String, mediaUrl : Swift.Optional<Swift.String>, commentType : Yako.CommentsModel.commentType, nid : Swift.Optional<Swift.String>) -> () + 4492 at CommentsModel.swift:423
    frame #14: 0x001d33fc Yako`Yako.CommentsModel.process (json=Yako.JSON @ 0x40195e7c, self=0x15f6df60)(json : Yako.JSON) -> () + 10384 at CommentsModel.swift:191
    frame #15: 0x002f31f0 Yako`Yako.FeedModel.(response=0x15e07a90, self=0x15ef6560, commentsModel=0x15f6df60, contactsModel=0x15f9cde0) -> () -> ()).(closure #1) + 3900 at FeedModel.swift:221
    frame #16: 0x00181898 Yako`reabstraction thunk helper from @callee_owned (@owned Yako.HTTPResponse) -> (@unowned ()) to @callee_owned (@in Yako.HTTPResponse) -> (@out ()) + 20 at AddressBookModel.swift:0
    frame #17: 0x002d66c8 Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Yako.HTTPResponse) -> (@unowned ()) to @callee_owned (@in Yako.HTTPResponse) -> (@out ()) + 76 at FeedModel.swift:0
    frame #18: 0x000cb100 Yako`Yako.HTTPTask.(data=0x1704b180, response=0x17048870, error=nil, opt=0x15f7f2b0, failure=(Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned __ObjC.NSError, @owned Swift.Optional<Yako.HTTPResponse>) -> (@unowned ()) to @callee_owned (@in (__ObjC.NSError, Swift.Optional<Yako.HTTPResponse>)) -> (@out ()) at FeedModel.swift), self=0x15e4c080, success=(Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Yako.HTTPResponse) -> (@unowned ()) to @callee_owned (@in Yako.HTTPResponse) -> (@out ()) at FeedModel.swift)) -> (Swift.String, method : Yako.HTTPMethod, parameters : Swift.ImplicitlyUnwrappedOptional<Swift.Dictionary<Swift.String, Swift.AnyObject>>, success : Swift.ImplicitlyUnwrappedOptional<(Yako.HTTPResponse) -> ()>, failure : Swift.ImplicitlyUnwrappedOptional<(__ObjC.NSError, Swift.Optional<Yako.HTTPResponse>) -> ()>) -> Swift.Optional<Yako.HTTPOperation>).(closure #1) + 5764 at HTTPTask.swift:218
    frame #19: 0x000c4ac0 Yako`partial apply forwarder for Yako.HTTPTask.(create (Yako.HTTPTask) -> (Swift.String, method : Yako.HTTPMethod, parameters : Swift.ImplicitlyUnwrappedOptional<Swift.Dictionary<Swift.String, Swift.AnyObject>>, success : Swift.ImplicitlyUnwrappedOptional<(Yako.HTTPResponse) -> ()>, failure : Swift.ImplicitlyUnwrappedOptional<(__ObjC.NSError, Swift.Optional<Yako.HTTPResponse>) -> ()>) -> Swift.Optional<Yako.HTTPOperation>).(closure #1) + 264 at HTTPTask.swift:0
    frame #20: 0x000cb504 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.Optional<__ObjC.NSData>, @owned Swift.Optional<__ObjC.NSURLResponse>, @owned Swift.Optional<__ObjC.NSError>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.Optional<__ObjC.NSData>, @unowned Swift.Optional<__ObjC.NSURLResponse>, @unowned Swift.Optional<__ObjC.NSError>) -> (@unowned ()) + 188 at HTTPTask.swift:0
    frame #21: 0x266c05b4 CFNetwork`__75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 16
    frame #22: 0x266cfcd6 CFNetwork`__49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 278
    frame #23: 0x27c5856c Foundation`__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 8
    frame #24: 0x27bb9fde Foundation`-[NSBlockOperation main] + 146
    frame #25: 0x27bac3ce Foundation`-[__NSOperationInternal _start:] + 774
    frame #26: 0x27c5a82c Foundation`__NSOQSchedule_f + 192
    frame #27: 0x016de61a libdispatch.dylib`_dispatch_queue_drain + 2014
    frame #28: 0x016d6f52 libdispatch.dylib`_dispatch_queue_invoke + 282
    frame #29: 0x016dfb0e libdispatch.dylib`_dispatch_root_queue_drain + 426
    frame #30: 0x016df960 libdispatch.dylib`_dispatch_worker_thread3 + 100
    frame #31: 0x38e14e0c libsystem_pthread.dylib`_pthread_wqthread + 1024
    frame #32: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #19: tid = 0x26d20e, 0x008791ac libswiftCore.dylib`swift_dynamicCastUnknownClass, queue = 'fmdb.<FMDatabaseQueue: 0x15d9b0e0>'
    frame #0: 0x008791ac libswiftCore.dylib`swift_dynamicCastUnknownClass
    frame #1: 0x00879822 libswiftCore.dylib`swift_dynamicCast + 842
    frame #2: 0x0087a37a libswiftCore.dylib`_dynamicCastFromExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::ExistentialTypeMetadata const*, swift::Metadata const*, swift::DynamicCastFlags) + 186
    frame #3: 0x00879b28 libswiftCore.dylib`swift_dynamicCast + 1616
    frame #4: 0x002f0310 Yako`Yako.FeedModel.(db=0x15d9b290, posts=581 values) -> () -> Swift.Array<Yako.Post>).(closure #1) + 22648 at FeedModel.swift:163
    frame #5: 0x001822f0 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) + 20 at AddressBookModel.swift:0
    frame #6: 0x002d5c5c Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) + 76 at FeedModel.swift:0
    frame #7: 0x00182324 Yako`reabstraction thunk helper from @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) + 48 at AddressBookModel.swift:0
    frame #8: 0x00182390 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) + 100 at AddressBookModel.swift:0
    frame #9: 0x000af73a Yako`__30-[FMDatabaseQueue inDatabase:]_block_invoke(.block_descriptor=0x40218ae0) + 94 at FMDatabaseQueue.m:150
    frame #10: 0x016dd4fa libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 102
    frame #11: 0x000af6b6 Yako`-[FMDatabaseQueue inDatabase:](self=0x15d9b0e0, _cmd="inDatabase:", block=(Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) at AddressBookModel.swift)) + 242 at FMDatabaseQueue.m:147
    frame #12: 0x002d5a88 Yako`Yako.FeedModel.list (self=0x15d963f0)() -> Swift.Array<Yako.Post> + 700 at FeedModel.swift:180
    frame #13: 0x001c91a0 Yako`Yako.FeedTableViewController.(self=0x15e960d0) -> () -> ()).(closure #1) + 396 at FeedTableViewController.swift:298
    frame #14: 0x000fbc70 Yako`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @convention(block) () -> (@unowned ()) + 56 at NewPostViewController.swift:0
    frame #15: 0x016d3d16 libdispatch.dylib`_dispatch_call_block_and_release + 10
    frame #16: 0x016e006e libdispatch.dylib`_dispatch_root_queue_drain + 1802
    frame #17: 0x016df960 libdispatch.dylib`_dispatch_worker_thread3 + 100
    frame #18: 0x38e14e0c libsystem_pthread.dylib`_pthread_wqthread + 1024
    frame #19: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #20: tid = 0x26d20f, 0x38d78024 libsystem_kernel.dylib`__semwait_signal + 24, queue = 'fmdb.<FMDatabaseQueue: 0x15dc5c40>'
    frame #0: 0x38d78024 libsystem_kernel.dylib`__semwait_signal + 24
    frame #1: 0x38ccf91c libsystem_c.dylib`nanosleep + 172
    frame #2: 0x38ccf86c libsystem_c.dylib`usleep + 52
    frame #3: 0x38a54258 libsqlite3.dylib`___lldb_unnamed_function293$$libsqlite3.dylib + 12
    frame #4: 0x38a5bc94 libsqlite3.dylib`sqlite3_sleep + 92
    frame #5: 0x000b44f8 Yako`FMDBDatabaseBusyHandler(f=0x15d3b7a0, count=16) + 260 at FMDatabase.m:235
    frame #6: 0x38a6189e libsqlite3.dylib`___lldb_unnamed_function406$$libsqlite3.dylib + 26
    frame #7: 0x38a0f3c0 libsqlite3.dylib`___lldb_unnamed_function57$$libsqlite3.dylib + 132
    frame #8: 0x38a0ea98 libsqlite3.dylib`___lldb_unnamed_function56$$libsqlite3.dylib + 340
    frame #9: 0x38a233dc libsqlite3.dylib`___lldb_unnamed_function114$$libsqlite3.dylib + 3500
    frame #10: 0x38a21b48 libsqlite3.dylib`sqlite3_step + 472
    frame #11: 0x000b855a Yako`-[FMDatabase executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:](self=0x15d3b7a0, _cmd="executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:", sql=0x15f2c730, outErr=0x00000000, arrayArgs=0x00000000, dictionaryArgs=0x00000000, args=0x00000000) + 2582 at FMDatabase.m:1037
    frame #12: 0x000b8ad2 Yako`-[FMDatabase executeUpdate:withArgumentsInArray:](self=0x15d3b7a0, _cmd="executeUpdate:withArgumentsInArray:", sql=0x15f2c730, arguments=0x00000000) + 114 at FMDatabase.m:1113
    frame #13: 0x002f8900 Yako`Yako.FeedModel.(db=0x15d3b7a0, storedValues=2 values, rowid=1074) -> (rowid : Swift.Int) -> ()).(closure #3).(closure #1) + 1196 at FeedModel.swift:540
    frame #14: 0x001822f0 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) + 20 at AddressBookModel.swift:0
    frame #15: 0x002ea628 Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) with unmangled suffix "111" + 76 at FeedModel.swift:0
    frame #16: 0x00182324 Yako`reabstraction thunk helper from @callee_owned (@in Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@out ()) to @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) + 48 at AddressBookModel.swift:0
    frame #17: 0x00182390 Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) + 100 at AddressBookModel.swift:0
    frame #18: 0x000af73a Yako`__30-[FMDatabaseQueue inDatabase:]_block_invoke(.block_descriptor=0x4029a348) + 94 at FMDatabaseQueue.m:150
    frame #19: 0x016d3d02 libdispatch.dylib`_dispatch_client_callout + 22
    frame #20: 0x016dd4fa libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 102
    frame #21: 0x000af6b6 Yako`-[FMDatabaseQueue inDatabase:](self=0x15dc5c40, _cmd="inDatabase:", block=(Yako`reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) to @callee_unowned @convention(block) (@unowned Swift.ImplicitlyUnwrappedOptional<__ObjC.FMDatabase>) -> (@unowned ()) at AddressBookModel.swift)) + 242 at FMDatabaseQueue.m:147
    frame #22: 0x002f8110 Yako`Yako.FeedModel.(response=0x15e0a4b0, self=0x15ef6560, type=imagePost, rowid=1074) -> (rowid : Swift.Int) -> ()).(closure #3) + 5828 at FeedModel.swift:546
    frame #23: 0x002dde60 Yako`partial apply forwarder for Yako.FeedModel.(download (Yako.FeedModel) -> (rowid : Swift.Int) -> ()).(closure #3) + 116 at FeedModel.swift:0
    frame #24: 0x00181898 Yako`reabstraction thunk helper from @callee_owned (@owned Yako.HTTPResponse) -> (@unowned ()) to @callee_owned (@in Yako.HTTPResponse) -> (@out ()) + 20 at AddressBookModel.swift:0
    frame #25: 0x002ddee8 Yako`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned Yako.HTTPResponse) -> (@unowned ()) to @callee_owned (@in Yako.HTTPResponse) -> (@out ()) with unmangled suffix "49" + 76 at FeedModel.swift:0
    frame #26: 0x000c78e0 Yako`Yako.HTTPTask.URLSession (session=0x173573a0, downloadTask=0x15fd6080, location=0x15f4fd90, self=0x15f1a9d0)(Swift.ImplicitlyUnwrappedOptional<__ObjC.NSURLSession>, downloadTask : Swift.ImplicitlyUnwrappedOptional<__ObjC.NSURLSessionDownloadTask>, didFinishDownloadingToURL : Swift.ImplicitlyUnwrappedOptional<__ObjC.NSURL>) -> () + 5092 at HTTPTask.swift:519
    frame #27: 0x000c7a70 Yako`@objc Yako.HTTPTask.URLSession (Yako.HTTPTask)(Swift.ImplicitlyUnwrappedOptional<__ObjC.NSURLSession>, downloadTask : Swift.ImplicitlyUnwrappedOptional<__ObjC.NSURLSessionDownloadTask>, didFinishDownloadingToURL : Swift.ImplicitlyUnwrappedOptional<__ObjC.NSURL>) -> () + 192 at HTTPTask.swift:0
    frame #28: 0x2679e6c0 CFNetwork`__82-[NSURLSession delegate_downloadTask:didFinishDownloadingToURL:completionHandler:]_block_invoke + 36
    frame #29: 0x27c5856c Foundation`__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 8
    frame #30: 0x27bb9fde Foundation`-[NSBlockOperation main] + 146
    frame #31: 0x27bac3ce Foundation`-[__NSOperationInternal _start:] + 774
    frame #32: 0x27c5a82c Foundation`__NSOQSchedule_f + 192
    frame #33: 0x016de61a libdispatch.dylib`_dispatch_queue_drain + 2014
    frame #34: 0x016d6f52 libdispatch.dylib`_dispatch_queue_invoke + 282
    frame #35: 0x016dfb0e libdispatch.dylib`_dispatch_root_queue_drain + 426
    frame #36: 0x016df960 libdispatch.dylib`_dispatch_worker_thread3 + 100
    frame #37: 0x38e14e0c libsystem_pthread.dylib`_pthread_wqthread + 1024
    frame #38: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #21: tid = 0x26d210, 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #0: 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x38e14e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
    frame #2: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #22: tid = 0x26d21c, 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #0: 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x38e14e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
    frame #2: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #23: tid = 0x26d21e, 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #0: 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x38e14e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
    frame #2: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

  thread #24: tid = 0x26d227, 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #0: 0x38d7888c libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x38e14e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
    frame #2: 0x38e149fc libsystem_pthread.dylib`start_wqthread + 8

I also included this from the call to NSThread.callStackSymbols():

       <_NSCallStackArray 0x1670de60>(
            0   ???                                 0x037e7f44 0x0 + 58621764,
            1   Yako                                0x0018c4e8 main + 0,
            2   Yako                                0x00110ad3 -[FMDatabase executeUpdate:withArgumentsInArray:] + 114,
            3   Yako                                0x001f0174 _TTSf2n_n_n_d_i_d_i_n_n_n_n_n___TFFC4Yako11MasterModel4saveFS0_FT4typeOS0_6Tables6paramsGVSs10DictionarySSPSs9AnyObject___GSqSi_U_FGSQCSo10FMDatabase_T_ + 648,
            4   Yako                                0x001ea0ec _TPA__TTSf2n_n_n_d_i_d_i_n_n_n_n_n___TFFC4Yako11MasterModel4saveFS0_FT4typeOS0_6Tables6paramsGVSs10DictionarySSPSs9AnyObject___GSqSi_U_FGSQCSo10FMDatabase_T_ + 352,
            5   Yako                                0x001da2f0 _TTRXFo_oGSQCSo10FMDatabase__dT__XFo_iGSQS___iT__ + 20,
            6   Yako                                0x001ea174 _TPA__TTRXFo_oGSQCSo10FMDatabase__dT__XFo_iGSQS___iT__ + 76,
            7   Yako                                0x001da324 _TTRXFo_iGSQCSo10FMDatabase__iT__XFo_oGSQS___dT__ + 48,
            8   Yako                                0x001da390 _TTRXFo_oGSQCSo10FMDatabase__dT__XFdCb_dGSQS___dT__ + 100,
            9   Yako                                0x0010773b __30-[FMDatabaseQueue inDatabase:]_block_invoke + 94,
            10  libdispatch.dylib                   0x0172bd03 _dispatch_client_callout + 22,
            11  libdispatch.dylib                   0x017354fb _dispatch_barrier_sync_f_invoke + 102,
            12  Yako                                0x001076b7 -[FMDatabaseQueue inDatabase:] + 242,
            13  Yako                                0x001e9bec _TFC4Yako11MasterModel4savefS0_FT4typeOS0_6Tables6paramsGVSs10DictionarySSPSs9AnyObject___GSqSi_ + 4616,
            14  Yako                                0x0022ec34 _TFC4Yako13CommentsModel4savefS0_FT3bidGSqSS_3cidSS7commentGSqSS_13commentMsisdnSS9timestampSS8mediaUrlGSqSS_11commentTypeOS0_11commentType3nidGSqSS__T_ + 5508,
            15  Yako                                0x0022b3fc _TFC4Yako13CommentsModel7processfS0_FT4jsonVS_4JSON_T_ + 10384,
            16  Yako                                0x0034b1f0 _TFFC4Yako9FeedModel5fetchFS0_FT_T_U_FCS_12HTTPResponseT_ + 3900,
            17  Yako                                0x001d9898 _TTRXFo_oC4Yako12HTTPResponse_dT__XFo_iS0__iT__ + 20,
            18  Yako                                0x0032e6c8 _TPA__TTRXFo_oC4Yako12HTTPResponse_dT__XFo_iS0__iT__ + 76,
            19  Yako                                0x00123100 _TFFC4Yako8HTTPTask6createFS0_FTSS6methodOS_10HTTPMethod10parametersGSQGVSs10DictionarySSPSs9AnyObject___7successGSQFCS_12HTTPResponseT__7failureGSQFTCSo7NSErrorGSqS4___T___GSqCS_13HTTPOperation_U_FTGSqCSo6NSData_GSqCSo13NSURLResponse_GSqS5___T_ + 5764,
            20  Yako                                0x0011cac0 _TPA__TFFC4Yako8HTTPTask6createFS0_FTSS6methodOS_10HTTPMethod10parametersGSQGVSs10DictionarySSPSs9AnyObject___7successGSQFCS_12HTTPResponseT__7failureGSQFTCSo7NSErrorGSqS4___T___GSqCS_13HTTPOperation_U_FTGSqCSo6NSData_GSqCSo13NSURLResponse_GSqS5___T_ + 264,
            21  Yako                                0x00123504 _TTRXFo_oGSqCSo6NSData_oGSqCSo13NSURLResponse_oGSqCSo7NSError__dT__XFdCb_dGSqS__dGSqS0__dGSqS1___dT__ + 188,
            22  CFNetwork                           0x266c05b5 <redacted> + 16,
            23  CFNetwork                           0x266cfcd7 <redacted> + 278,
            24  Foundation                          0x27c5856d <redacted> + 8,
            25  Foundation                          0x27bb9fdf <redacted> + 146,
            26  Foundation                          0x27bac3cf <redacted> + 774,
            27  Foundation                          0x27c5a82d <redacted> + 192,
            28  libdispatch.dylib                   0x0173661b _dispatch_queue_drain + 2014,
            29  libdispatch.dylib                   0x0172ef53 _dispatch_queue_invoke + 282,
            30  libdispatch.dylib                   0x01737b0f _dispatch_root_queue_drain + 426,
            31  libdispatch.dylib                   0x01737961 _dispatch_worker_thread3 + 100,
            32  libsystem_pthread.dylib             0x38e14e0d _pthread_wqthread + 1024,
            33  libsystem_pthread.dylib             0x38e149fc start_wqthread + 8
            )

To shed more light on the issue. I usually have a thread inserting/updating the database while another thread downloads a file and when it finishes it tries to update the same database. Till now I had some luck with Transactions, however, it's only a work around because it retries from the journal.

ccgus commented 8 years ago

Can you paste all the backtraces? We need to see what every thread is doing.

nawar commented 8 years ago

Sorry, I did this now. Please check.

ccgus commented 8 years ago

Looks like you might have two FMDatabaseQueue instances- is that correct? If that's true, then you'll need to share a single one across threads for the locking not to happen.

nawar commented 8 years ago

I have one only which I created in a master model. This is got shared across the app.

 Class MasterModel {
   var dbQueue: FMDatabaseQueue?
   .
   . 
   init?() {

    // just let it be to create the folder        
    if dbQueue == nil {

        dbQueue = FMDatabaseQueue(path: dbDir?.path)

        if dbQueue == nil {
            DLog("Problem connecting to the db at:\( __FUNCTION__)")
            return nil
        } else {
           // DLog("Successfully connected to the db")
        }
    }

}
 }
ccgus commented 8 years ago

I'm not a swift expert- but don't these addresses indicate two instances of the db?

Yako.FeedModel.(db=0x15d9b290 Yako.FeedModel.(db=0x15d3b7a0

And it's SQLite locking, not the queue. Which generally happens when there are open updates or queries or something else to lock things up.

nawar commented 8 years ago

Very possible. Let me look into the code closely to see if this is the culprit

robertmryan commented 8 years ago

One pattern that I use is a singleton for the database queue, e.g.:

class MasterModel {
    static let sharedQueue: FMDatabaseQueue = {
        let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
        let fileURL = documents.URLByAppendingPathComponent("MyApp.sqlite")
        return FMDatabaseQueue(path: fileURL.path)
    }()
}

Then I can use MasterModel.sharedQueue:

MasterModel.sharedQueue.inDatabase { db in
    do {
        try db.executeUpdate("create table test (a text, b text)", values: nil)
        try db.executeUpdate("insert into test (a, b) values (?, ?)", values: ["foo", "bar"])
        let rs = try db.executeQuery("select * from test", values: nil)
        while rs.next() {
            print(rs.resultDictionary())
        }
    } catch {
        print(error)
    }
}

This way, I know I can never have multiple FMDatabaseQueue instances running.

nawar commented 8 years ago

Thanks guys for the pointers. It seems that multiple FMDBQueue instances were the culprit. Thanks @robertmryan for that bit of code.