Просмотр исходного кода

Merge pull request #1874 from tmatth/bugfix/avoid-sigpipe-on-closed-sock

Avoid sigpipe on writing to closed file descriptors
Jim 6 лет назад
Родитель
Сommit
e9185f462a
2 измененных файлов с 24 добавлено и 2 удалено
  1. 13 0
      UI/obs-app.cpp
  2. 11 2
      plugins/obs-outputs/librtmp/rtmp.c

+ 13 - 0
UI/obs-app.cpp

@@ -50,6 +50,7 @@
 #include <windows.h>
 #else
 #include <signal.h>
+#include <pthread.h>
 #endif
 
 #include <iostream>
@@ -2245,6 +2246,18 @@ int main(int argc, char *argv[])
 	sig_handler.sa_flags = 0;
 
 	sigaction(SIGINT, &sig_handler, NULL);
+
+
+	/* Block SIGPIPE in all threads, this can happen if a thread calls write on
+	a closed pipe. */
+	sigset_t sigpipe_mask;
+	sigemptyset(&sigpipe_mask);
+	sigaddset(&sigpipe_mask, SIGPIPE);
+	sigset_t saved_mask;
+	if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask) == -1) {
+		perror("pthread_sigmask");
+		exit(1);
+	}
 #endif
 
 #ifdef _WIN32

+ 11 - 2
plugins/obs-outputs/librtmp/rtmp.c

@@ -40,6 +40,10 @@
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 #endif
 
+#if !defined(MSG_NOSIGNAL)
+#define MSG_NOSIGNAL 0
+#endif
+
 #if defined(USE_MBEDTLS)
 #if defined(_WIN32)
 #include <windows.h>
@@ -930,6 +934,11 @@ RTMP_Connect0(RTMP *r, struct sockaddr * service, socklen_t addrlen)
 
     if (r->m_sb.sb_socket != INVALID_SOCKET)
     {
+#ifndef _WIN32
+#ifdef SO_NOSIGPIPE
+        setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof(int));
+#endif
+#endif
         if(r->m_bindIP.addrLen)
         {
             if (bind(r->m_sb.sb_socket, (const struct sockaddr *)&r->m_bindIP.addr, r->m_bindIP.addrLen) < 0)
@@ -4589,7 +4598,7 @@ RTMPSockBuf_Fill(RTMPSockBuf *sb)
         else
 #endif
         {
-            nBytes = recv(sb->sb_socket, sb->sb_start + sb->sb_size, nBytes, 0);
+            nBytes = recv(sb->sb_socket, sb->sb_start + sb->sb_size, nBytes, MSG_NOSIGNAL);
         }
         if (nBytes > 0)
         {
@@ -4642,7 +4651,7 @@ RTMPSockBuf_Send(RTMPSockBuf *sb, const char *buf, int len)
     else
 #endif
     {
-        rc = send(sb->sb_socket, buf, len, 0);
+        rc = send(sb->sb_socket, buf, len, MSG_NOSIGNAL);
     }
     return rc;
 }