From: [1]Benjamin Goldberg ([2]goldbb2@earthlink.net) Subject: New FAQ idea. Newsgroups: [4]comp.lang.perl.misc Date: 2002-11-16 21:35:19 PST One question that is frequently asked: Why does my IO deadlock, even though I'm using IO::Select or Tk::fileevent? You've probably got some code which does something like this: $MW->fileevent( $socket, 'readable', sub { my $line = <$socket> or $MW->fileevent( $socket, 'readable', ''), return; # process $line } ); Or like this: my $select = IO::Select->new($socket); while( () = $select->can_read($timeout) ) { my $line = <$socket> or last; # process $line } The reason for the problem is that <> buffers data, but when IO::Select and Tk::filehandle test for readability, they can only see the underlying *unbuffered* filedescriptor, and the data that gets buffered confuses them. The solution is to use the IO operator which works directly on the underlying unbuffered filedescriptor (sysread), and do your buffering yourself, in a seperate variable. Be very careful that sysread is only called *once* for each time that you've been told that a handle is readable -- otherwise, it's quite possible that when the second call is made, no data will be there, and you'll get blocked. Change your code to be like one of the following: my $buffer = ""; $MW->fileevent( $socket, 'readable', sub { sysread( $socket, $buffer, 8192, length $buffer ) or $MW->fileevent( $socket, 'readable', '' ), return; while( $buffer =~ s/^(.*\n)// ) { my $line = $1; # process $line } } ); Or like this: my $select = IO::Select->new($socket); my $buffer = ""; while( () = $select->can_read($timeout) ) { sysread( $socket, $buffer, 8192, length $buffer ) or last; while( $buffer =~ s/^(.*\n)// ) { my $line = $1; # process $line } } It's important to be aware that one call to sysread can return less than one line, or it could return more than one line. By using a while loop in this manner, both cases are dealt with equally well.