Skip to content
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-14953] Data.write(to:) fails when writing to /dev/stdout #3207

Closed
weissi opened this issue Jul 22, 2021 · 4 comments
Closed

[SR-14953] Data.write(to:) fails when writing to /dev/stdout #3207

weissi opened this issue Jul 22, 2021 · 4 comments

Comments

@weissi
Copy link
Member

weissi commented Jul 22, 2021

Previous ID SR-14953
Radar rdar://problem/80949531
Original Reporter @weissi
Type Bug
Environment

5.4/main docker image

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 98334e709b9a35bb96b5b5e9f20f864e

Issue Description:

try Data("hello".utf8).write(to: URL(fileURLWithPath: "/dev/stdout"))

will fail on Linux with

NSError = domain: nil - code: 14987979559889010706 {
  Foundation.NSObject = {}
  _domain = "NSCocoaErrorDomain"
  _code = 512
  _userInfo = 1 key/value pair {
    [0] = {
      key = "NSUnderlyingError"
      value = domain: nil - code: 14987979559889010706 {
        Foundation.NSObject = {}
        _domain = "NSPOSIXErrorDomain"
        _code = 22
        _userInfo = nil
      }
    }
  }
}

Or a more comprehensive program

import Foundation

let target = CommandLine.arguments.dropFirst().first ?? "/dev/stdout"
do {
    try Data("hello".utf8).write(to: URL(fileURLWithPath: target))
} catch {
    print("ERROR: \(error)")
}

when run under Linux will print

helloERROR: Error Domain=NSCocoaErrorDomain Code=512 "(null)"

note how it actually succeeds in writing the data.

As of osstatus.com, the error is NSFileWriteUnknownError.

The problem seems to be that we call fsync on the file descriptor after the write which isn't valid for most devices:

openat(AT_FDCWD, "/dev/stdout", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(3, "hello", 5hello)                    = 5
fsync(3)                                = -1 EINVAL (Invalid argument)

probably originating from here:

@weissi
Copy link
Member Author

weissi commented Jul 22, 2021

CC @millenomi/@tomerd/@spevans

@weissi
Copy link
Member Author

weissi commented Jul 22, 2021

@swift-ci create

@spevans
Copy link
Collaborator

spevans commented Jul 31, 2021

#3022

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
@weissi
Copy link
Member Author

weissi commented May 5, 2022

This is fixed in 5.6:

$ cat repro.swift 
import Foundation
try Data("hello".utf8).write(to: URL(fileURLWithPath: "/dev/stdout"))

johannes:/tmp
$ jw-docker-swift-5.6 swift repro.swift
hellojohannes:/tmp
$ 

@weissi weissi closed this as completed May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants