CyTS library stalling

On our guild website, we have a block in the portal that shows who is currently on teamspeak. This uses the CyTS library to connect and get such information and occasionally it would cause the portal to stop loading.

In each case I would get home, load it up and not find any problems at all. Well today was a different matter, I was able to pin point the exact issue. For some reason the library connects to the server but then stalls when receiving information from the server.

I should tell you know, this is not a perfect fix but it does the job. I did look into other options like switch socket blocking on and off and such other approaches, but it seems that if fgets reads more than 1 byte, it stalls. This is a bug in PHP that has hung around for a while.

I have changed the _readcall function to read the first byte, check it for failure then continue with the rest.

function _readcall()
{
	if (!is_resource($this->sCon))
		return false;

	// HACK : Workaround to fail fast
	$first = fgets($this->sCon, 1);

	if($first === false)
		return false;

	$sRead = '';
	do
	{
		$cRead = $first.fgets($this->sCon);
		$first = '';

		$sRead .= $cRead;
	} while ($cRead != CYTS_SYN && $cRead != CYTS_OK && strtoupper(substr($cRead, 0, 5)) != "ERROR");
	return $sRead;
}

Leave a Reply