On 27 Jan 2013, at 06:49, Martin Hewitson <martin.hewit...@aei.mpg.de> wrote:

> (sent again because it was too long and got held for moderation)
> 
> Dear list,
> 
> Forgive my potential use of bad terminology and ignorance here. I'm about to 
> talk about concurrency in the context of core data, and I suspect I'm doing 
> things terribly wrong.
> 
> I have an app which does background processing of the entities in a core data 
> stack of an NSPersistentDocument . Sometimes I get deadlocks (I think) which, 
> when sampled, look like the excerpt below.
> 
> Roughly, what my app does is:
> 
> 1) When a File entity is created, it creates for itself a MetaData collector
> 2) The MetaData collector has a timer which fires every second
> 3) In the timer selector, if a update is no in process, I do:
>       a) create a new MetaDataOperation (NSOperation subclass)
>               - the completion block of this operation tries to post a 
> notification on the main thread like this
>                       
>                       dispatch_sync(dispatch_get_main_queue(), ^{
>                               [blockSelf notifyOfUpdate];
>                               blockSelf = nil;
>                       });
> 
>       b) add operation to operation queue
>       c) use an SyntaxChecker (which uses an NSTask) to do some other 
> processing of the File's content
> 
> 4) When the SyntaxChecker's NSTask completes, it calls back to its delegate, 
> the MetaData collector, which then posts notification which is observed by a 
> view controller. I think this is all on the main thread. This view controller 
> is essentially the same as those I describe next, which handle displaying the 
> meta data that's gathered in the MetaDataOperation step.
> 
> 5) When the MetaData collector gets the notification that an update has 
> completed, it then does some local state clean up and posts a notification 
> which is received by a number of view controllers, each of which controls the 
> display of different parts of the meta data. A typical handler in one of 
> these view controllers looks like this:
> 
> - (void) handleMetadataUpdate:(NSNotification*)aNote
> {  
>  [self performSelectorOnMainThread:@selector(updateUI) withObject:nil 
> waitUntilDone:NO];
> }
> 
> In the updating of the UI, I do things like:
> 
> 1) get the set of the relevant File entities from the view controller's 
> delegate 
> 2) compare the meta data in the File entities to a local set of objects 
> (which hold references to their corresponding entities), and update any if 
> necessary
> 3) reload outline view
> 
> Naturally, for the sake of brevity, I've simplified the picture a little, but 
> not much. I believe the salient points are described. 
> 
> So, now my questions and issues:
> 
> 1) How can I interpret the call graph shown below? I'm finding it difficult 
> to extract from that who's blocking whom.
> 2) Is there something glaringly wrong with the strategy I sketched above? (I 
> guess so, since it took a lot of experimentation to get it working at all.)

>From your sample, I would judge that the main thread is waiting to acquire the 
>persistent store coordinator's lock. The worker thread is stuck trying to 
>deliver a message to the main thread, which it can't because the main thread 
>is currently tied up.

Now, you didn't explain too much what your background operations are doing. Are 
they accessing any managed objects at all? If so those managed objects 
ABSOLUTELY MUST come from a separate dedicated context. You cannot access Core 
Data from multiple threads without using an appropriate technique.

Also, I don't see any need for your completion block to use dispatch_sync. 
Async should be fine here.
> 
> -----------------------------------------------------
> 
> Call graph:
>    2771 Thread_4677360   DispatchQueue_1: com.apple.main-thread  (serial)
>    + 2771 start  (in TeXnicle) + 52  [0x1000018e4]
>    +   2771 main  (in TeXnicle) + 34  [0x100001912]  main.m:32
>    +     2771 NSApplicationMain  (in AppKit) + 869  [0x7fff829dfcb6]
>    +       2771 -[NSApplication run]  (in AppKit) + 517  [0x7fff82a3b283]
>    +         2771 -[NSApplication 
> nextEventMatchingMask:untilDate:inMode:dequeue:]  (in AppKit) + 128  
> [0x7fff82a43ed2]
>    +           2771 _DPSNextEvent  (in AppKit) + 685  [0x7fff82a44613]
>    +             2771 BlockUntilNextEventMatchingListInMode  (in HIToolbox) + 
> 62  [0x7fff8a0e5cd3]
>    +               2771 ReceiveNextEventCommon  (in HIToolbox) + 356  
> [0x7fff8a0e5e42]
>    +                 2771 RunCurrentEventLoopInMode  (in HIToolbox) + 209  
> [0x7fff8a0e60a4]
>    +                   2771 CFRunLoopRunSpecific  (in CoreFoundation) + 290  
> [0x7fff81aaa6b2]
>    +                     2771 __CFRunLoopRun  (in CoreFoundation) + 789  
> [0x7fff81aaadc5]
>    +                       2771 __CFRunLoopDoSources0  (in CoreFoundation) + 
> 445  [0x7fff81a87aed]
>    +                         2771 
> __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__  (in 
> CoreFoundation) + 17  [0x7fff81a88101]
>    +                           2771 _performFileHandleSource  (in Foundation) 
> + 1280  [0x7fff8a6dba7c]
>    +                             2771 -[NSNotificationCenter 
> postNotificationName:object:userInfo:]  (in Foundation) + 64  [0x7fff8a61d846]
>    +                               2771 _CFXNotificationPost  (in 
> CoreFoundation) + 2554  [0x7fff81abb47a]
>    +                                 2771 -[TPSyntaxChecker 
> texOutputAvailable:]  (in TeXnicle) + 535  [0x10017daa7]  
> TPSyntaxChecker.m:481
>    +                                   2771 -[TPSyntaxChecker 
> syntaxCheckerCheckDidFinish:]  (in TeXnicle) + 258  [0x10017e282]  
> TPSyntaxChecker.m:518
>    +                                     2771 -[TPFileEntityMetadata 
> syntaxCheckerCheckDidFinish:]  (in TeXnicle) + 671  [0x1001975af]  
> TPFileEntityMetadata.m:249
>    +                                       2771 -[TPFileEntityMetadata 
> postWarningsUpdateNotification]  (in TeXnicle) + 107  [0x1001971ab]  
> TPFileEntityMetadata.m:225
>    +                                         2771 -[NSNotificationCenter 
> postNotificationName:object:userInfo:]  (in Foundation) + 64  [0x7fff8a61d846]
>    +                                           2771 _CFXNotificationPost  (in 
> CoreFoundation) + 2554  [0x7fff81abb47a]
>    +                                             2771 
> -[TPWarningsViewController handleMetadataUpdate:]  (in TeXnicle) + 70  
> [0x1001ab046]  TPWarningsViewController.m:91
>    +                                               2771 
> -[NSObject(NSThreadPerformAdditions) 
> performSelectorOnMainThread:withObject:waitUntilDone:]  (in Foundation) + 131 
>  [0x7fff8a6610c8]
>    +                                                 2771 
> -[NSObject(NSThreadPerformAdditions) 
> performSelector:onThread:withObject:waitUntilDone:modes:]  (in Foundation) + 
> 212  [0x7fff8a661220]
>    +                                                   2771 
> -[TPWarningsViewController updateUI]  (in TeXnicle) + 56  [0x1001ac408]  
> TPWarningsViewController.m:235
>    +                                                     2771 
> -[TPWarningsViewController warningsViewlistOfFiles:]  (in TeXnicle) + 258  
> [0x1001acf12]  TPWarningsViewController.m:297
>    +                                                       2771 
> -[TeXProjectDocument warningsViewlistOfFiles:]  (in TeXnicle) + 155  
> [0x100022cdb]  TeXProjectDocument.m:4060
>    +                                                         2771 
> -[ProjectEntity items]  (in TeXnicle) + 311  [0x1000288b7]  ProjectEntity.m:94
>    +                                                           2771 
> -[NSManagedObjectContext executeFetchRequest:error:]  (in CoreData) + 537  
> [0x7fff8bcae309]
>    +                                                             2771 
> -[NSPersistentStoreCoordinator executeRequest:withContext:error:]  (in 
> CoreData) + 1138  [0x7fff8bcafba2]
>    +                                                               2771 
> -[_PFLock lock]  (in CoreData) + 24  [0x7fff8bc9d548]
>    +                                                                 2771 
> pthread_mutex_lock  (in libsystem_c.dylib) + 536  [0x7fff824aad9d]
>    +                                                                   2771 
> __psynch_mutexwait  (in libsystem_kernel.dylib) + 10  [0x7fff8d1f0122]
>    2771 Thread_4677447   DispatchQueue_2: com.apple.libdispatch-manager  
> (serial)
>    + 2771 _dispatch_mgr_thread  (in libdispatch.dylib) + 54  [0x7fff8031c9ee]
>    +   2771 _dispatch_mgr_invoke  (in libdispatch.dylib) + 883  
> [0x7fff8031cdea]
>    +     2771 kevent  (in libsystem_kernel.dylib) + 10  [0x7fff8d1f0d16]
>    2771 Thread_4677448   DispatchQueue_129: NSOperation low-priority 
> concurrency-limiting queue  (concurrent)
>    + 2771 start_wqthread  (in libsystem_c.dylib) + 13  [0x7fff82492171]
>    +   2771 _pthread_wqthread  (in libsystem_c.dylib) + 404  [0x7fff824a7cab]
>    +     2771 _dispatch_worker_thread2  (in libdispatch.dylib) + 304  
> [0x7fff8031b1fa]
>    +       2771 _dispatch_client_callout  (in libdispatch.dylib) + 8  
> [0x7fff8031a0b6]
>    +         2771 _dispatch_async_f_redirect_invoke  (in libdispatch.dylib) + 
> 117  [0x7fff8031f317]
>    +           2771 _dispatch_client_callout  (in libdispatch.dylib) + 8  
> [0x7fff8031a0b6]
>    +             2771 _dispatch_call_block_and_release  (in 
> libdispatch.dylib) + 15  [0x7fff8031df01]
>    +               2771 __103+[__NSOperationInternal 
> _observeValueForKeyPath:ofObject:changeKind:oldValue:newValue:indexes:context:]_block_invoke_0121
>   (in Foundation) + 49  [0x7fff8a719700]
>    +                 2771 __38-[TPFileEntityMetadata 
> updateMetadata]_block_invoke_0  (in TeXnicle) + 92  [0x100196dac]  
> TPFileEntityMetadata.m:168
>    +                   2771 _dispatch_barrier_sync_f_slow  (in 
> libdispatch.dylib) + 188  [0x7fff8031ea92]
>    +                     2771 _dispatch_thread_semaphore_wait  (in 
> libdispatch.dylib) + 16  [0x7fff8031ec32]
>    +                       2771 semaphore_wait_trap  (in 
> libsystem_kernel.dylib) + 10  [0x7fff8d1ee6c2]
> 
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/cocoadev%40mikeabdullah.net
> 
> This email sent to cocoa...@mikeabdullah.net


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to