[erlang-questions] how to flush a gen_tcp socket on close?

Per Hedeland <>
Mon Apr 2 21:30:06 CEST 2012

Matthias Lang <> wrote:
>On Monday, April 02, Andreas Schultz wrote:
>> gen_tcp sockets do buffer sends internaly. It seems, when
>> calling close on them, the buffered data is not flushed out,
>> but simply discarded .
>> I was expecting that a close on an socket with outstanding
>> send data would block until that data has been send, but that
>> seems not to happen. In fact, that might be a bug????
>I would also expect a socket to send all data. So your description
>of what actually happens is surprising.

+1. I would actually go as far as saying that gen_tcp would be unusable
for many applications if close/1 discarded buffered data. *Blocking* on
close is another thing though, I don't expect that - the C API doesn't
block on close either, you can ask for it via setsockopt(SO_LINGER), but
it isn't the default.

>Can you provide a minimal but complete code which demonstrates what
>you're seeing?

+1 again.

And to comment on the other reply, using shutdown/2 should most
certainly not be needed for this - the only purpose of using that is to
do a "half close".

--Per Hedeland

More information about the erlang-questions mailing list