open() with pipe breaks AnyEvent STDIN poll
Marc Lehmann
schmorp at schmorp.de
Wed Nov 26 13:26:47 CET 2014
On Wed, Nov 26, 2014 at 06:10:43PM +1100, Nick Andrew <nick at nick-andrew.net> wrote:
> 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 |").
The callback modifies $_, which is a global variable, without restoring it,
so code outside the watcher will have its loop variable destroyed.
If you run with AE_STRICT=1 (highly recommended during development),
AnyEvent will diagnose this for you:
Modification of a read-only value attempted at ... line 25.
This is, strictly speaking, a bug in the 3rd party module - it shouldn't
trash variables in use by the caller. The workaround is easy though, just add
a local $_ before you call it.
> which case it deserves a big fat warning somewhere in the AnyEvent documentation)?
There is one in bold letters in the WATCHERS section (and the perlop
docs also mention that $_ isn't localised by while and you would need an
explicit local $_ for that):
Note that callbacks must not permanently change global variables
potentially in use by the event loop (such as $_ or $[) and that
callbacks must not "die". The former is good programming practice in
Perl and the latter stems from the fact that exception handling differs
widely between event loops.
However, using AE_STRICT during development will catch these, unlike
reading the warning (3rd party library coders wil unlikely read warnings
in - for them - unrelated modules).
It is probably useful to report the bug to the 3rd party lib, as modifying
the caller's $_ variable is likely going to break other code as well and
is usually hard to diagnose (outside of AnyEvent's strict mode :).
--
The choice of a Deliantra, the free code+content MORPG
-----==- _GNU_ http://www.deliantra.net
----==-- _ generation
---==---(_)__ __ ____ __ Marc Lehmann
--==---/ / _ \/ // /\ \/ / schmorp at schmorp.de
-=====/_/_//_/\_,_/ /_/\_\
More information about the anyevent
mailing list