Event-polling after a fork

Felipe Gasper felipe at felipegasper.com
Fri Feb 28 20:10:15 CET 2020



> On Feb 28, 2020, at 1:48 PM, Marc Lehmann <schmorp at schmorp.de> wrote:
> 
> On Thu, Feb 27, 2020 at 07:03:16AM -0500, Felipe Gasper <felipe at felipegasper.com> wrote:
>> Once I took a step back, I realized that because AE’s internal references to the watchers are weakened, AnyEvent->reset() would only remove those references and not fire any DESTROY handlers. So yeah, it would serve no purpose.
> 
> I think you are looking at the wrong code - AnyEvent itself does not keep
> any references, weak or not, to watchers.
> 
> However, even if it only stored weak references, that wouldn't stop it
> from resetting weatchers, if it really wanted and it would be useful.

Sorry, yes: to clarify, I looked only at AE::Loop.

> 
>> (With Mojo the reset() method works because Mojo::IOLoop stores normal references.)
> 
> Since you never really defined what "works" means, it's hard to assess
> the validity of that statement, buit it certainly doesn't work for
> any practical use case, because reste resets all watchers, those that
> need resetting, and those that don't, so it would break otherwise
> correctly-written programs. In other words, no, it doesn't "work", by
> design.

If I take your meaning, you’re saying that, for example, epoll would break because after a fork the two processes share the same epoll. That seems solvable enough in a backend that’s aware it uses epoll rather than select/poll: just make that implementation of reset() close the epoll rather than removing individual watchers from the epoll.

e.g.:
https://metacpan.org/release/Mojo-Reactor-Epoll/source/lib/Mojo/Reactor/Epoll.pm#L108

By “work”, I mean simply that the child does evented I/O independently of the parent.

-F


More information about the anyevent mailing list