Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WxTH7-0006Hs-RJ for pgsql-odbc@arkaria.postgresql.org; Thu, 19 Jun 2014 03:44:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WxTH7-0000QJ-A2 for pgsql-odbc@arkaria.postgresql.org; Thu, 19 Jun 2014 03:44:33 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WxTH6-0000Nk-3w for pgsql-odbc@postgresql.org; Thu, 19 Jun 2014 03:44:32 +0000 Received: from mout.kundenserver.de ([212.227.17.13]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WxTH2-0003Jt-TN for pgsql-odbc@postgresql.org; Thu, 19 Jun 2014 03:44:30 +0000 Received: from ayaki.localdomain (58-7-63-35.dyn.iinet.net.au [58.7.63.35]) by mrelayeu.kundenserver.de (node=mreue101) with ESMTP (Nemesis) id 0MXYog-1XBAPX1pRe-00WZnq; Thu, 19 Jun 2014 05:44:26 +0200 Message-ID: <53A25C95.707@2ndquadrant.com> Date: Thu, 19 Jun 2014 11:44:21 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: "pgsql-odbc@postgresql.org" Subject: Protocol de-synchronisation bug, bogus query sent X-Enigmail-Version: 1.6 Content-Type: multipart/mixed; boundary="------------050406040600070205010508" X-Provags-ID: V02:K0:4YGlywy9fx9LR6pdrFRibxnQ4J2pbtbpx3rZJM2EmYC O00lqVzz1fIoWnt7kLDNQOfikK3wP9bCvGRBay1OoLHIGWEICy S67zd612rLWao8HYysufkAKMamL30kijyzCfaHqMlROTI/IuuI 3C511HcMVJXxVmcuiC1kgt/5kPlR83s0NoLpPRvT1Je+b4iiEV i0wBbQIQl7oB8vFQ9qfE33/FL5+dBbhBgI+MavPL2fnWWYBbL0 hEV3Yak3oHgAKsrW5hoUFYXPZelh5AW5od5XX1SZ73reOk+Sn2 Gxs0q6bu1JQw0WPsPmRiGlxZrKaYr8rDX1j4e1yOQO53OD7s0W s0wM44zWxNq5eGGSXjd0= X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------050406040600070205010508 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi folks I've found a bug in psqlODBC, in CC_send_query_append, where psqlODBC gets out of sync with the server's idea of the connection state and both wait for each other indefinitely. So far I've only reproduced it as part of debugging a larger issue. I've only actually seen it when psqlODBC establishes an "isolated" XA connection after failing to acquire a lock on the DTC connection in IAsyncPG::getLockedXAConn(). If the client has specified a connect-time query string with a DSN like: DRIVER={PostgreSQL ANSI};SERVER=LOCALHOST;DATABASE=postgres;A6=set search_path to someschema;UID=;PWD=;BI=-5;debug=1;commlog=1; ... but it's clearly a bug in its own right, so I'll write it up here and propose a fix. Patch attached, or you can pull the branch: fix-febe-protocol-desync-emptyquery from https://github.com/ringerc/psqlODBC.git The whole code block around connection.c:3082 appears to be attempting to send an empty query in order to elicit an 'I' (EmptyQueryResponse) message. It sends a malformed protocol message: SOCK_put_string(self->sock, "Q "); SOCK_flush_output(self->sock); so the server just waits for the rest of the message, never responding. When the connection is killed by program exit later, the server will log: LOG: unexpected EOF within message length word LOG: disconnection: session time: 0:00:08.910 user=Administrator database=XXXX host=127.0.0.1 port=53705 LOG: could not receive data from client: No connection could be made because the target machine actively refused it. That's because the V3 protocol specifies that the message format for Query is: 'Q' [32 bit integer] [query string] http://www.postgresql.org/docs/current/static/protocol-message-formats.html ... but psqlODBC just sends: 'Q' '\x20' '\x00' If it is really necessary to go through this empty-query dance, then for the V3 protocol the correct message would be: /* Send an empty query to elicit an 'I' (EmptyQueryResponse) * from the server */ SOCK_put_char(self->sock, 'Q'); SOCK_put_int(self->sock, 5, 4); SOCK_put_char(self->sock, '\0'); Since no 'I' message is ever received the next message is 'Z' (ReadyForQuery). psqlODBC consumes this and carries on waiting for the next message, as it has set emptyreqs=1 to expect 'I' messages. So it waits forever, since the server thinks it's waiting for psqlODBC to finish sending the prior protocol message. It isn't clear to me why psqlODBC does this weird dance with empty queries at all, but it's certainly doing it wrong. The attached patch fixes the immediate problem, but I'd like to see if this can be simplified out entirely in favour of, in ascending size-of-work order, (a) using a protocol sync message, then (b) deleting as much as possible of this custom protocol handling code entirely and using libpq. -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------050406040600070205010508 Content-Type: text/x-patch; name="0001-Fix-FE-BE-protocol-loss-of-sync-due-to-invalid-empty.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Fix-FE-BE-protocol-loss-of-sync-due-to-invalid-empty.pa"; filename*1="tch" From 60d2a2b4f3a386c885bfbbee840dcb388b98a5f6 Mon Sep 17 00:00:00 2001 From: Craig Ringer Date: Thu, 19 Jun 2014 11:38:35 +0800 Subject: [PATCH] Fix FE/BE protocol loss of sync due to invalid empty query The V3 protocol specifies that the message format for Query is: 'Q' [32 bit integer] [query string] but psqlODBC was sending "Q \0", causing the server to wait for the second half of the length word forever. At connection close, the server would emit: unexpected EOF within message length word This issue was observed while tracking down problems with XA transactions in MSDTC failing to commit after a successful prepare because an isolated tx created via getLockedXAConn() got stuck here, but is not confined to that problem. --- connection.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/connection.c b/connection.c index 52f5463..f33a6a4 100644 --- a/connection.c +++ b/connection.c @@ -3081,7 +3081,22 @@ inolog("Discarded the first SAVEPOINT\n"); if (empty_reqs == 0) { - SOCK_put_string(self->sock, "Q "); + /* + * Send an empty query to elicit an 'I' (EmptyQueryResponse) + * message from the server. This gives us a synchronisation point + * with the server, though it isn't clear why the code doesn't just + * use 'S' (Sync). + */ + SOCK_put_char(self->sock, 'Q'); + if (PROTOCOL_74(ci)) + { + SOCK_put_int(self->sock, 5, 4); + SOCK_put_char(self->sock, '\0'); + } + else + { + SOCK_put_string(self->sock, " "); + } SOCK_flush_output(self->sock); empty_reqs++; } -- 1.9.0 --------------050406040600070205010508 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------050406040600070205010508--