From: Armin Schöffmann <armin.schoeffmann@aegaeon.de>
To: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Subject: PATCH: pg_restore parallel-execution-deadlock issue
Date: Tue, 5 Apr 2016 02:28:45 +0200
Message-ID: <zarafa.570306bd.3418.074bf1420d8f2ba2@root.aegaeon.de> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>
worthy hackers,
I propose the below patches to parallels.c and pg_backup_utils.c fixing deadlocks 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.postgresql.org
- Winsock's "recv(...)" called in piperead() is a blocking read by default, therefor, signalizing termEvent as used in ShutdownWorkersHard() is not enough 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 worker is active (e.g. option -j3) and a premature EOF occurs in the input-file.
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-forget-scenarios, but it matters in other cases.
As of current, pg_backup_utils uses EndThread to retire additional worker-threads., which are spawned by _beginthreadex in parallel.c. The corresponding 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 synchronization 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 somebody can notify the tool's maintainer, to take a look into it.
best regards,
Armin Schöffmann.
--
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;
+#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
@@ -367,4 +368,7 @@ ShutdownWorkersHard(ParallelState *pstate)
/* The workers monitor this event via checkAborting(). */
SetEvent(termEvent);
+
+ for (i = 0; i < pstate->numWorkers; i++)
+ shutdown(pstate->parallelSlot[i].pipeWrite, SD_BOTH);
#endif
@@ -408,5 +412,8 @@ WaitForTerminatingWorkers(ParallelState *pstate)
for (j = 0; j < pstate->numWorkers; j++)
if (pstate->parallelSlot[j].hThread == hThread)
+ {
slot = &(pstate->parallelSlot[j]);
+ CloseHandle(hThread);
+ }
free(lpHandles);
pg_backup_utils.c
@@ -120,5 +120,5 @@ exit_nicely(int code)
#ifdef WIN32
if (parallel_init_done && GetCurrentThreadId() != mainThreadId)
- ExitThread(code);
+ _endthreadex(code);
#endif
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackersAttachments:download all (2 files, .tar.gz)parallel.c.diff (application/octet-stream, 1.1K) download
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\f0\fs22 @@ -350,7 +350,8 @@ static void\par
ShutdownWorkersHard(ParallelState *pstate)\par
\{\par
-#ifndef WIN32\par+\par
\tab int\tab\tab\tab i;\par
\par
+#ifndef WIN32\par
\tab signal(SIGPIPE, SIG_IGN);\par
\par
@@ -367,4 +368,7 @@ ShutdownWorkersHard(ParallelState *pstate)\par
\tab /* The workers monitor this event via checkAborting(). */\par
\tab SetEvent(termEvent);\par
+\par+\tab for (i = 0; i < pstate->numWorkers; i++)\par+\tab\tab shutdown(pstate->parallelSlot[i].pipeWrite, SD_BOTH);\par
#endif\par
\par
@@ -408,5 +412,8 @@ WaitForTerminatingWorkers(ParallelState *pstate)\par
\tab\tab for (j = 0; j < pstate->numWorkers; j++)\par
\tab\tab\tab if (pstate->parallelSlot[j].hThread == hThread)\par
+\tab\tab\tab\{\par
\tab\tab\tab\tab slot = &(pstate->parallelSlot[j]);\par
+\tab\tab\tab\tab CloseHandle(hThread);\par+\tab\tab\tab\}\par
\par
\tab\tab free(lpHandles);\par
}
pg_backup_utils.c.diff (application/octet-stream, 377B) download
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\f0\fs22 @@ -120,5 +120,5 @@ exit_nicely(int code)\par
#ifdef WIN32\par
\tab if (parallel_init_done && GetCurrentThreadId() != mainThreadId)\par
-\tab\tab ExitThread(code);\par+\tab\tab _endthreadex(code);\par
#endif\par
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: armin.schoeffmann@aegaeon.de
Subject: Re: PATCH: pg_restore parallel-execution-deadlock issue
In-Reply-To: <zarafa.570306bd.3418.074bf1420d8f2ba2@root.aegaeon.de>
References: <zarafa.570306bd.3418.074bf1420d8f2ba2@root.aegaeon.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox