State corruption in AnyEvent::Loop

Yorhel info at yorhel.nl
Sat Jun 27 12:19:45 CEST 2020


Hi,

Please consider this as a heads up rather than a bug report, because I
strongly suspect that this is a bug in perl rather than in AnyEvent.

What I've observed is that it is possible to corrupt the internal state
of AnyEvent's pure-perl event model by breaking out of a while loop over
a filehandle, i.e. `while(<$FH>) { last }` is enough to cause havoc.

I've attached a minimal script with which I can reproduce the problem on
perl 5.30.3 x86_64-linux with AnyEvent 7.17. The problem does not occur
when the EV model is used. I suspect the problem also does not occur on
earlier perl versions, seeing as someone else has gotten the exact same
error message almost 3 years ago:

https://stackoverflow.com/questions/45622484/does-anyevent-work-with-perl-5-26

(Why was that even reported on Stack Overflow rather than the proper
channels? sigh...)

The test script has two useful-but-commented-out 'warn' lines,
uncommenting them (and patching AnyEvent::Loop to expose @fds) clearly
shows that the 'while' line is modifying internal state of AnyEvent.
I've included the output that gave on my machine.

I'm a bit at a loss here. I'll try to report this at perl upstream
if/when I manage to create a minimal example that exposes the bug
without AnyEvent. Until then, using the EV model or writing the
following instead will do as workaround:

  while(local $_ = <$FH>) {..}

I'm curious to hear if anyone else has seen similar issues or can
provide more insight into this behavior.

Thanks,

Yorhel.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: event-loop-corruption.pl
Type: text/x-perl
Size: 1006 bytes
Desc: not available
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20200627/0842837c/attachment.pl>
-------------- next part --------------
Pre 1 $VAR1 = [
          [
            '',
            [
              undef,
              undef,
              undef,
              [
                bless( [
                         3,
                         0,
                         sub { "DUMMY" },
                         0
                       ], 'AnyEvent::Loop::io' )
              ]
            ]
          ],
          []
        ];
Post 1 $VAR1 = [
          [
            '',
            [
              undef,
              undef,
              undef,
              [
                bless( [
                         3,
                         0,
                         sub { "DUMMY" },
                         0
                       ], 'AnyEvent::Loop::io' )
              ]
            ]
          ],
          []
        ];
Pre 2 $VAR1 = [
          [
            '',
            [
              undef,
              undef,
              undef,
              [
                bless( [
                         3,
                         0,
                         sub { "DUMMY" },
                         0
                       ], 'AnyEvent::Loop::io' )
              ]
            ]
          ],
          []
        ];
Post 2 $VAR1 = [
          [
            '',
            [
              undef,
              undef,
              undef,
              [
                'a
'
              ]
            ]
          ],
          []
        ];
Can't use an undefined value as a subroutine reference at /home/yorhel/.perl5/lib/perl5/x86_64-linux/AnyEvent/Loop.pm line 248, <$F> line 2.


More information about the anyevent mailing list