Re: UNIX Sockets, unlink() and accept()

From: beltorak (beltorak_at_ananzi.co.za)
Date: 08/02/03


Date: 1 Aug 2003 23:40:39 -0700

Russ Lewis <spamhole-2001-07-16@deming-os.org> wrote in message news:<I_BWa.278$9M3.42712@news.uswest.net>...
> I am using UNIX sockets for some interprocess communication. I have a
> thread in the server blocking on accept().
>
> What I wanted to do was to be able, from another thread (perhaps even
> another process) to tell the server to cleanly terminate. I was
> figuring that I could do this by unlink()ing the socket; I figured that
> this would cause accept() to terminate with an error.
>
> However, this is not the case; apparently, since the server still has
> the open socket to the inode, the inode is not cleaned up and the server
> continues to wait. Thus, the blocked thread will block forever (until I
> Ctrl-C it).
>
> Is there a better way to do this?
>
> If it happens to matter, I am using Linux 2.4.x.
>
> Russ Lewis

Correct; according to the man page, the socket remains open and
available for use until the last process using it closes it.

Solution: write a signal handler for either HUP or USR1 to close the
socket and terminate. Install the signal handler after you create the
socket, then send that signal to the process/thread to cleanly
terminate. The signal handler will be executed even if the process is
blocking on read.

man setsigaction for more info.

-t.



Relevant Pages

  • Re: Dangers of intercepting SIGINT?
    ... I used signal(SIGINT,my_handler) to intercept SIGINT. ... it must close it's socket and exit. ... phases of establishing a server communication ... in a signal are very limited, you should make things in signal handler ...
    (comp.os.linux.development.apps)
  • Re: How to terminate a socket in CLOSE_WAIT state
    ... FTP Server fixed for certain FTP clients who use both passive ... This was causing a PASSIVE opened socket to be left ... but instead expect the "half close" from the receiver. ... this sends a TCP/IP FIN packet to the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Applet Hangs when submitting data to servlet
    ... to put a time limit on my socket connections and socket reads. ... public void setTimeoutthrows UnknownHostException, ... on our web server. ... private JButton theSubmitButton_, theClearButton_; ...
    (comp.lang.java.programmer)
  • Re: Problem with writing fast UDP server
    ... UDP packets per second. ... socket and threads. ... I wrote a simple case test: client and server. ... The maximum theoretical limit is 14,880 frames per ...
    (comp.lang.python)