Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1couEH-0006AO-2p for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Mar 2017 15:55:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1couEG-0006my-Jj for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Mar 2017 15:55:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1couCn-00048K-0Y for pgsql-hackers@postgresql.org; Fri, 17 Mar 2017 15:54:17 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1couCe-0007p1-6T for pgsql-hackers@postgresql.org; Fri, 17 Mar 2017 15:54:15 +0000 Received: by mail-wm0-x230.google.com with SMTP id t189so19376179wmt.1 for ; Fri, 17 Mar 2017 08:54:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to; bh=MJ1en+AqmBfI/1LyvP7iRoL4oN7qZEl5zaDLLnyFvpk=; b=y9eHhxvPFRWskj+asOXxGgGguBg8QQuptu+fqHr2xEW9sKcujR3sAG0BLqUazLOhv1 WO+N0vc5WhwxYTeTer2y7fl/bxcm9W5X3ckuPQOuFbXL0hxONfeX+oz1lTz/bhxVWsXe xhUYp7USfD1oFXJ1volIx66waTjcVqaUUg3LD0VsNqKpc3PzjXup3tn/XpGtXEFvD3OW OPkHuUZQUu5OMsEKWf3wjVQoy5Sbhs3Z1k5LUod2yt7dTsCFSQ68cmcvzHJvt1lUE88/ O7YOifyf6oIVgGd1LKgOuJEXUb9yH10BKmKVOsvLzD7rWXeqEMzg7PpuUTyo5HCi9Oe9 Ewbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to; bh=MJ1en+AqmBfI/1LyvP7iRoL4oN7qZEl5zaDLLnyFvpk=; b=PXCw0Uu/RIvKsDg/rBuFbrBUknjiZ9gJrXe3AivQ2VxZyUjWsv38KtYzrLY6317j5Q eCX+ZHEdMW3TD+r2vOjDESat6C+UUS58MJcV5+3KJF3EBOqDGediG5oDcFa8+gYq45ut i8kNik/hFuy63fcF9BJGndnEuoE7Uiv6IptmGzDlHIc8T5DFCytTBG/Jrf6k3QpUoQg7 zZgPLrFaTSa0V9bR3oy/K7QA/wF1JTJKKUzNUMkhNiwCyO0bF1iO6v8/UWgBxGpts0dx uIIzihI+uzBTVDamBwOPzce/1DtLDkSlZPlGgIzm967R93IM7t+DQW48EJm6unpiBLwo YD9A== X-Gm-Message-State: AFeK/H2Nrl/I0CAWNqeRAtSzM0kJSVEiP+TRDivOdjFtgDEu7DRlqH2k7WlTErlXcMXX6Jca0glEIVs/zW3nmiGsrRy6XpMHjOmdIViwb0OSMtBciuQHHi3lr0nIP+6hq0UNj8jkUs06dlNVHTs2JKR/HTapHLBz/QJxJz7RfobQGdrKgE1lWOUPgfZE5szPN+eI4NOtdtZQ X-Received: by 10.28.96.194 with SMTP id u185mr3375698wmb.82.1489766045628; Fri, 17 Mar 2017 08:54:05 -0700 (PDT) Received: from [192.168.0.105] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id v21sm5876200wrd.58.2017.03.17.08.54.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Mar 2017 08:54:04 -0700 (PDT) Subject: Re: [COMMITTERS] pgsql: Use asynchronous connect API in libpqwalreceiver To: Tom Lane , Andrew Dunstan References: <19403.1488586564@sss.pgh.pa.us> <34d98fbf-9195-04d2-b85a-634d6952ffdf@2ndquadrant.com> <19815.1488600693@sss.pgh.pa.us> <4144256b-dc4f-892c-a720-71f6e7b9ac87@2ndquadrant.com> <3ffe98f7-d2ed-65c6-51f3-4d97ffd6e312@2ndquadrant.com> <34591606-976a-bbee-8eda-778c1e1fc52f@2ndQuadrant.com> <10458.1489763266@sss.pgh.pa.us> Cc: Peter Eisentraut , pgsql-hackers@postgresql.org From: Petr Jelinek Message-ID: <5b6a6d6d-fb45-0afb-2e95-5600063c3dbd@2ndquadrant.com> Date: Fri, 17 Mar 2017 16:54:03 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: <10458.1489763266@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------5D0F87609BC1FFB284B7AAAA" X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------5D0F87609BC1FFB284B7AAAA Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit On 17/03/17 16:07, Tom Lane wrote: > Andrew Dunstan writes: >> I have confirmed on jacana Petr's observation that adding a timeout to >> the WaitLatchOrSocket cures the problem. > > Does anyone have a theory as to why that cures the problem? > I now have theory and even PoC patch for it. The long wait of WaitLatchOrSocket happens after connection state switches from CONNECTION_STARTED to CONNECTION_MADE in both cases the return value of PQconnectPoll is PGRES_POLLING_WRITING so we wait for FD_WRITE. Now the documentation for WSAEventSelect says "The FD_WRITE network event is handled slightly differently. An FD_WRITE network event is recorded when a socket is first connected with a call to the connect, ConnectEx, WSAConnect, WSAConnectByList, or WSAConnectByName function or when a socket is accepted with accept, AcceptEx, or WSAAccept function and then after a send fails with WSAEWOULDBLOCK and buffer space becomes available. Therefore, an application can assume that sends are possible starting from the first FD_WRITE network event setting and lasting until a send returns WSAEWOULDBLOCK. After such a failure the application will find out that sends are again possible when an FD_WRITE network event is recorded and the associated event object is set." But while PQconnectPoll does connect() before setting connection status to CONNECTION_STARTED and returns PGRES_POLLING_WRITING so the FD_WRITE eventually happens, it does not do any writes in the code block that switches to CONNECTION_MADE and PGRES_POLLING_WRITING. That means FD_WRITE event is never recorded as per quoted documentation. Then what remains is why it works in libpq. If you look at pgwin32_select which is eventually called by libpq code, it actually handles the situation by trying empty WSASend on any socket that is supposed to wait for FD_WRITE and only calling the WaitForMultipleObjectsEx when WSASend failed with WSAEWOULDBLOCK, when the WSASend succeeds it immediately returns ok. So I did something similar in attached for WaitEventSetWaitBlock() and it indeed solves the issue my windows test machine. Now the coding/placement probably isn't the best (and there are no comments), but maybe somebody will find proper place for this now that we know the cause. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------5D0F87609BC1FFB284B7AAAA Content-Type: text/x-patch; name="fix-wait-for-writable-socket-on-windows.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="fix-wait-for-writable-socket-on-windows.patch" diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latc= h.c index ea7f930..300b866 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -1401,6 +1401,37 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_tim= eout, WaitEventAdjustWin32(set, cur_event); cur_event->reset =3D false; } + + if (cur_event->events & WL_SOCKET_WRITEABLE) + { + char c; + WSABUF buf; + DWORD sent; + int r; + + buf.buf =3D &c; + buf.len =3D 0; + + r =3D WSASend(cur_event->fd, &buf, 1, &sent, 0, NULL, NULL); + if (r =3D=3D 0) + { + occurred_events->pos =3D cur_event->pos; + occurred_events->user_data =3D cur_event->user_data; + occurred_events->events =3D WL_SOCKET_WRITEABLE; + occurred_events->fd =3D cur_event->fd; + return 1; + } + else + { + if (WSAGetLastError() !=3D WSAEWOULDBLOCK) + /* + * Not completed, and not just "would block", so an error + * occurred + */ + elog(ERROR, "failed writability check on socket: error code %u", + WSAGetLastError()); + } + } } =20 /* --------------5D0F87609BC1FFB284B7AAAA Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------5D0F87609BC1FFB284B7AAAA--