5 Replies
      Latest reply on Oct 21, 2019 1:15 AM by eskimo
      ManuelCSolis Level 1 Level 1 (0 points)

        My application handles multiple connections through the USB ports as serial devices that use termios, the application needs to send and receive a large amount of data for each open connection, but in very rare cases the process that handles the data received through a specific USB port crashes, collapsing the application and affecting the other open connections. To minimize the damage, I intend to use NSXPCConnection instances. My question to this forum is: How many similar instances of NSXPCConnection can be opened for the same application?

        • Re: NSXPCConnection multiple instances.
          eskimo Apple Staff Apple Staff (12,285 points)

          How many similar instances of NSXPCConnection can be opened for the same application?

          That depends on what you mean by “similar”.  Looking at your big picture goal I suspect that you’re trying to use an XPC Service to run your serial connections, as discussed in the Creating XPC Services section of the Daemons and Services Programming Guide.  Is that right?

          If so, be aware that the public XPC Service mechanism does not support multiple instances.  That is, if an app has XPC Services nested within the app, an instance of that app can only instantiate a single instance of each of those services.  So, for example, if you have an XPC Service that runs a serial connection, you must either:

          • Restrict yourself to one serial connection

          • Run multiple serial connections in the same service

          Clearly both of these are suboptimal.  Specifically, the second option means that, while your app is isolated from a crash in your service, a crash for serial connection A will also cause serial connections B and C to fail.

          IMPORTANT If you’d like us to support multiple instances of a single service in the future, I encourage you to file an enhancement request describing your requirements.  Please post your bug number, just for the record.

          It is possible to work around this.  One easy workaround would be to have multiple services that all run the same code.  It’s unlikely that a Mac would have more than, say, 10 serial ports attached at once, so this won’t get too far out of control.  Also, you can use a framework to share code between the services, so the disk space impact won’t be prohibitive.

          The alternative is less easy, namely, you have a single service that acts as a broker and launches child processes to run each serial connection.  This breaks down as follows:

          1. The app connects to the main service and asks it to start a new serial connection.

          2. The service launches a child process (using NSTask or any other posix_spawn wrapper).

          3. That child ‘checks in’ with the main service.

          4. The child creates an anonymous XPC listener (+[NSXPCListener anonymousListener]), gets an endpoint for that listener (endpoint) and passes that endpoint to the service.

          5. The service then passes that endpoint back to the app.

          6. The app can then connect to the child directly (-[NSXPCConnection initWithListenerEndpoint:).

          Now, if the broker service goes down then that will take all of the connections with it, but that should be relatively robust because it’s not actually doing any work with the serial port.

          Getting this to work reliably will involve a bunch of complex code, but I don’t see any unsurmountable obstacles.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"