Skip to content
Advertisement

Process (NSTask) fileHandleForReading readabilityHandler closure not being called on Linux

When compiling the following code in macOS it runs and gives output.

import Foundation

let runloop = RunLoop.current

let process = Process()
process.launchPath = "/bin/echo"
process.arguments = ["hello world"]

let output = Pipe()
process.standardOutput = output

output.fileHandleForReading.readabilityHandler = { fileHandle in
    let data = fileHandle.availableData
    print("received data: (data.count)")
    print(String(data: data, encoding: .utf8) ?? "")
}

print("starting")
process.launch()

while runloop.run(mode: .default, before: Date(timeIntervalSinceNow: 2)) { }

output:

received data: 12
hello worldn

But when I compile and run this in a Ubuntu Swift docker image (Swift version 5.0.1), the readabilityHandler closure doesn’t get called at all.

Advertisement

Answer

This appears to be fixed in the swift:latest Docker image (5.1.4 at the time of writing). It looks like readabilityHandler was implemented in Linux and merged in the run-up to Swift 5.1.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement