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
[SR-5773] DispatchIO.read on Linux inconsistent with macOS #681
Comments
ktopley-apple (JIRA User), dgrove-oss (JIRA User) |
Comment by Erik Little (JIRA) It should be noted that someone running on Ubuntu 16.10 was unable to reproduce this inconsistency. I haven't tested on anything other than 16.04, so I can't verify that it might be specific to that Ubuntu version. |
Comment by Ryan Lovelett (JIRA) I am also experiencing what I believe is this exact issue in Swift 4.2 The code I'm using to reproduce can be found in this forum thread. |
Comment by Ryan Lovelett (JIRA) Okay I've made a small test case that exemplifies the bug I am seeing. import Dispatch
import Foundation
let pipe1 = Pipe()
let pipe2 = Pipe()
let task1 = Process()
task1.launchPath = "/bin/cat"
task1.arguments = ["/dev/zero"]
task1.standardOutput = pipe1
let task2 = Process()
task2.launchPath = "/usr/bin/head"
task2.arguments = ["-c", "10000"]
task2.standardInput = pipe1
task2.standardOutput = pipe2
let global = DispatchQueue.global(qos: .background)
let channel = DispatchIO(type: .stream, fileDescriptor: pipe2.fileHandleForReading.fileDescriptor, queue: global) { (int) in
DispatchQueue.main.async {
print("Clean-up Handler: \(int)")
exit(EXIT_SUCCESS)
}
}
channel.setLimit(highWater: Int(PAGE_SIZE))
var bytes = 0
channel.read(offset: 0, length: Int.max, queue: global) { (closed, dispatchData, error) in
if let data = dispatchData, !data.isEmpty {
DispatchQueue.main.async {
bytes += data.count
print("Got data! \(data.count) of \(bytes)")
}
}
if closed {
channel.close()
}
}
task1.launch()
task2.launch()
RunLoop.main.run() It definitely is indeterminate when it will occur. I've taken to running it in a loop and eventually the loop will hang. #!/usr/bin/env bash
rm test
rm run-*.log
~/Source/swift-source/rlovelett-installation/usr/bin/swiftc -g test.swift
for run in {001..100}
do
echo "Run ${run}"
LIBDISPATCH_LOG=stderr ./test 2> run-${run}.log
done I'm still trying to make sense of the libdispatch logs to see if they shed any additional information. |
Comment by Ryan Lovelett (JIRA) I think I have figured out what is causing this bug. Though I'm still not 100% what is the best way to fix the problem. Basically, when Dispatch goes to process the file descriptor it gets a Right now my very bad work-around is basically to call the |
Comment by Ryan Lovelett (JIRA) I've attached a proposed fix 0001-Requeue-the-stream-handler-if-the-read-would-block.patch to this issue. I'm still going to keep testing but I think this resolves the bug. This is not the same The real question is does this need a test? I guess I'll make a PR and ask that of the maintainers. |
Comment by Ryan Lovelett (JIRA) Added link to pull request. |
Comment by Erik Little (JIRA) rlovelett (JIRA User) thanks for looking at this! |
this bug is actually the same as https://bugs.swift.org/browse/SR-9033 . The underlying issue here is that Dispatch doesn't handle
|
the underlying issue is that Dispatch on Linux (and other systems that use |
this should be fixed on master by #478 |
Attachment: Download
Environment
Swift version 3.1.1 (swift-3.1.1-RELEASE)
Target: x86_64-unknown-linux-gnu
Ubuntu 16.04
and
Apple Swift version 3.1 (swift-3.1-RELEASE)
Target: x86_64-apple-macosx10.9
macOS 10.12.5
Additional Detail from JIRA
md5: 89a90c899eae85ceb8bc7c56f5960b81
duplicates:
Issue Description:
It seems that when using a pipe and a DispatchIO channel linked to that pipe's file handle for reading, DispatchIO on Linux displays behavior inconsistent with macOS.
See https://gist.github.com/nuclearace/d67fba7c8f94c36d91e31e82863ec4ae for sample code that displays the issue. On macOS this consistently reads until EOF, while on Linux it either hangs while reading after the writer is done, or fails to fall into the EOF branch.
The text was updated successfully, but these errors were encountered: