Unable to open more than 2560 Pipe() (even if I close them)

Hi, I have an issue that I don't understand, and I need help. I am using Pipe() in Swift, and I found that if I create more than 2560 (more or less) pipes, I cannot create more. The call fails with Too many open files, even though I closed all the files associated with the pipes.

This is a minimal code to reproduce the issue:

for i in 0...1278 {
    NSLog("Testing: \(i)")
    let stdin_pipe = Pipe()
    let stdin_file = fdopen(stdin_pipe.fileHandleForReading.fileDescriptor, "r")
    var stdout_pipe = Pipe()
    var stdout_file = fdopen(stdout_pipe.fileHandleForWriting.fileDescriptor, "w")
    if (stdout_file == nil) {
        let errorString = String(cString: strerror(errno))
        NSLog("Could not create an output stream. Error: \(errorString)")
    }
    do {
        // I'm really trying to close everything (but it doesn't matter):
        close(stdout_pipe.fileHandleForWriting.fileDescriptor)
        close(stdin_pipe.fileHandleForReading.fileDescriptor)
        try stdout_pipe.fileHandleForWriting.close()
        try stdin_pipe.fileHandleForReading.close()
        try stdout_pipe.fileHandleForReading.close()
        try stdin_pipe.fileHandleForWriting.close()
    }
    catch {
        NSLog("Error in closing pipes in MyExtension: \(error.localizedDescription)")
    }
}

It will work for i going to 0 to 1277, and fail for i == 1278. The error (obtained from strerror(errno) is Too many files open, even though there are around 6 file descriptors opened (I checked).

What am I doing wrong?

Answered by N. Holzschuch in 784524022

The answer is that I need to use fclose, not close. So:

        do {
            fclose(stdout_file)
            fclose(stdin_file)
            try stdout_pipe.fileHandleForWriting.close()
            try stdin_pipe.fileHandleForReading.close()
            try stdout_pipe.fileHandleForReading.close()
            try stdin_pipe.fileHandleForWriting.close()
        }
Accepted Answer

The answer is that I need to use fclose, not close. So:

        do {
            fclose(stdout_file)
            fclose(stdin_file)
            try stdout_pipe.fileHandleForWriting.close()
            try stdin_pipe.fileHandleForReading.close()
            try stdout_pipe.fileHandleForReading.close()
            try stdin_pipe.fileHandleForWriting.close()
        }
Unable to open more than 2560 Pipe() (even if I close them)
 
 
Q