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

Make 'echo' raise IOErrors when appropriate #16367

Merged
merged 7 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/system/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -792,20 +792,21 @@ when declared(stdout):
proc flockfile(f: File) {.importc, nodecl.}
proc funlockfile(f: File) {.importc, nodecl.}
flockfile(stdout)
defer: funlockfile(stdout)
when defined(windows) and compileOption("threads"):
acquireSys echoLock
defer: releaseSys echoLock
for s in args:
when defined(windows):
writeWindows(stdout, s)
else:
discard c_fwrite(s.cstring, cast[csize_t](s.len), 1, stdout)
if c_fwrite(s.cstring, cast[csize_t](s.len), 1, stdout) != s.len:
iffy marked this conversation as resolved.
Show resolved Hide resolved
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

this seems unfortunately subject to the classic EINTR bug, see https://github.com/angrave/SystemProgramming/wiki/POSIX%2C-Part-1%3A-Error-handling#what-is-eintr-what-does-it-mean-for-sem_wait-read-write and how I fixed it here #13232 via a while loop which is IIUC the correct way to handle this

the question is whether to fix this in this PR or merge this PR as is and fix it in future PR; is there an easy repro?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think do it as a separate PR, abstracting out a common template in the process (as mentioned in your fix for the linked issue).

checkErr(stdout)
const linefeed = "\n"
discard c_fwrite(linefeed.cstring, linefeed.len, 1, stdout)
discard c_fflush(stdout)
when stdOutLock:
funlockfile(stdout)
when defined(windows) and compileOption("threads"):
releaseSys echoLock
if c_fwrite(linefeed.cstring, linefeed.len, 1, stdout) != linefeed.len:
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
checkErr(stdout)
if c_fflush(stdout) != 0:
checkErr(stdout)


when defined(windows) and not defined(nimscript) and not defined(js):
Expand Down
10 changes: 10 additions & 0 deletions tests/exception/t16366.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
discard """
action: run
exitcode: 1
iffy marked this conversation as resolved.
Show resolved Hide resolved
targets: "c cpp"
iffy marked this conversation as resolved.
Show resolved Hide resolved
"""

echo "foo1"
close stdout
doAssertRaises(IOError):
echo "foo"