I got a launchd tool working using ServiceManagement framework.  It runs as root.

The tool is setup to run for a few seconds after the last request and it will keep alive as long as requests are coming in using MachServices.

When idle, the tool exits.

If I try to run the tool a second time, it crashes.  I found out the problem was on the application side I was using an NSConnection that connected to the tool that has already died.  I didn't get any NSConnectionDied notifications or anything, so there's no way for me to know on the application side when the connection has gone down.  If I try to use that connection, I get crashes, and I'm afraid there since this launchd stuff is so crash prone that it won't be reliable.

I'm also sending an object "byRef" using DO from the application to my tool so the tool can talk back to my application.  I'm using the same connection I get from connectionWithRegisteredName.

My application does this to connect to the tool:

NSConnection* connection = [NSConnection connectionWithRegisteredName:[self hostServerName] host:nil];

Do I need to invalidate the NSConnection before the tool dies?  I think I just delete it.

The crash is 100% repeatable.  If I don't reuse the NSConnection on the app side for the next incoming request, it works fine.  Should I always tear down the NSConnection and get it fresh for every call I make to my tool?

// the crash

Process:         com.cocoatech.pathfinder.SMHelper [36457]
Path:            /Library/PrivilegedHelperTools/com.cocoatech.pathfinder.SMHelper
Identifier:      com.cocoatech.pathfinder.SMHelper
Version:         ??? (???)
Code Type:       X86 (Native)
Parent Process:  launchd [1]

Date/Time:       2009-08-28 00:25:47.754 -0700
OS Version:      Mac OS X 10.6 (10A432)
Report Version:  6

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSConcretePortCoder decodeInvocation]: no local target'
*** Call stack at first throw:
(
0   CoreFoundation                      0x9447b58a __raiseError + 410
1   libobjc.A.dylib                     0x94304f49 objc_exception_throw + 56
2   CoreFoundation                      0x9447b2b8 +[NSException raise:format:arguments:] + 136
3   CoreFoundation                      0x9447b22a +[NSException raise:format:] + 58
4   Foundation                          0x9352619f -[NSConcretePortCoder decodeInvocation] + 130
5   Foundation                          0x935207ae -[NSConcretePortCoder decodeRetainedObject] + 794
6   Foundation                          0x934ff805 _NSWalkData2 + 1098
7   Foundation                          0x93525aa9 -[NSConnection handleRequest:sequence:] + 76
8   Foundation                          0x9351fdfe -[NSConnection handlePortCoder:] + 951
9   Foundation                          0x9351fa2d -[NSConcretePortCoder dispatch] + 160
10  Foundation                          0x93507039 __NSFireMachPort + 325
11  CoreFoundation                      0x943eadf2 __CFMachPortPerform + 338
12  CoreFoundation                      0x943eab8e __CFRunLoopDoSource1 + 366
13  CoreFoundation                      0x943e69e9 __CFRunLoopRun + 5561
14  CoreFoundation                      0x943e4d34 CFRunLoopRunSpecific + 452
15  CoreFoundation                      0x943e4b61 CFRunLoopRunInMode + 97
16  Foundation                          0x934df3e4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 279
17  Foundation                          0x935343c8 -[NSRunLoop(NSRunLoop) runUntilDate:] + 79
18  com.cocoatech.pathfinder.SMHelper   0x00002bb6 -[NTSMHelperToolConnection(Private) startRunloop] + 198
19  com.cocoatech.pathfinder.SMHelper   0x00002633 +[NTSMHelperToolConnection connect] + 179
20  com.cocoatech.pathfinder.SMHelper   0x00002556 main + 102
21  com.cocoatech.pathfinder.SMHelper   0x000024e5 start + 53
)


