Skip to content

Use readData to read data from a pipe instead of availableData #8047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 16, 2024

Using availableData causes parallel XCTest execution to hang on Windows if the tests output significant data to stderr.

@ahoppen
Copy link
Member Author

ahoppen commented Oct 16, 2024

@swift-ci Please test

Using `availableData` causes parallel XCTest execution to hang on Windows if the tests output significant data to stderr.
ahoppen added a commit to ahoppen/swift-tools-support-core that referenced this pull request Oct 16, 2024
@ahoppen
Copy link
Member Author

ahoppen commented Oct 16, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Oct 16, 2024

@swift-ci Please test Windows

Copy link
Member

@dschaefer2 dschaefer2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we all have scars from using availableData to read stdout/stderr. Thanks @ahoppen!

@ahoppen ahoppen enabled auto-merge (squash) October 16, 2024 18:27
Copy link
Contributor

@jakepetroules jakepetroules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this will work?. availableData just calls the same underlying API as read(upToCount:), and it still has the ability to block the calling thread.

    private func _checkFileHandle() {
        precondition(self._handle != INVALID_HANDLE_VALUE, "Invalid file handle")
    }
    open var availableData: Data {
        _checkFileHandle()
        do {
            let readResult = try _readDataOfLength(Int.max, untilEOF: false)
            return readResult.toData()
        } catch {
            fatalError("\(error)")
        }
    }
    public func read(upToCount count: Int) throws -> Data? {
        guard self != FileHandle._nulldeviceFileHandle else { return nil }
        
        let result = try _readDataOfLength(count, untilEOF: true)
        return result.length == 0 ? nil : result.toData()
    }

@ahoppen ahoppen merged commit 7e1c6b9 into swiftlang:main Oct 16, 2024
5 checks passed
@ahoppen ahoppen deleted the read-data branch October 16, 2024 21:52
@ahoppen
Copy link
Member Author

ahoppen commented Oct 16, 2024

Yes, I tested that we no longer see the hang when using read(upToCount:). I don’t know why it fixes it but 🤷🏽 maybe it’s because one of them does untilEOF: true and the other one untilEOF: false.

shahmishal pushed a commit to swiftlang/swift-tools-support-core that referenced this pull request Oct 17, 2024
@dschaefer2
Copy link
Member

Yes, I tested that we no longer see the hang when using read(upToCount:). I don’t know why it fixes it but 🤷🏽 maybe it’s because one of them does untilEOF: true and the other one untilEOF: false.

That feels true. The hangs I've seen have been at the end of the stream where EOF isn't properly detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants