Skip to content

Instantly share code, notes, and snippets.

@lmb
Last active September 25, 2016 18:13
Show Gist options
  • Save lmb/63189f83c5f00dd59c86f3c9bc07694d to your computer and use it in GitHub Desktop.
Save lmb/63189f83c5f00dd59c86f3c9bc07694d to your computer and use it in GitHub Desktop.
[PATCH] Return EPIPE from mdb_env_copyfd2 instead abort on SIGPIPE
From 575a1386a9c47e2467e96c9e2be2abf32a56a734 Mon Sep 17 00:00:00 2001
From: Lorenz Bauer <lmb@cloudflare.com>
Date: Sun, 25 Sep 2016 11:11:08 -0700
Subject: [PATCH] Return EPIPE from mdb_env_copyfd2 instead abort on SIGPIPE
---
libraries/liblmdb/mdb.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c
index b0518a5..06a5fc4 100644
--- a/libraries/liblmdb/mdb.c
+++ b/libraries/liblmdb/mdb.c
@@ -159,6 +159,7 @@ typedef SSIZE_T ssize_t;
#ifndef _WIN32
#include <pthread.h>
+#include <signal.h>
#ifdef MDB_USE_POSIX_SEM
# define MDB_USE_HASH 1
#include <semaphore.h>
@@ -9655,10 +9656,17 @@ mdb_env_copythr(void *arg)
#define DO_WRITE(rc, fd, ptr, w2, len) rc = WriteFile(fd, ptr, w2, &len, NULL)
#else
int len;
+ sigset_t set;
#define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0)
+
+ sigemptyset(&set);
+ sigaddset(&set, SIGPIPE);
#endif
pthread_mutex_lock(&my->mc_mutex);
+#ifndef _WIN32
+ my->mc_error = pthread_sigmask(SIG_BLOCK, &set, NULL);
+#endif
for(;;) {
while (!my->mc_new)
pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
@@ -9672,6 +9680,12 @@ again:
DO_WRITE(rc, my->mc_fd, ptr, wsize, len);
if (!rc) {
rc = ErrCode();
+#ifndef _WIN32
+ if (rc == EPIPE) {
+ int tmp;
+ sigwait(&set, &tmp);
+ }
+#endif
break;
} else if (len > 0) {
rc = MDB_SUCCESS;
--
2.9.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment