title: stream_select timeout on newly opened socket
body: <p>I use thrift php client to connect to java thrift server. But got the error of </p>
  
  <pre><code>TSocket: timed out writing 78 bytes from 10.0.1.151:1234
  </code></pre>
  
  <p>I dig into the php source of thrift and find it was caused from timeout on function  <code>stream_select</code>.</p>
  
  <pre><code>/**
   * Write to the socket.
   *
   * @param string $buf The data to write
   */
  public function write($buf)
  {
      $null = null;
      $write = array($this-&gt;handle_);
  
      // keep writing until all the data has been written
      while (TStringFuncFactory::create()-&gt;strlen($buf) &gt; 0) {
          // wait for stream to become available for writing
          $writable = @stream_select($null, $write, $null, $this-&gt;sendTimeoutSec_, $this-&gt;sendTimeoutUsec_);
          if ($writable &gt; 0) {
              // write buffer to stream
              $written = @stream_socket_sendto($this-&gt;handle_, $buf);
              if ($written === -1 || $written === false) {
                  throw new TTransportException('TSocket: Could not write '.TStringFuncFactory::create()-&gt;strlen($buf).' bytes '.
                          $this-&gt;host_.':'.$this-&gt;port_);
              }
              // determine how much of the buffer is left to write
              $buf = TStringFuncFactory::create()-&gt;substr($buf, $written);
          } elseif ($writable === 0) {
              throw new TTransportException('TSocket: timed out writing '.TStringFuncFactory::create()-&gt;strlen($buf).' bytes from '.
                      $this-&gt;host_.':'.$this-&gt;port_);
          } else {
              throw new TTransportException('TSocket: Could not write '.TStringFuncFactory::create()-&gt;strlen($buf).' bytes '.
                      $this-&gt;host_.':'.$this-&gt;port_);
          }
      }
  }
  </code></pre>
  
  <p>That means the socket was blocked and was not able to write. But the socket is just open and should not be blocked. I try select immediately after <code>pfsockopen</code></p>
  
  <pre><code>if ($this-&gt;persist_) {
    $this-&gt;handle_ = @pfsockopen($this-&gt;host_,
                                 $this-&gt;port_,
                                 $errno,
                                 $errstr,
                                 $this-&gt;sendTimeoutSec_ + ($this-&gt;sendTimeoutUsec_ / 1000000));
  } else {
    $this-&gt;handle_ = @fsockopen($this-&gt;host_,
                                $this-&gt;port_,
                                $errno,
                                $errstr,
                                $this-&gt;sendTimeoutSec_ + ($this-&gt;sendTimeoutUsec_ / 1000000));
  }
  
  // Connect failed?
  if ($this-&gt;handle_ === FALSE) {
    $error = 'TSocket: Could not connect to '.$this-&gt;host_.':'.$this-&gt;port_.' ('.$errstr.' ['.$errno.'])';
    if ($this-&gt;debug_) {
      call_user_func($this-&gt;debugHandler_, $error);
    }
    throw new TException($error);
  }
  
  $write = array($this-&gt;handle_);
  $writable = @stream_select($null, $write, $null, $this-&gt;sendTimeoutSec_, $this-&gt;sendTimeoutUsec_);
  if ($writable === 0) {
      die('123');
  }
  </code></pre>
  
  <p>The result show that it is block right after it is opened!</p>
  
  <p>When restart php-fpm, the error disappear for a while and come up again. </p>
  
  <p>Here is the client code:</p>
  
  <pre><code>$socket = new TSocket('10.0.1.151', 1234, true); 
  $framedSocket = new TFramedTransport($socket); 
  $transport = $framedSocket;
  $protocol = new TCompactProtocol($transport);   
  $transport-&gt;open();
  $client= new userservice\UserServiceProxyClient($protocol);  
  $result = $client-&gt;findUser($id); 
  </code></pre>
  
  <p>If I adjust the php-fpm configuration <code>pm.max_children</code> from 200 to 2, the error also disappear.</p>
  
  <p>I tried increase timeout to 10 seconds and it timeout after 10 seconds.</p>
  
  <p>Any idea for what's the cause of the problem and how to fix it?</p>