Swift macOS Process.run() port leak

2020-07-17 04:18发布

问题:

Current code:

#!/usr/bin/swift

import Foundation

func runSleepProcess() {
    let sleepProcess = Process()
    sleepProcess.executableURL = URL(fileURLWithPath: "/bin/sleep")
    sleepProcess.arguments = ["0"]
    try? sleepProcess.run()
    sleepProcess.waitUntilExit()
}

while true {
    runSleepProcess()
}

Looking in activity monitor, it seems that the mach port usage increases by 1 each loop. Is this expected behavior when running an external process? If not, how do I fix the leak? Thanks.

回答1:

It's not expected behaviour and this problem is already reported. The workaround for this is using posix_spawn instead of Process.