Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crash (Signal 13) while creating input pipe in Swift iOS

Tags:

ios

swift

crash

My app got crash with error Terminated due to signal 13.

I am creating input pipe and on that line app got crash with above reason.

 public func openConsolePipe() {

        inputPipe = Pipe()

        outputPipe = Pipe()
        let pipeReadHandle = inputPipe.fileHandleForReading

        dup2(STDOUT_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)
        dup2(STDERR_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)

        dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
        dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)

        NotificationCenter.default.addObserver(self, selector: #selector(self.handlePipeNotification), name: FileHandle.readCompletionNotification, object: pipeReadHandle)

        pipeReadHandle.readInBackgroundAndNotify()
}

I am following This Link for implement this.

Now above tutorial working fine in regular project.

But I am creating framework of this demo and use it another app code.

At that time app got crash on Line.

dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)

This is below method in that I am getting all logs from pipe.

@objc func handlePipeNotification(notification: Notification)
{
        inputPipe.fileHandleForReading.readInBackgroundAndNotify()

        if let data = notification.userInfo![NSFileHandleNotificationDataItem] as? Data,
        let str = String(data: data, encoding: String.Encoding.ascii) {

            outputPipe.fileHandleForWriting.write(data)
        }
}
like image 348
Hardik Vyas Avatar asked Sep 06 '25 02:09

Hardik Vyas


1 Answers

I have founded issue after too much R&D and testing.

Here my app crash because of when i am implement pipe() in library and import that library it's crash because of Pipe() always have to run on Main Queue.

So here i simply create pipe() and all method in Main Queue like this.

public func openConsolePipe() {
DispatchQueue.main.async {
        inputPipe = Pipe()

        outputPipe = Pipe()
        let pipeReadHandle = inputPipe.fileHandleForReading

        dup2(STDOUT_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)
        dup2(STDERR_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)

        dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
        dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)

        NotificationCenter.default.addObserver(self, selector: #selector(self.handlePipeNotification), name: FileHandle.readCompletionNotification, object: pipeReadHandle)

        pipeReadHandle.readInBackgroundAndNotify()
}
}

And this is pipe handler method with main Queue.

@objc func handlePipeNotification(notification: Notification)
{
        DispatchQueue.main.async {
        inputPipe.fileHandleForReading.readInBackgroundAndNotify()

        if let data = notification.userInfo![NSFileHandleNotificationDataItem] as? Data,
        let str = String(data: data, encoding: String.Encoding.ascii) {

            outputPipe.fileHandleForWriting.write(data)
        }
        }
}
like image 81
Hardik Vyas Avatar answered Sep 07 '25 21:09

Hardik Vyas