From 6b4097f5a168f60ef49bf8c2385ae3074cbc6a3f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= <git@rmr.ninja>
Date: Thu, 9 Jan 2020 10:08:16 +0100
Subject: [PATCH] Abort with _exit on pg_dump / pg_restore signal handler

---
 src/bin/pg_dump/parallel.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c
index 37f0d0d39a7b..e931016c1df8 100644
--- a/src/bin/pg_dump/parallel.c
+++ b/src/bin/pg_dump/parallel.c
@@ -611,8 +611,14 @@ sigTermHandler(SIGNAL_ARGS)
 		write_stderr("terminated by user\n");
 	}
 
-	/* And die. */
-	exit(1);
+	/*
+	 * We abort the program using _exit because using exit may interfere
+	 * with the function that was running when the interruption came.
+	 * For example, if the running function had acquired an allocation mutex
+	 * before it was interrupted and one of the atexit functions tried to
+	 * deallocate memory, it would block indefinitely.
+	 */
+	_exit(1);
 }
 
 /*
