Issue with AnyEvent::AIO

Marc Lehmann schmorp at schmorp.de
Fri Jun 30 05:08:55 CEST 2023


On Thu, Jun 29, 2023 at 03:24:54PM +0200, Dominik Csapak <d.csapak at proxmox.com> wrote:
> We have an issue with AnyEvent::AIO, basically we don't use it directly, but as
> soon as it's installed on a machine, our AnyEvent based daemon (a http-sever basically)
> uses 100% cpu time on one core (per forked subprocess).

Hi!

IF the event loop is initialised before fork (which happens during
AnyEvent->condvar in your example, and generally in the first call), you
must not use it after fork. In general, after fork, you need to either
call exec or _exit (not exit!), without doing much in between.

See the FORK section in the AnyEvent manpage.

The specific reason why AnyEvent::AIO seems to make a difference is
because it also registers an I/O watcher during AnyEvent initialisation,
not because it does anything special.

As a demo, if you replace the "return 1" in fork_child by POSIX::_exit 1,
the program finishes, as you don't call into AnyEvent in the child.

While some event loops (such as EV) can be made to work after fork
even when initialized, this generally requires careful control, as you
typically have to be certain about what watchers you want to be active
still - it's generally not worth it to try, and in any case, very fragile.

And this is genrally no help for AnyEvent, as it has to work with other
event loops as well.

If you need an event loop in the child, you can try to have a look at
AnyEvent::Fork, which creates subprocesses without forking your main
proccess.

This will also help with lots of other problems, such as graphical
toolkits, threads and so on, which all don't support fork except in a very
limited way.

-- 
                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