title: PostgreSQL frontend unexpectedly closes connection
body: <p>I'm a little bit confused with the following case.</p>
  
  <p>I've got a <code>Postgres</code> server running on host A, and a java based client running on host B. The client uses <code>org.postgresql.Driver</code> JDBC driver (version 9.1-901.jdbc3).</p>
  
  <p>sometimes while executing long running stored procedure I get exception "<code>java.net.SocketException: Socket closed</code>". I'm using <code>org.apache.commons.dbcp.BasicDataSource</code> for retrieving 
  connections. </p>
  
  <p>DBCP pool is configured with default options.</p>
  
  <p>I got tcp dump in order to figure out on which side (client or server) socket is being closed;</p>
  
  <p>Here is what I've got:</p>
  
  <pre><code>1.   Client B sends a test query message when tries to borrow connection from dbcp pool ("Select 1")
  2.   Server A sends successful response back (Type: Command completion, Ready for query)
  3.   Client B sends ACK message in response on server A response (see the item 2).
  4.   Client B sends query message to the server A.
  5.   Server A sends ACK message in response on client Query message (see the item 4).
  6.   Client B sends terminating message (Type : Termination) after some time passed (from 3 to 10 or sometimes even more minutes).
  7    Client B sends FIN ACK message to the server.
  8.   Server A sends back ACK on termination message.
  9.   Server A sends ACK on (FIN, ACK) message (item 7).
  10.  Server A sends back a response on the client query (from item 4) Type: Row description Columns: 40.
  11.  Client B sends RST message (reset).
  12.  Server A continues sending response on the query Type: Data row Length: 438 Columns 40 and so on.
  13   Client B sends RST message (reset) again.
  14.  Server A continues sending response on the query Type: Data row Length: 438 Columns 40 and so on.
  15.  Client B sends RST message (reset).
  </code></pre>
  
  <p>After that communication seems to be finished.</p>
  
  <p>After the item 6, in my client logs I got Exception like the following:</p>
  
  <pre><code>Caused by: java.net.SocketException: Socket closed
          at java.net.SocketInputStream.socketRead0(Native Method)
          at java.net.SocketInputStream.read(SocketInputStream.java:152)
          at java.net.SocketInputStream.read(SocketInputStream.java:122)
          at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:145)
          at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:114)
          at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:73)
          at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:274)
          at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1661)
          at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
  </code></pre>
  
  <p>Could you please help me to figure out the reason of such a failure. (This bug happens once per 10 successful cases.)</p>