Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dgzo8-000759-OQ for pgsql-bugs@arkaria.postgresql.org; Sun, 13 Aug 2017 20:48:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dgzo7-0003Ee-Ny for pgsql-bugs@arkaria.postgresql.org; Sun, 13 Aug 2017 20:48:23 +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 1dgznP-0000p9-2h; Sun, 13 Aug 2017 20:47:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dgznL-00007R-Sg; Sun, 13 Aug 2017 20:47:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v7DKlSBw021795; Sun, 13 Aug 2017 16:47:28 -0400 From: Tom Lane To: Andres Freund cc: "Augustine, Jobin" , Noah Misch , Peter Eisentraut , pgsql-bugs@postgresql.org, pgsql-hackers@postgresql.org Subject: Re: [HACKERS] Replication to Postgres 10 on Windows is broken In-reply-to: <20170813200438.dua2s2blhtlutfbz@alap3.anarazel.de> References: <20170806062634.GA3180536@rfd.leadboat.com> <3819.1502032677@sss.pgh.pa.us> <20170806155037.GD3179882@rfd.leadboat.com> <20170808010632.GA3599653@rfd.leadboat.com> <20170811015107.GA3623941@rfd.leadboat.com> <20170811015512.7x5kl2qcshricgzl@alap3.anarazel.de> <20170813200438.dua2s2blhtlutfbz@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Sun, 13 Aug 2017 13:04:38 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <21720.1502657116.0@sss.pgh.pa.us> Date: Sun, 13 Aug 2017 16:47:28 -0400 Message-ID: <21794.1502657248@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21720.1502657116.1@sss.pgh.pa.us> Andres Freund writes: > On 2017-08-11 18:11:03 +0530, Augustine, Jobin wrote: >> Appears that patch is not helping. > That's too bad. Any chance you could install > https://docs.microsoft.com/en-us/sysinternals/downloads/procmon and > activate monitoring just for that phase? I think it can export to a text > file... It strikes me that maybe the misuse of io_flag could be contributing to this: if the walreceiver process's latch were set, we'd end up calling PQconnectPoll before the socket had necessarily come ready, which would produce the described symptom. That's grasping at straws admittedly, because I'm not sure why the walreceiver process's latch would be set at this point; but it seems like we ought to test a version of the patch that we believe correct before deciding that we still have a problem. To move things along, here's a corrected patch --- Jobin, please test. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="add-connected-event-2.patch"; charset="us-ascii" Content-ID: <21720.1502657116.2@sss.pgh.pa.us> Content-Description: add-connected-event-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/= src/backend/replication/libpqwalreceiver/libpqwalreceiver.c index de03362..37b481c 100644 --- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c +++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c @@ -168,13 +168,18 @@ libpqrcv_connect(const char *conninfo, bool logical, = const char *appname, status =3D PGRES_POLLING_WRITING; do { - /* Wait for socket ready and/or other events. */ int io_flag; int rc; =20 - io_flag =3D (status =3D=3D PGRES_POLLING_READING - ? WL_SOCKET_READABLE - : WL_SOCKET_WRITEABLE); + if (status =3D=3D PGRES_POLLING_READING) + io_flag =3D WL_SOCKET_READABLE; +#ifdef WIN32 + /* Windows needs a different test while waiting for connection-made */ + else if (PQstatus(conn->streamConn) =3D=3D CONNECTION_STARTED) + io_flag =3D WL_SOCKET_CONNECTED; +#endif + else + io_flag =3D WL_SOCKET_WRITEABLE; =20 rc =3D WaitLatchOrSocket(MyLatch, WL_POSTMASTER_DEATH | diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latc= h.c index 07b1364..c7345de 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -374,11 +374,11 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEven= ts, pgsocket sock, AddWaitEventToSet(set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET, NULL, NULL); =20 - if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) + if (wakeEvents & WL_SOCKET_MASK) { int ev; =20 - ev =3D wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE); + ev =3D wakeEvents & WL_SOCKET_MASK; AddWaitEventToSet(set, ev, sock, NULL, NULL); } =20 @@ -390,8 +390,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents= , pgsocket sock, { ret |=3D event.events & (WL_LATCH_SET | WL_POSTMASTER_DEATH | - WL_SOCKET_READABLE | - WL_SOCKET_WRITEABLE); + WL_SOCKET_MASK); } =20 FreeWaitEventSet(set); @@ -640,10 +639,12 @@ FreeWaitEventSet(WaitEventSet *set) * Add an event to the set. Possible events are: * - WL_LATCH_SET: Wait for the latch to be set * - WL_POSTMASTER_DEATH: Wait for postmaster to die - * - WL_SOCKET_READABLE: Wait for socket to become readable - * can be combined in one event with WL_SOCKET_WRITEABLE - * - WL_SOCKET_WRITEABLE: Wait for socket to become writeable - * can be combined with WL_SOCKET_READABLE + * - WL_SOCKET_READABLE: Wait for socket to become readable, + * can be combined in one event with other WL_SOCKET_* events + * - WL_SOCKET_WRITEABLE: Wait for socket to become writeable, + * can be combined with other WL_SOCKET_* events + * - WL_SOCKET_CONNECTED: Wait for socket connection to be established, + * can be combined with other WL_SOCKET_* events * * Returns the offset in WaitEventSet->events (starting from 0), which can= be * used to modify previously added wait events using ModifyWaitEvent(). @@ -652,9 +653,9 @@ FreeWaitEventSet(WaitEventSet *set) * i.e. it must be a process-local latch initialized with InitLatch, or a * shared latch associated with the current process by calling OwnLatch. * - * In the WL_SOCKET_READABLE/WRITEABLE case, EOF and error conditions are - * reported by returning the socket as readable/writable or both, dependin= g on - * WL_SOCKET_READABLE/WRITEABLE being specified. + * In the WL_SOCKET_READABLE/WRITEABLE/CONNECTED cases, EOF and error + * conditions are reported by returning the socket as readable/writable or + * both, depending on WL_SOCKET_READABLE/WRITEABLE/CONNECTED being specifi= ed. * * The user_data pointer specified here will be set for the events returned * by WaitEventSetWait(), allowing to easily associate additional data with @@ -685,8 +686,7 @@ AddWaitEventToSet(WaitEventSet *set, uint32 events, pgs= ocket fd, Latch *latch, } =20 /* waiting for socket readiness without a socket indicates a bug */ - if (fd =3D=3D PGINVALID_SOCKET && - (events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))) + if (fd =3D=3D PGINVALID_SOCKET && (events & (WL_SOCKET_MASK))) elog(ERROR, "cannot wait on socket event without a socket"); =20 event =3D &set->events[set->nevents]; @@ -885,6 +885,8 @@ WaitEventAdjustWin32(WaitEventSet *set, WaitEvent *even= t) flags |=3D FD_READ; if (event->events & WL_SOCKET_WRITEABLE) flags |=3D FD_WRITE; + if (event->events & WL_SOCKET_CONNECTED) + flags |=3D FD_CONNECT; =20 if (*handle =3D=3D WSA_INVALID_EVENT) { @@ -1395,7 +1397,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_time= out, returned_events++; } } - else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)) + else if (cur_event->events & WL_SOCKET_MASK) { WSANETWORKEVENTS resEvents; HANDLE handle =3D set->handles[cur_event->pos + 1]; @@ -1432,6 +1434,12 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_tim= eout, /* writeable */ occurred_events->events |=3D WL_SOCKET_WRITEABLE; } + if ((cur_event->events & WL_SOCKET_CONNECTED) && + (resEvents.lNetworkEvents & FD_CONNECT)) + { + /* connected */ + occurred_events->events |=3D WL_SOCKET_CONNECTED; + } if (resEvents.lNetworkEvents & FD_CLOSE) { /* EOF */ @@ -1439,6 +1447,8 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_time= out, occurred_events->events |=3D WL_SOCKET_READABLE; if (cur_event->events & WL_SOCKET_WRITEABLE) occurred_events->events |=3D WL_SOCKET_WRITEABLE; + if (cur_event->events & WL_SOCKET_CONNECTED) + occurred_events->events |=3D WL_SOCKET_CONNECTED; } =20 if (occurred_events->events !=3D 0) diff --git a/src/include/storage/latch.h b/src/include/storage/latch.h index 73abfaf..8af0266 100644 --- a/src/include/storage/latch.h +++ b/src/include/storage/latch.h @@ -126,6 +126,16 @@ typedef struct Latch #define WL_SOCKET_WRITEABLE (1 << 2) #define WL_TIMEOUT (1 << 3) /* not for WaitEventSetWait() */ #define WL_POSTMASTER_DEATH (1 << 4) +#ifdef WIN32 +#define WL_SOCKET_CONNECTED (1 << 5) +#else +/* avoid having to to deal with case on platforms not requiring it */ +#define WL_SOCKET_CONNECTED WL_SOCKET_WRITEABLE +#endif + +#define WL_SOCKET_MASK (WL_SOCKET_READABLE | \ + WL_SOCKET_WRITEABLE | \ + WL_SOCKET_CONNECTED) =20 typedef struct WaitEvent { ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--