Page 1 of 1

socket and TIME_WAIT issue

Posted: Sat Feb 28, 2004 1:50 am
by itbegary
Hello,

While working on a threaded tcp daemon I ran into a litlte snag. This daemon forks childrean for incoming connections. What I found was that after making many connections to the daemon I started to see a backlog of TIME_WAIT connections. I noticed that other applications also do this. When I stop the daemon any active children are killed which is done by using SIGUSR1. The connection goes from ESTABLISHED to TIME_WAIT with a 60 second interval. Because this timeout is in effect I cannot start the daemon again until all of these dead processes connections finally die.

After this child connection has been accepted this is the code that is ran:

Code: Select all

<?php
	socket_setopt($activesock->sock, SOL_SOCKET, SO_KEEPALIVE, 1);
	//socket_setopt($activesock->sock, SOL_SOCKET, SO_REUSEADDR, 1);
	socket_getpeername($activesock->sock, $activesock->clienthost, $activesock->clientport);
	socket_write($activesock->sock, "hello $activesock->clienthost .\r\n");
	socket_set_nonblock($activesock->sock);		
	socket_write($activesock->sock,"Hi Jack, I have to go now.\r\n");
	// Do stuff loop to be implemented later
	socket_close($activesock->sock);
	unset($activesock->sock);

?>
I have tried keep_alive but it doesn't seem to help. Specifying a value of SO_REUSEADDR = 0 doesn't seem to do much either. I've notice that these TIME_WAIT connections occur under mysql and apache but when you stop the process they are killed. At first I though that the socket may have stayed open waiting for some data so I removed the original socket_read.

Is there something simple that I am missing?

Posted: Sat Feb 28, 2004 10:41 am
by Weirdan
http://www.skynet.ie/~heathclf/fyp/fyp-interim/node13.html wrote: As with the original TCP, the host that sends the first FIN is required to remain in the TIME_WAIT state for twice MSL once the connection is completely closed at both ends. This implies that the TIME_WAIT state with the original TCP is 240 seconds
TIME_WAIT is required to ensure that no segments from previous session can arrive to new reincarnation of your daemon.

Posted: Sat Feb 28, 2004 10:47 am
by itbegary
I see what the article is referring to and it's exactly what's happening. What I have noticed though is things like apache seems to be able to kill their oddball connections that are a time_wait state. What I'd llike to do is to kill all of those connections on exit.

Is this possible?