Thread 0 Crashed:  Dispatch queue: com.apple.main-thread
0   com.apple.CoreFoundation       0x944c5b07 ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ + 7
1   libobjc.A.dylib                0x94304f49 objc_exception_throw + 56
2   com.apple.CoreFoundation       0x943e4fbc CFRunLoopRunSpecific + 1100
3   com.apple.CoreFoundation       0x943e4b61 CFRunLoopRunInMode + 97
4   com.apple.Foundation           0x934df3e4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 279
5   com.apple.Foundation           0x935343c8 -[NSRunLoop(NSRunLoop) runUntilDate:] + 79
6   ...coatech.pathfinder.SMHelper 0x00002bb6 -[NTSMHelperToolConnection(Private) startRunloop] + 198
7   ...coatech.pathfinder.SMHelper 0x00002633 +[NTSMHelperToolConnection connect] + 179
8   ...coatech.pathfinder.SMHelper 0x00002556 main + 102 (NTSMHelperTool.m:19)
9   ...coatech.pathfinder.SMHelper 0x000024e5 start + 53

Thread 1:  Dispatch queue: com.apple.libdispatch-manager
0   libSystem.B.dylib              0x9634b10a kevent + 10
1   libSystem.B.dylib              0x9634b824 _dispatch_mgr_invoke + 215
2   libSystem.B.dylib              0x9634ace1 _dispatch_queue_invoke + 163
3   libSystem.B.dylib              0x9634aa86 _dispatch_worker_thread2 + 234
4   libSystem.B.dylib              0x9634a511 _pthread_wqthread + 390
5   libSystem.B.dylib              0x9634a356 start_wqthread + 30

Thread 2:
0   libSystem.B.dylib              0x9634a1a2 __workq_kernreturn + 10
1   libSystem.B.dylib              0x9634a738 _pthread_wqthread + 941
2   libSystem.B.dylib              0x9634a356 start_wqthread + 30

Thread 0 crashed with X86 Thread State (32-bit):
 eax: 0x00000000  ebx: 0x94304f25  ecx: 0xa06ef500  edx: 0x0000003b
 edi: 0xa046eab0  esi: 0x00201a90  ebp: 0xbffffb18  esp: 0xbffffb00
  ss: 0x0000001f  efl: 0x00000286  eip: 0x944c5b07   cs: 0x00000017
  ds: 0x0000001f   es: 0x0000001f   fs: 0x00000000   gs: 0x00000037
 cr2: 0x00032000

Binary Images:
   0x1000 -     0x3ff7 +com.cocoatech.pathfinder.SMHelper ??? (???) <361D2609-7597-AD42-0EFC-50122CD7183B> /Library/PrivilegedHelperTools/com.cocoatech.pathfinder.SMHelper
0x8fe00000 - 0x8fe4162b  dyld 132.1 (???) <211AF0DD-42D9-79C8-BB6A-1F4BEEF4B4AB> /usr/lib/dyld
0x90003000 - 0x90083feb  com.apple.SearchKit 1.3.0 (1.3.0) <9E18AEA5-F4B4-8BE5-EEA9-818FC4F46FD9> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit
0x90206000 - 0x9022eff7  libxslt.1.dylib ??? (???) <769EF4B2-C1AD-73D5-AAAD-1564DAEA77AF> /usr/lib/libxslt.1.dylib
0x90388000 - 0x90425ff3  com.apple.LaunchServices 360.3 (360.3) <C8590D53-E46A-F58A-7CF2-03A8159D8569> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices
0x90426000 - 0x90688ff3  com.apple.security 6.0 (36910) <F045B57C-054F-F06F-EF7E-EABEC2700274> /System/Library/Frameworks/Security.framework/Versions/A/Security
0x90d12000 - 0x90d4fff7  com.apple.SystemConfiguration 1.10 (1.10) <897AEEAF-CF5D-2843-C33B-31A0A7C98A6A> /System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration
0x91118000 - 0x91121ff7  com.apple.DiskArbitration 2.3 (2.3) <E9C40767-DA6A-6CCB-8B00-2D5706753000> /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration
0x91508000 - 0x91609fe7  libxml2.2.dylib ??? (???) <C242A74D-280A-90C3-3F79-891624AA45D2> /usr/lib/libxml2.2.dylib
0x91cc0000 - 0x91d06ff7  libauto.dylib ??? (???) <FAB17F30-A28B-E33D-6E21-C7119C9C83ED> /usr/lib/libauto.dylib
0x91d54000 - 0x91ed6fe7  libicucore.A.dylib ??? (???) <FBB66376-CBA9-8149-A1AA-10AB8578C3B3> /usr/lib/libicucore.A.dylib
0x91ff5000 - 0x92028ff7  com.apple.AE 496 (496) <B638FDD4-7322-F0E4-ACEB-777D8A1399E1> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE
0x92e58000 - 0x92f11fe7  libsqlite3.dylib ??? (???) <16CEF8E8-8C9A-94CD-EF5D-05477844C005> /usr/lib/libsqlite3.dylib
0x9306d000 - 0x9307aff7  com.apple.NetFS 3.2 (3.2) <E6FD80B0-0238-0C42-A3EC-EBDEC107A1C3> /System/Library/Frameworks/NetFS.framework/Versions/A/NetFS
0x9348f000 - 0x936ffffb  com.apple.Foundation 6.6 (751) <A61B645E-1A15-5BCA-3043-C8AB85C6AD30> /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
0x9383d000 - 0x938e4fe7  com.apple.CFNetwork 454.4 (454.4) <7C563385-9893-3B48-8607-5BC81DA2C4CF> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CFNetwork.framework/Versions/A/CFNetwork
0x93a2f000 - 0x93a3bff7  libkxld.dylib ??? (???) <152C8DBB-0149-5827-3240-E57CA85CFE5F> /usr/lib/system/libkxld.dylib
0x93e68000 - 0x93e68ff7  com.apple.CoreServices 44 (44) <AC35D112-5FB9-9C8C-6189-5F5945072375> /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices
0x94038000 - 0x94092ff7  com.apple.framework.IOKit 2.0 (???) <7618DDEC-2E3B-9C6E-FDC9-15169E24B4FB> /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
0x942fb000 - 0x943a8fe7  libobjc.A.dylib ??? (???) <410DD065-A18F-F054-0457-65525F4D1039> /usr/lib/libobjc.A.dylib
0x943a9000 - 0x94520fef  com.apple.CoreFoundation 6.6 (550) <193E33D6-2E92-3452-773B-60A1A9CCC573> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
0x94760000 - 0x94774fe7  libbsm.0.dylib ??? (???) <14CB053A-7C47-96DA-E415-0906BA1B78C9> /usr/lib/libbsm.0.dylib
0x95f48000 - 0x96267fe7  com.apple.CoreServices.CarbonCore 859.1 (859.1) <2E72AF56-4BE6-294A-7372-19C360688B8B> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore
0x962d0000 - 0x962defe7  libz.1.dylib ??? (???) <7B7A02AB-DA99-6180-880E-D28E4F9AA8EB> /usr/lib/libz.1.dylib
0x962df000 - 0x96323fe7  com.apple.Metadata 10.6.0 (507.1) <CBD1B22B-5F10-C784-03A2-35106B97DF3F> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata
0x96324000 - 0x964c8feb  libSystem.B.dylib ??? (???) <068CC3F2-F867-A231-A16C-CC01C29A9816> /usr/lib/libSystem.B.dylib
0x9688a000 - 0x968f4fe7  libstdc++.6.dylib ??? (???) <411D87F4-B7E1-44EB-F201-F8B4F9227213> /usr/lib/libstdc++.6.dylib
0x969d4000 - 0x969d7fe7  libmathCommon.A.dylib ??? (???) <1622A54F-1A98-2CBE-B6A4-2122981A500E> /usr/lib/system/libmathCommon.A.dylib
0x97343000 - 0x97369fff  com.apple.DictionaryServices 1.1 (1.1) <07694B30-56A9-5C98-B8BC-DA0628715FA8> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/DictionaryServices.framework/Versions/A/DictionaryServices
0x98c75000 - 0x98d3ffef  com.apple.CoreServices.OSServices 352 (352) <D9F21CA4-EED0-705F-8F3C-F1322D114B52> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices
0xffff0000 - 0xffff1fff  libSystem.B.dylib ??? (???) <068CC3F2-F867-A231-A16C-CC01C29A9816> /usr/lib/libSystem.B.dylib