Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZlEGX-00068d-TF for pgsql-jdbc@arkaria.postgresql.org; Sun, 11 Oct 2015 10:54:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZlEGX-0006a0-Ca for pgsql-jdbc@arkaria.postgresql.org; Sun, 11 Oct 2015 10:54:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZlEGW-0006ZW-JI for pgsql-jdbc@postgresql.org; Sun, 11 Oct 2015 10:54:08 +0000 Received: from smtp88.iad3a.emailsrvr.com ([173.203.187.88]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZlEGT-0003yI-LA for pgsql-jdbc@postgresql.org; Sun, 11 Oct 2015 10:54:07 +0000 Received: from smtp4.relay.iad3a.emailsrvr.com (localhost.localdomain [127.0.0.1]) by smtp4.relay.iad3a.emailsrvr.com (SMTP Server) with ESMTP id 8C23E28013F for ; Sun, 11 Oct 2015 06:54:02 -0400 (EDT) Received: by smtp4.relay.iad3a.emailsrvr.com (Authenticated sender: demolish-AT-cwgsy.net) with ESMTPSA id DB701280143 for ; Sun, 11 Oct 2015 06:54:01 -0400 (EDT) X-Sender-Id: demolish@cwgsy.net Received: from [192.168.100.3] ([UNAVAILABLE]. [80.73.221.67]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA) by 0.0.0.0:465 (trex/5.4.2); Sun, 11 Oct 2015 10:54:02 GMT Message-ID: <561A3FC8.6080305@kiwi-fraser.net> Date: Sun, 11 Oct 2015 11:54:00 +0100 From: Donald Reply-To: postgres@kiwi-fraser.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: "[JDBC]" Subject: Re: Connection terminated by the server causes deadlock in jdbc client side connection References: <0c59e097a3ec4139b049b89627fb149f@exchange.sh-solutions.de> <543c7dab3e334742b02d18dcf7b2c49a@exchange.sh-solutions.de> In-Reply-To: Content-Type: multipart/alternative; boundary="------------080601060706080900020400" X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org This is a multi-part message in MIME format. --------------080601060706080900020400 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit I think there maybe multiple bugs going on here and possibly not fixed... First I'd like to say that even though we are dealing with TCP one must treat it almost like UDP. TCP packets can be consumed by firewalls and proxy servers with no response given to the sender. So with that in mind to create a robust fault tolerant driver, timeout on all TCP communications is a must. 1) So in the first case where an IOException is thrown, the original code that is closing the connection via "close" cannot have a timeout feature enabled and that will still exist in the code with the potential to fail... 2) When an IOException is thrown as the origin of an exception this should not be lost and should always be attached to the final exception. I don't see this cascaded in the updated driver using the "abort" code... Regards Donald On 08/10/2015 18:45, Leonardo Frittelli wrote: > Steffen, > > I can't really comment on what is the behavior of the PG server, but > at least in the scenario that I am having (replication taking priority > over a local query in hot_standby) either the client does not appear > to receive an RST or Java 1.7 is still trying to write to that socket > even if the reading end is gone, or even worse: the server is not > actually closing it but simply no longer reading (Perhaps this is > indeed hinting at a bug in the server side too?) > > The stack trace does not give much detail either. This is the new > trace I get after patching the driver to use abort() instead of close(): > > ... > at java.lang.Thread.run(Unknown Source) > Caused by: org.postgresql.util.PSQLException: FATAL: terminating > connection due to conflict with recovery > Detail: User query might have needed to see row versions that must > be removed. > Hint: In a moment you should be able to reconnect to the database > and repeat your command. > at > org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198) > at > org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927) > at > org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:562) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:305) > at > com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:174) > at > org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56) > ... 42 more > > As you can see, the IO Exception which caused the PSQL Exception is > not coming up in the stack trace. > > Just for your awareness, please note that we are now using a snapshot > that Dave created for us with the change above mentioned (to use > abort() instead of close()) and we no longer see the issue now. > > Regards, > > Leonardo > > > > On 8 October 2015 at 13:46, Steffen Heil (Mailinglisten) > > wrote: > > Hi > > > > I was referring to the fact that the TCP connection is indeed > left 'half > > open' > > with the jdbc client side still assuming it's 'properly' open > and thus > > trying to > > send a graceful close command over the socket ("X" in v2 or "X4" > in v3) . It > > is > > this action that indefinitely blocks the client thread, which is > then no > > longer > > usable by the application. > > But it should not block indefinitely. Why would it? > If the server closed the connection, it should reply with a RST > and the client > should detect that and throw an IOException( "Socket closed" ). > > > > All of this said, I still think it is conceptually incorrect to > attempt a > > graceful > > closure after an IO error in the client. > > Agreed. IOExceptions on blocking sockets usually indicate that you > cannot do > anything good with them anymore. > > > Regards, > Steffen > > > --------------080601060706080900020400 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit I think there maybe multiple bugs going on here and possibly not fixed...
First I'd like to say that even though we are dealing with TCP one must treat it almost like UDP. TCP packets can be consumed by firewalls and proxy servers with no response given to the sender. So with that in mind to create a robust fault tolerant driver, timeout on all TCP communications is a must.

1) So in the first case where an IOException is thrown, the original code that is closing the connection via "close" cannot have a timeout feature enabled and that will still exist in the code with the potential to fail...

2) When an IOException is thrown as the origin of an exception this should not be lost and should always be attached to the final exception. I don't see this cascaded in the updated driver using the "abort" code...

Regards
Donald



On 08/10/2015 18:45, Leonardo Frittelli wrote:
Steffen,

I can't really comment on what is the behavior of the PG server, but at least in the scenario that I am having (replication taking priority over a local query in hot_standby) either the client does not appear to receive an RST or Java 1.7 is still trying to write to that socket even if the reading end is gone, or even worse: the server is not actually closing it but simply no longer reading (Perhaps this is indeed hinting at a bug in the server side too?)

The stack trace does not give much detail either. This is the new trace I get after patching the driver to use abort() instead of close():

...
        at java.lang.Thread.run(Unknown Source)
Caused by: org.postgresql.util.PSQLException: FATAL: terminating connection due to conflict with recovery
  Detail: User query might have needed to see row versions that must be removed.
  Hint: In a moment you should be able to reconnect to the database and repeat your command.
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:562)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:305)
        at com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:174)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
        ... 42 more

As you can see, the IO Exception which caused the PSQL Exception is not coming up in the stack trace. 

Just for your awareness, please note that we are now using a snapshot that Dave created for us with the change above mentioned (to use abort() instead of close()) and we no longer see the issue now.

Regards,

Leonardo



On 8 October 2015 at 13:46, Steffen Heil (Mailinglisten) <lists@steffen-heil.de> wrote:
Hi


> I was referring to the fact that the TCP connection is indeed left 'half
> open'
> with the jdbc client side still assuming it's 'properly' open and thus
> trying to
> send a graceful close command over the socket ("X" in v2 or "X4" in v3) . It
> is
> this action that indefinitely blocks the client thread, which is then no
> longer
> usable by the application.

But it should not block indefinitely. Why would it?
If the server closed the connection, it should reply with a RST and the client
should detect that and throw an IOException( "Socket closed" ).


> All of this said, I still think it is conceptually incorrect to attempt a
> graceful
> closure after an IO error in the client.

Agreed. IOExceptions on blocking sockets usually indicate that you cannot do
anything good with them anymore.


Regards,
  Steffen




--------------080601060706080900020400--