Unlinking of AF_UNIX sockets

doug at hcsw.org doug at hcsw.org
Wed Jul 4 07:10:11 CEST 2012


Hi Marc and Co,

First off, thank you very much for AnyEvent.

I'm having a problem with (I think) this change:

6.01  Fri Aug 26 07:04:11 CEST 2011
        - tcp_server now tries to unlink unix domain sockets when it gets destroyed.

My program listens on an AF_UNIX socket, accepts
connections and then forks off "worker" processes to
handle them. The workers don't use AnyEvent at all but
when they are done they exit and the guard installed
by AnyEvent::Socket unlinks the unix socket.

I was hoping that the parent process could continue
listening on the unix socket and accept more connections.

I don't seem to be able to get access to the guard to
cancel it since it's in a lexical %state variable in
tcp_server().

Luckily in my case the workers can run POSIX::_exit()
when they are done and therefore bypass the guard, but
it would be ideal if they were given a chance to flush
log buffers and otherwise exit normally.

Here is a really trivial patch I came up with that
solves it for me:


diff --git a/lib/AnyEvent/Socket.pm b/lib/AnyEvent/Socket.pm
index fe317a5..107adc2 100644
--- a/lib/AnyEvent/Socket.pm
+++ b/lib/AnyEvent/Socket.pm
@@ -1183,10 +1183,11 @@ sub tcp_server($$$;$) {
    if ($af == AF_UNIX) {
       my $fh  = $state{fh};
       my $ino = (stat $fh)[1];
+      $state{orig_pid} = $$;
       $state{unlink} = guard {
          # this is racy, but is not designed to be foolproof, just best-effort
          unlink $service
-            if $ino == (stat $fh)[1];
+            if $ino == (stat $fh)[1] && $state{orig_pid} == $$;
       };
    }


Having access to the guard so I could cancel it or an
option to not install it in the first place would also
work fine for me.

Let me know if further code or test-cases would help.

Cheers,

Doug



More information about the anyevent mailing list