0

I'm trying to close socket on windows from closesocket(), but it takes 20 sec to complete. at first I thought it was about linger interval, although I was not setting anything with setsockopt by linger, so I added such code:

linjer lobj;
lobj.l_onoff = 1;
lobj.l_linger = 0;

sz = sizeof(lobj);

setsockopt(s_, SOL_SOCKET, SO_LINGER, (char *) &lobj, sz);

but it still does not help.

any ideas? I just want to close connection, doesnt matter if its gracefull or abortion, just want to close it as soon as possible.

P.S. it takes exactly 20 sec.

lobj.l_onoff = 1; 
lobj.l_linger = 0; 
sz = sizeof(lobj); 
setsockopt(s_, SOL_SOCKET, SO_LINGER, (char *) &lobj, sz);
lobj.l_onoff = -1;
lobj.l_linger = -1; 
getsockopt(s_, SOL_SOCKET, SO_LINGER, (char *) &lobj, &sz); 
log << "Option 1:" << lobj.l_linger << ".\n"; 
log << "Option 2:" << lobj.l_onoff << ".\n";
closesocket(s_);

this code prints option1 = 0 and option2 = 1, so it really sets option correctly.

Also, observing from wireshark, it sends RST at the beginning of whole delay.

plus, closesocket() returns 0.

P.S. I have set SO_REUSADDR, can it be causing it?

Zhani Baramidze
  • 1,407
  • 1
  • 13
  • 32

1 Answers1

1

If you can't post the code you can't ask the question here. Those are the rules.

However the only way closesocket() can take any measurable time at all is if:

  • there is a lot of pending outgoing data, and
  • you have set a positive SO_LINGER timeout.

You can only get a delay of 20 seconds by setting a positive SO_LINGER timeout of >= 20 seconds and having a lot of pending outgoing data and probably a peer that isn't reading.

If you had left SO_LINGER strictly alone, which you should, or set it to false, or true with a zero timeout, closesocket() is asynchronous and returns immediately, and in the latter case you would also have reset the connection.

Ergo either you haven't done what you claimed or your observations are faulty. Possibly it is the final send() which is blocking.

It would be interesting to know whether this closesocket() call returned -1 and if so what the error value was.

user207421
  • 305,947
  • 44
  • 307
  • 483