Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Doc: example in fileevent manpage is bad.

Closed / Fixed1995063fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-06-16 12:06:40
The example event-handler procedure does check for
eof on the channel, but doesn't close the channel,
and would therefore cause a busy loop on eof.

Suggestion:

   proc GetData {chan} {
       if {[eof $chan]} { close $chan; return }

       puts [gets $chan]
   }

might be a more typical example.
avl42 Ticket [1995063fff]: 1 change [History diff]2008-06-16 19:07:28
dkfplain2008-06-16 20:07:41
Logged In: YES 
user_id=79902
Originator: NO

[eof] shouldn't be used until after the [gets] anyway. Better would be this:

  proc GetData {chan} {
     if {[gets $chan line] >= 0} {
        puts $line
     } elseif {[eof $chan]} {
        close $chan
     }
  }
avl42plain2008-06-16 20:38:10
Logged In: YES 
user_id=830354
Originator: YES

ad dkf:  huh?

This "checking eof only after reading" is appropriate for 
while loops, where it's usually the reading command that
causes the eof to be reached in the first place.

fileevent-callbacks are also fired when previous gets 
returned with no eof-condition, but the channel died 
afterwards on peer's behalf. At that point, the eof-
status for the channel is already set on entry of the
handler, so no *need* to try a "gets", first.

Of course, you can put another eof-check after the gets
but that's redundant, because if eof occurred through
"gets", then the handler is immediately called again,
anyway, and that time eof is true already.

Finally, if it's a writeable event,  reading isn't a 
good idea either, nor is trying to "puts" something,
when eof was already true before.
dkfplain2008-06-16 20:40:45
Logged In: YES 
user_id=79902
Originator: NO

Your proposal will add an extra blank line at the end, and this is an example of readable events.
ferrieuxplain2008-06-16 21:06:59
Logged In: YES 
user_id=496139
Originator: NO

ad Andreas: huh ?

> fileevent-callbacks are also fired when previous gets 
> returned with no eof-condition, but the channel died 
> afterwards on peer's behalf

This is just plain wrong.
[eof] is not updated until a read operation has been tried.
To convince yourself of this:

    set n [open /dev/null r] ;# works with NUL: on Windows too
    fileevent $n readable {puts stderr EOF:[eof $n];close $n}
  => EOF:0

Hence the idiom proposed by Donal which is minimal, accurate (no extra empty line), and syscall-efficient ([eof] only reads memory).
avl42plain2008-06-16 21:59:15
Logged In: YES 
user_id=830354
Originator: YES

Oups. ok, you're right.

Let's use Donal's version instead of the one
currently in the manpage (which was even worse
than my original correction attempt)

Sorry for my previous wrong statement.
flatwormplain2008-06-16 22:32:22
Logged In: YES 
user_id=1350198
Originator: NO

While the statement regarding EOF made by Donal and Alexander is correct, since [fileevent readable] handler is *necessarily* called on a non-blocking channel when a EOF condition is encountered, the whole problem just has no meaning: when I have

proc Read chan {
  if {[eof $chan]} {
    close $chan
    ...
  } else {
    set data [read $chan]
  }
}
...
fileevent $chan readable [list Read $chan]

in my code, [Read] will be called on EOF anyway. Just the event subsystem will make one extra invocation of the callback script.

For instance, reading from a file of length 0 using a proc similar to that shown above will result in two invocations of the proc -- one for [read] which will return an empty string and one resulting from hitting EOF.

So in fact it's not a big deal where checking for EOF is performed.
patthoytsplain2008-06-24 21:44:09
Logged In: YES 
user_id=202636
Originator: NO

flatworm: some channel implementations do not issue another round through the loop. iocpsock is one such. It's rather unfortunate and should be spelled out more strongly - read, then eof check.

I've modified the fileevent.n page to provide better samples and commented on the eof testing.
Log in (anonymous is fine) to add a comment.