Technical article 107592

My first write to a closed socket succeeds, but the second write fails.

OpenServer 5.0.0

Written · Last revised

Problem

I have a client/server socket-based application.  I find that
if I issue a close() at one end and then attempt a write() to
the socket at the other, this first write() succeeds, even though
I know that the remote process has issued the close() and has
not received the data.  However, the second write() fails,
as I might have expected the first to do, with EPIPE.

Cause

When a process initiates the close of a socket connection by
issuing a close() (or by being terminated), its TCP sends
a segment to its peer with the FIN bit set, and closes data
flow in the direction to the remote process.  The remote TCP
responds by sending back a segment with ACK.  However, data
flow in the opposite direction is not closed until this remote
process takes explicit action to do so by issuing a close()
of its own.  If this process performs a write() to the socket,
it will succeed.  However, the TCP that had sent the initial
FIN will send RST (reset connection) upon receiving the written
data, since by TCP design, RST will be elicited by TCP if the
connection at that end has been closed (by a close() of the
socket descriptor and/or an exit() of the associated process).
Since writing to a connected socket that has received RST causes
SIGPIPE to be sent to the writing process, the subsequent write()
will fail with EPIPE.
This can be clarified further by considering two processes, A
and B, which communicate via TCP sockets over a TCP/IP network.
If process A is the writing process and process B is the one that
issues a close(), then the receipt of a FIN by A from its peer
does not tell A that the other process has really gone away or
even issued just a close().  To A at this stage, B has no more
data to send, but it can still receive data.  This is why the
first write() succeeds, although, of course, the data never gets
to B; the TCP servicing B knows that B has already closed its
socket, and therefore sends RST to its peer.  Thus A can deduce
this remote closure by performing another write() (resulting in
EPIPE), and can complete the full-duplex closure by itself
issuing a close() on its socket.

Solution

The above describes a typical situation in which the writing
process must know immediately if the data that it writes to the
remote process has been properly received and handled.  Clearly,
in the above case, the success of the write() (whether it returned
without error) is inadequate to evaluate this.  However, these
facts are known about what happens when TCP connections terminate
in the manner described above:
1. The writing process (process A) knows that the remote process
(process B) closed its connection if it performs a write() and
this call fails with EPIPE.  This could be because it is the
second write() to this closed connection,or the third, fourth,
or tenth write(), for that matter.

2. If process A performs a read(), then the result depends upon the state of process B:

If process B is busy, the read() will block.

If process B had closed its socket (causing FIN to be sent), the read() will succeed, but with zero bytes read.

If process B had closed its socket and RST had been sent to process A's TCP, the read() will succeed with zero bytes read.

If process B still had its socket open, the read() will return the number of bytes last written by process A, assuming that read buffer in B is large enough.

Using these facts, a "user protocol" can be built into processes A and B, complimenting the TCP handshake described above (which uses FIN, ACK, and RST), in order to inform process A whether the data that it attempts to write to process B is actually being read by B.

Thus, process A can implement the following logic:

if (write(data intended for B) == EPIPE)
{ /* RST sent due to previous write(), remote connection closed */ }
else
{ if (read(ack from A))
  { /* write() successful, continue with A tasks */ }
  else if ( /* zero bytes read */ )
  { /* FIN or RST sent, remote connection closed, so... */
    if (write(data intended for B) == EPIPE)
    { /* RST sent due to previous write(), remote connection
         closed */ }
    else ( /* FIN sent due to previous close(), write() again
              to verify RST sent due to previous write() */ )
  }
  else if ( /* other read() error */ )
  { /* exit due to read() error  */ }
  else ( /* read() hanging */ )
  { /* timeout, try again later */ }
}

Process B, on the other hand, simply waits to read the data (assuming it didn't issue the close() yet), and writes an acknowledgement if this read is successful:

if (read(data intended for B)) { /* got data, send ackowledgement */ if (write(acknowledgement)) { /* acknowledgement sent */ } else { /* other write() error; exit */ } } else if ( /* other read() error */ ) { /* exit due to other read() error */ } else ( /* read () hanging */ ) { /* timeout, try again later */ }

From the Xinuos technical article database, record 107592. Reproduced with permission. The original is at www.sco.com/ta/107592, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.