[vlc-devel] [PATCH] fix sigpipe crash and recover when bad network

Janboe Ye janboe.ye at outlook.com
Fri Feb 21 13:27:56 CET 2020


>From a9380d8033a3718f3cdb740c3a982d42037aa129 Mon Sep 17 00:00:00 2001
From: Janboe Ye <janboe.ye at gmail.com>
Date: Fri, 21 Feb 2020 15:25:22 +0800
Subject: [PATCH] fix sigpipe crash and recover when bad network

dsm will send sigpipe when bad network conditional and crash app.
This will disable sigpipe and recovery after writing failure

prefetch thread will try to recovery by creating fake EOS.

Signed-off-by: Janboe Ye <janboe.ye at gmail.com>
---
 .../0001-setsockopt-to-disable-sigpipe.patch  | 48 +++++++++++++++++++
 contrib/src/libdsm/rules.mak                  |  1 +
 modules/stream_filter/prefetch.c              |  6 +--
 3 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 contrib/src/libdsm/0001-setsockopt-to-disable-sigpipe.patch

diff --git a/contrib/src/libdsm/0001-setsockopt-to-disable-sigpipe.patch b/contrib/src/libdsm/0001-setsockopt-to-disable-sigpipe.patch
new file mode 100644
index 0000000000..0b3cb0d8df
--- /dev/null
+++ b/contrib/src/libdsm/0001-setsockopt-to-disable-sigpipe.patch
@@ -0,0 +1,48 @@
+From 1e7633a86fab43f0aba233abb13ecc886071b253 Mon Sep 17 00:00:00 2001
+From: Janboe Ye <janboe.ye at gmail.com>
+Date: Fri, 21 Feb 2020 15:14:32 +0800
+Subject: [PATCH] setsockopt to disable sigpipe
+
+this signal will crash app when network goes wrong
+
+Signed-off-by: Janboe Ye <janboe.ye at gmail.com>
+---
+ src/netbios_session.c | 11 ++++++++++-
+ 1 file changed, 10 insertions(+), 1 deletion(-)
+
+diff --git a/src/netbios_session.c b/src/netbios_session.c
+index a77cbc7..e2159c2 100644
+--- a/src/netbios_session.c
++++ b/src/netbios_session.c
+@@ -52,10 +52,19 @@
+ #include "netbios_session.h"
+ #include "netbios_utils.h"
+ 
++#if !defined(MSG_NOSIGNAL)
++# define MSG_NOSIGNAL 0
++#endif
++
+ static int open_socket_and_connect(netbios_session *s)
+ {
+     if ((s->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+         goto error;
++#ifdef SO_NOSIGPIPE
++    //Never generate SIGPIPE on broken write
++    if (setsockopt(s->socket, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof(int)))
++        goto error;
++#endif
+     if (connect(s->socket, (struct sockaddr *)&s->remote_addr, sizeof(s->remote_addr)) <0)
+         goto error;
+ 
+@@ -229,7 +238,7 @@ int               netbios_session_packet_send(netbios_session *s)
+ 
+     s->packet->length = htons(s->packet_cursor);
+     to_send           = sizeof(netbios_session_packet) + s->packet_cursor;
+-    sent              = send(s->socket, (void *)s->packet, to_send, 0);
++    sent              = send(s->socket, (void *)s->packet, to_send, MSG_NOSIGNAL);
+ 
+     if (sent != to_send)
+     {
+-- 
+2.23.0
+
diff --git a/contrib/src/libdsm/rules.mak b/contrib/src/libdsm/rules.mak
index 1ea9b769b5..e7b9181865 100644
--- a/contrib/src/libdsm/rules.mak
+++ b/contrib/src/libdsm/rules.mak
@@ -24,6 +24,7 @@ libdsm: libdsm-$(LIBDSM_VERSION).tar.gz .sum-libdsm
 	$(APPLY) $(SRC)/libdsm/fix-pc-generation.patch
 	$(APPLY) $(SRC)/libdsm/fix-pipe-compat.patch
 	$(APPLY) $(SRC)/libdsm/0001-compat-Don-t-use-_pipe-when-building-for-winstore.patch
+	$(APPLY) $(SRC)/libdsm/0001-setsockopt-to-disable-sigpipe.patch
 	$(MOVE)
 
 DEPS_libdsm = libtasn1 iconv
diff --git a/modules/stream_filter/prefetch.c b/modules/stream_filter/prefetch.c
index a31f52c67f..1ae63094ea 100644
--- a/modules/stream_filter/prefetch.c
+++ b/modules/stream_filter/prefetch.c
@@ -238,13 +238,13 @@ static void *Thread(void *data)
             len = sys->buffer_size - offset;
 
         ssize_t val = ThreadRead(stream, sys->buffer + offset, len);
-        if (val < 0)
-            continue;
-        if (val == 0)
+
+        if (val <= 0)
         {
             assert(len > 0);
             msg_Dbg(stream, "end of stream");
             sys->eof = true;
+            val = 0;
         }
 
         assert((size_t)val <= len);
-- 
2.23.0




More information about the vlc-devel mailing list