ilya1725
ilya1725

Reputation: 4968

Tcl flush command returns error

I've gotten this error from Tcl flush command:

error flushing "stdout": I/O error

Any ideas why it can happen? Also, the man page doesn't say anything about flush returning errors. Can the same error come from puts? And ultimately: what do do about it?

Thank you.

Upvotes: 3

Views: 1019

Answers (1)

kostix
kostix

Reputation: 55533

By default Tcl uses line buffering on stdout (more on this later). This means whatever you puts there gets buffered and is only output when the newline is seen in the buffer or when you flush the channel. So, yes, you can get the same error from puts directly, if a call to it is done on an unbuffered channel or if it managed to hit that "buffer full" condition so that the underlying medium is really accessed.

As to "I/O error", we do really need more details. Supposedly the stdout of your program has been redirected (or reopened), and the underlying media is inaccessible for some reason.

You could try to infer more details about the cause by inspecting the POSIX errno of the actual failing syscall — it's accessible via the global errorCode variable after a Tcl command involving such a syscall signalized error condition. So you could go like this:

set rc [catch {flush stdout} err]
if {$rc != 0} {
    global errorCode
    set fd [open error.log w]
    puts $fd $err\n\n$errorCode
    close $fd
}

(since Tcl 8.5 you can directly ask catch to return all the relevant info instead of inspecting magic variables, see the manual).

Providing more details on how you run your program or whether you reopen stdout is strongly suggested.


A note on buffering. Output buffering can be controlled using fconfigure (or chan configure since 8.5 onwards) on a channel by manipulating its -buffering option. Usually it's set to line on stdout but it can be set to full or none. When set to full, the -buffersize option can be used to control the buffer size explicitly.

Upvotes: 4

Related Questions