open() with pipe breaks AnyEvent STDIN poll

Nick Andrew nick at nick-andrew.net
Wed Nov 26 08:10:43 CET 2014


I found a problem where a working AnyEvent script broke badly because
of some code in a 3rd party module called from the script. I isolated
it to the call to open(CMD, "command |").

Here's my test script:

	#!/usr/bin/perl

	use strict;
	use warnings;

	use AnyEvent;

	my $fail = 1;

	my $cv = AnyEvent->condvar;

	my $w;

	$w = AnyEvent->io(
		fh => \*STDIN,
		poll => 'r',
		cb => sub {
			print STDERR "Called cb\n";
			my $line = <STDIN>;
			if (defined $line) {
				print STDERR "Read line: <$line>\n";
				if ($fail) {
					open(CMD, "/bin/echo hello|");
					while (<CMD>) {
						print "Hello: $_";
					}
					close(CMD);
				}
			} else {
				print STDERR "EOF\n";
				undef $w;
				$cv->send;
			}
		}
	);

	$cv->recv;

When $fail == 1, the script correctly reads and echoes only the first line
typed, when a second line is entered the script enters a tight select()
loop polling for fd 0 (which is readable, as it has a line pending) but
it doesn't call the function to read and process that line.

I have tested this with the latest AnyEvent (7.07) and perl 5.10 and 5.18.
Is this a bug in AnyEvent, or some perl niggle which can't be solved (in
which case it deserves a big fat warning somewhere in the AnyEvent documentation)?

Nick.
-- 
PGP Key ID = 0x418487E7                      http://www.nick-andrew.net/
PGP Key fingerprint = B3ED 6894 8E49 1770 C24A  67E3 6266 6EB9 4184 87E7



More information about the anyevent mailing list