State corruption in AnyEvent::Loop

Yorhel info at yorhel.nl
Sat Jun 27 13:20:38 CEST 2020


Hi,

On 2020-06-27, Felipe Gasper wrote:
> Have you dug into AE’s Perl loop to see if there’s a potential fix
> there? Looks like maybe something setting $_ that could use a lexical
> instead?
> 
> The interpreter has changed some of the edge-case semantics of $_ in
> recent years; that may be why the breakage only manifests in newer
> versions.

Wow, you're right. I hadn't considered that case because I was under the
impression that a while loop over a filehandle would always use a
new localized $_, but that appears to be not the case. Changing the
'while' line to a simple '$_=1' also triggers the behavior.

Digging a bit further, it is indeed fixable in AnyEvent::Loop with the
attached patch.

Thanks for the tip!

Yorhel.


> > On Jun 27, 2020, at 06:20, Yorhel <info at yorhel.nl> wrote:
> > 
> > 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.
> > <event-loop-corruption.pl>
> > <event-loop-corruption.out>
> > _______________________________________________
> > anyevent mailing list
> > anyevent at lists.schmorp.de
> > http://lists.schmorp.de/mailman/listinfo/anyevent
> 
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: event-loop-corruption.patch
Type: text/x-diff
Size: 571 bytes
Desc: not available
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20200627/5c4a7ffe/attachment.patch>


More information about the anyevent mailing list