Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1anErX-0008Uf-BL for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Apr 2016 00:28:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1anErW-0006vY-N5 for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Apr 2016 00:28:54 +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 1anErV-0006uv-IO for pgsql-hackers@postgresql.org; Tue, 05 Apr 2016 00:28:53 +0000 Received: from root.aegaeon.de ([62.141.38.162] helo=smtp.aegaeon.de) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1anErS-0001m5-4U for pgsql-hackers@postgresql.org; Tue, 05 Apr 2016 00:28:52 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.aegaeon.de (Postfix) with ESMTP id F0048540014 for ; Tue, 5 Apr 2016 02:28:46 +0200 (CEST) X-Virus-Scanned: amavisd-new at aegaeon.de Received: from smtp.aegaeon.de ([127.0.0.1]) by localhost (root.aegaeon.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VszQCMJawwKd for ; Tue, 5 Apr 2016 02:28:46 +0200 (CEST) Received: from root.aegaeon.de (localhost.localdomain [IPv6:::1]) by smtp.aegaeon.de (Postfix) with ESMTP id 06B32540013 for ; Tue, 5 Apr 2016 02:28:46 +0200 (CEST) Subject: PATCH: pg_restore parallel-execution-deadlock issue From: =?utf-8?Q?Armin_Sch=C3=B6ffmann?= To: =?utf-8?Q?pgsql-hackers=40postgresql=2Eorg?= Date: Tue, 5 Apr 2016 02:28:45 +0200 Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=_ZqM0c9SHlKCnPcgW5BgQ44VJtSpAHVG0ebf2z9bvm6UErX9A" X-Priority: 3 (Normal) X-Mailer: Zarafa 7.2.2-0 X-Original-To: Message-Id: X-Pg-Spam-Score: -2.9 (--) 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. Your mail reader does not understand MIME message format. --=_ZqM0c9SHlKCnPcgW5BgQ44VJtSpAHVG0ebf2z9bvm6UErX9A Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable worthy hackers, I propose the below patches to parallels.c and pg_backup_utils.c fixing dea= dlocks in pg_restore (windows only) if running more than 2 parallel jobs. This problem was reported by me earlier this year. http://www.postgresql.org/message-id/20160307161619.25731.78653@wrigleys.po= stgresql.org - Winsock's "recv(...)" called in piperead() is a blocking read by default,= therefor, signalizing termEvent as used in ShutdownWorkersHard() is not en= ough to make worker-threads go away. We need a preceding shutdown(pipeWrite, SD_BOTH), first, to abort blocking = IO in this case. Otherwise, the main-thread will wait forever, if more than one additional w= orker is active (e.g. option -j3) and a premature EOF occurs in the input-f= ile. Findings in pg_backup_utils.c/ parallels.c, which could impact other tools,= too: - threads created with _beginthreadex need to be exited by either a "return= exitcode" or "_endthreadex(exitcode)". It might be obsolete in fire-and-f= orget-scenarios, but it matters in other cases. As of current, pg_backup_utils uses EndThread to retire additional worker-t= hreads., which are spawned by _beginthreadex in parallel.c. The correspondi= ng call for ExitThread would be CreateThread, nevertheless, _beginthreadex is the correct choice here, as we do call-out = into CRT and need to retain the thread-handle for after-death synchronizati= on with the main-thread. The thread-handle needs to be closed explicitly. If this is not the correct place to discuss patches, I'd be glad if somebod= y can notify the tool's maintainer, to take a look into it. best regards, Armin Sch=C3=B6ffmann. -- Aegaeon technologies GmbH phone: +49.941.8107344 fax: +49.941.8107356 Legal disclaimer: http://aegaeon.de/disclaimer/email_all_int.txt parallel.c @@ -350,7 +350,8 @@ static void ShutdownWorkersHard(ParallelState *pstate) { -#ifndef WIN32 + int i; =20 +#ifndef WIN32 signal(SIGPIPE, SIG_IGN); =20 @@ -367,4 +368,7 @@ ShutdownWorkersHard(ParallelState *pstate) /* The workers monitor this event via checkAborting(). */ SetEvent(termEvent); + + for (i =3D 0; i < pstate->numWorkers; i++) + shutdown(pstate->parallelSlot[i].pipeWrite, SD_BOTH); #endif =20 @@ -408,5 +412,8 @@ WaitForTerminatingWorkers(ParallelState *pstate) for (j =3D 0; j < pstate->numWorkers; j++) if (pstate->parallelSlot[j].hThread =3D=3D hThread) + { slot =3D &(pstate->parallelSlot[j]); + CloseHandle(hThread); + } =20 free(lpHandles); pg_backup_utils.c @@ -120,5 +120,5 @@ exit_nicely(int code) #ifdef WIN32 if (parallel_init_done && GetCurrentThreadId() !=3D mainThreadId) - ExitThread(code); + _endthreadex(code); #endif --=_ZqM0c9SHlKCnPcgW5BgQ44VJtSpAHVG0ebf2z9bvm6UErX9A Content-Type: application/octet-stream; name=parallel.c.diff Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=parallel.c.diff e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcZGVmbGFuZzEwMzF7XGZv bnR0Ymx7XGYwXGZuaWxcZmNoYXJzZXQwIENvdXJpZXIgTmV3O319DQp7XCpc Z2VuZXJhdG9yIE1zZnRlZGl0IDUuNDEuMjEuMjUxMDt9XHZpZXdraW5kNFx1 YzFccGFyZFxzbDI0MFxzbG11bHQxXGYwXGZzMjIgQEAgLTM1MCw3ICszNTAs OCBAQCBzdGF0aWMgdm9pZFxwYXINCiBTaHV0ZG93bldvcmtlcnNIYXJkKFBh cmFsbGVsU3RhdGUgKnBzdGF0ZSlccGFyDQogXHtccGFyDQotI2lmbmRlZiBX SU4zMlxwYXINCitccGFyDQogXHRhYiBpbnRcdGFiXHRhYlx0YWIgaTtccGFy DQogXHBhcg0KKyNpZm5kZWYgV0lOMzJccG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/octet-stream; name=pg_backup_utils.c.diff Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=pg_backup_utils.c.diff e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcZGVmbGFuZzEwMzF7XGZv bnR0Ymx7XGYwXGZuaWxcZmNoYXJzZXQwIENvdXJpZXIgTmV3O319DQp7XCpc Z2VuZXJhdG9yIE1zZnRlZGl0IDUuNDEuMjEuMjUxMDt9XHZpZXdraW5kNFx1 YzFccGFyZFxmMFxmczIyIEBAIC0xMjAsNSArMTIwLDUgQEAgZXhpdF9uaWNl bHkoaW50IGNvZGUpXHBhcg0KICNpZmRlZiBXSU4zMlxwYXINCiBcdGFiIGlm IChwYXJhbGxlbF9pbml0X2RvbmUgJiYgR2V0Q3VycmVudFRocmVhZElkKCkg IT0gbWFpblRocmVhZElkKVxwYXINCi1cdGFiXHRhYiBFeGl0VGhyZWFkKGNv ZGUpO1xwYXINCitcdGFiXHRhYiBfZW5kdGhyZWFkZXgoY29kZSk7XHBhcg0K ICNlbmRpZlxwYXINCn0NCgA= --=_ZqM0c9SHlKCnPcgW5BgQ44VJtSpAHVG0ebf2z9bvm6UErX9A 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 --=_ZqM0c9SHlKCnPcgW5BgQ44VJtSpAHVG0ebf2z9bvm6UErX9A--