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

14354117: Nested Core Data contexts deadlock on 1st document save if they're on same queue #13555

Open
openradar-mirror opened this issue Apr 17, 2016 · 0 comments

Comments

@openradar-mirror
Copy link

Description

Summary:
In the attached sample project (or download from http://cl.ly/2Y0y020o1t2R) of a document based app we've got a setup of three nested Core Data contexts.

  • savingContext (async queue)
    -- mainDocumentContext (main queue)
    --- scratchContext (main queue)

The reason to have two contexts on the main queue is to allow the scratchContext contents to be used for UI updates as well. If changes were made in the mainDocumentContext and the document gets saved to disk for the first time, e.g. through autosave, the contexts on the main queue deadlock.

Steps to Reproduce:
Open attached sample app. On the left, add one or more items. Switch to another app to let autosave kick in.

Expected Results:
App still being responsive.

Actual Results:
App deadlocks/hangs.

What's interesting that the array controller somehow tries to access the saving context in the other queue (see stack trace below). The fetch request on the main queue is just waiting around. This seems to happen while the persistent store is created for the first time with addPersistentStoreWithType:configuration:URL:options:error: (as can be seen in the stack trace of the second queue below)

Regression:

Notes:

Thread 1, Queue : NSManagedObjectContext Queue
#0 0x00007fff91dc3122 in __psynch_mutexwait ()
#1 0x00007fff91806dfd in pthread_mutex_lock ()
#2 0x00007fff8f4dd548 in -_PFLock lock
#3 0x00007fff8f4ee2e7 in -NSManagedObjectContext executeFetchRequest:error:
#4 0x00007fff8f536a4e in -NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:
#5 0x00007fff8f56a72a in __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 ()
#6 0x00007fff938f60b6 in _dispatch_client_callout ()
#7 0x00007fff938faaf1 in _dispatch_barrier_sync_f_slow ()
#8 0x00007fff8f53687c in _perform ()
#9 0x00007fff8f5366c2 in -NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:
#10 0x00007fff8f4ee309 in -NSManagedObjectContext executeFetchRequest:error:
#11 0x00007fff8f536a4e in -NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:
#12 0x00007fff8f56a72a in __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 ()
#13 0x00007fff938fba2d in _dispatch_barrier_sync_f_slow_invoke ()
#14 0x00007fff938f60b6 in _dispatch_client_callout ()
#15 0x00007fff938fb0c8 in _dispatch_main_queue_callback_4CF ()
#16 0x00007fff8a817b4c in __CFRunLoopRun ()
#17 0x00007fff8a8170e2 in CFRunLoopRunSpecific ()
#18 0x00007fff8d181eb4 in RunCurrentEventLoopInMode ()
#19 0x00007fff8d181c52 in ReceiveNextEventCommon ()
#20 0x00007fff8d181ae3 in BlockUntilNextEventMatchingListInMode ()
#21 0x00007fff92b4e533 in _DPSNextEvent ()
#22 0x00007fff92b4ddf2 in -NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:
#23 0x00007fff92b451a3 in -NSApplication run
#24 0x00007fff92ae9bd6 in NSApplicationMain ()
#25 0x0000000100002812 in main at /Users/ck/Documents/Golden Box/Tests/DocumentContextsTest/DocumentContextsTest/main.m:13
#26 0x00007fff8fb4c7e1 in start ()

Thread 5, Queue : com.apple.root.default-priority
#0 0x00007fff91dc16c2 in semaphore_wait_trap ()
#1 0x00007fff938fac32 in _dispatch_thread_semaphore_wait ()
#2 0x00007fff938faa92 in _dispatch_barrier_sync_f_slow ()
#3 0x00007fff8f53687c in _perform ()
#4 0x00007fff8f5366c2 in -NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:
#5 0x00007fff8f4ee309 in -NSManagedObjectContext executeFetchRequest:error:
#6 0x00007fff92ab890c in -_NSManagedProxy fetchObjectsWithFetchRequest:error:
#7 0x00007fff92da9352 in -NSArrayController(NSManagedController) _performFetchWithRequest:merge:error:
#8 0x00007fff92ab837a in -NSObjectController(NSManagedController) fetchWithRequest:merge:error:
#9 0x00007fff92f8dd65 in -_NSManagedProxy _storesDidChange:
#10 0x00007fff8a827eda in _CFXNotificationPost ()
#11 0x00007fff8e5ef7b6 in -NSNotificationCenter postNotificationName:object:userInfo:
#12 0x00007fff8f4ecced in -NSPersistentStoreCoordinator(_NSInternalMethods) _postStoresChangedNotificationsForStores:changeKey:options:
#13 0x00007fff8f4de516 in -NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:
#14 0x0000000100003737 in -[BSManagedDocument configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:] at /Users/ck/Documents/Golden Box/Tests/DocumentContextsTest/DocumentContextsTest/BSManagedDocument/BSManagedDocument.m:156
#15 0x0000000100004b1e in __63-[BSManagedDocument contentsForURL:ofType:saveOperation:error:]_block_invoke at /Users/ck/Documents/Golden Box/Tests/DocumentContextsTest/DocumentContextsTest/BSManagedDocument/BSManagedDocument.m:325
#16 0x00000001000072e9 in -[BSManagedDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] at /Users/ck/Documents/Golden Box/Tests/DocumentContextsTest/DocumentContextsTest/BSManagedDocument/BSManagedDocument.m:756
#17 0x00007fff92a252ce in -NSDocument _writeSafelyToURL:ofType:forSaveOperation:forceTemporaryDirectory:error:
#18 0x00007fff92a25014 in -NSDocument _writeSafelyToURL:ofType:forSaveOperation:error:
#19 0x00007fff92a24b99 in -NSDocument writeSafelyToURL:ofType:forSaveOperation:error:
#20 0x0000000100006fe6 in -[BSManagedDocument writeSafelyToURL:ofType:forSaveOperation:error:] at /Users/ck/Documents/Golden Box/Tests/DocumentContextsTest/DocumentContextsTest/BSManagedDocument/BSManagedDocument.m:715
#21 0x00007fff92a24337 in __block_global_90 ()
#22 0x00007fff92a31aef in __block_global_97 ()
#23 0x00007fff938f9f01 in _dispatch_call_block_and_release ()
#24 0x00007fff938f60b6 in _dispatch_client_callout ()
#25 0x00007fff938f71fa in _dispatch_worker_thread2 ()
#26 0x00007fff91803d0b in _pthread_wqthread ()
#27 0x00007fff917ee1d1 in start_wqthread ()

Product Version: 10.8.4/12E55
Created: 2013-07-04T10:16:03.836896
Originated: 2013-07-04T11:09:00
Open Radar Link: http://www.openradar.me/14354117

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

No branches or pull requests

1 participant