I am creating DispatchIO channels using the file descriptor constructor:
self.channel = DispatchIO(type: .random, fileDescriptor: self.file, queue: fileProcessingQueue, cleanupHandler: { (errorCode) in
// clean up when the channel closes
})
The documentation for cleanupHandler states:
"The handler to execute once the channel is closed."
The better documentation (when you switch language to ObjC) states:
"The block to enqueue when the system relinquishes control of the channel’s file descriptor."
"... the system takes control of the specified file descriptor until one of the following occurs:
You close the channel by calling the
dispatch_io_close
function.
..."
So from both of these, I expect the handler gets enqueued and executed if I call
self.channel.close(flags [.stop])
But it seems to never get executed under this condition. In fact, it's very difficult to get the cleanup handler to be called at all. The only way so far I have been able to get it to execute is by calling close() on the underlying file descriptor. Am I doing something wrong here? There's cleanup that I want to do when the channel closes that is different than the cleanup I want to do when I'm done with the file descriptor. Is this not the correct place to do it?