svc_run: make sure only one svc_run loop runs in one process
diff mbox series

Message ID 20190409113713.30595-1-xiubli@redhat.com
State New
Headers show
Series
  • svc_run: make sure only one svc_run loop runs in one process
Related show

Commit Message

Xiubo Li April 9, 2019, 11:37 a.m. UTC
From: Xiubo Li <xiubli@redhat.com>

In gluster-block project and there are 2 separate threads, both
of which will run the svc_run loop, this could work well in glibc
version, but in libtirpc we are hitting the random crash and stuck
issues.

More detail please see:
https://github.com/gluster/gluster-block/pull/182

Signed-off-by: Xiubo Li <xiubli@redhat.com>
---
 src/svc_run.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Xiubo Li May 16, 2019, 2:55 a.m. UTC | #1
Hey ping.

What's the state of this patch and will it make sense here?

Thanks
BRs

On 2019/4/9 19:37, xiubli@redhat.com wrote:
> From: Xiubo Li <xiubli@redhat.com>
>
> In gluster-block project and there are 2 separate threads, both
> of which will run the svc_run loop, this could work well in glibc
> version, but in libtirpc we are hitting the random crash and stuck
> issues.
>
> More detail please see:
> https://github.com/gluster/gluster-block/pull/182
>
> Signed-off-by: Xiubo Li <xiubli@redhat.com>
> ---
>   src/svc_run.c | 19 +++++++++++++++++++
>   1 file changed, 19 insertions(+)
>
> diff --git a/src/svc_run.c b/src/svc_run.c
> index f40314b..b295755 100644
> --- a/src/svc_run.c
> +++ b/src/svc_run.c
> @@ -38,12 +38,17 @@
>   #include <string.h>
>   #include <unistd.h>
>   #include <sys/poll.h>
> +#include <syslog.h>
> +#include <stdbool.h>
>   
>   
>   #include <rpc/rpc.h>
>   #include "rpc_com.h"
>   #include <sys/select.h>
>   
> +static bool svc_loop_running = false;
> +static pthread_mutex_t svc_run_lock = PTHREAD_MUTEX_INITIALIZER;
> +
>   void
>   svc_run()
>   {
> @@ -51,6 +56,16 @@ svc_run()
>     struct pollfd *my_pollfd = NULL;
>     int last_max_pollfd = 0;
>   
> +  pthread_mutex_lock(&svc_run_lock);
> +  if (svc_loop_running) {
> +    pthread_mutex_unlock(&svc_run_lock);
> +    syslog (LOG_ERR, "svc_run: svc loop is already running in current process %d", getpid());
> +    return;
> +  }
> +
> +  svc_loop_running = true;
> +  pthread_mutex_unlock(&svc_run_lock);
> +
>     for (;;) {
>       int max_pollfd = svc_max_pollfd;
>       if (max_pollfd == 0 && svc_pollfd == NULL)
> @@ -111,4 +126,8 @@ svc_exit()
>   	svc_pollfd = NULL;
>   	svc_max_pollfd = 0;
>   	rwlock_unlock(&svc_fd_lock);
> +
> +    pthread_mutex_lock(&svc_run_lock);
> +    svc_loop_running = false;
> +    pthread_mutex_unlock(&svc_run_lock);
>   }

Patch
diff mbox series

diff --git a/src/svc_run.c b/src/svc_run.c
index f40314b..b295755 100644
--- a/src/svc_run.c
+++ b/src/svc_run.c
@@ -38,12 +38,17 @@ 
 #include <string.h>
 #include <unistd.h>
 #include <sys/poll.h>
+#include <syslog.h>
+#include <stdbool.h>
 
 
 #include <rpc/rpc.h>
 #include "rpc_com.h"
 #include <sys/select.h>
 
+static bool svc_loop_running = false;
+static pthread_mutex_t svc_run_lock = PTHREAD_MUTEX_INITIALIZER;
+
 void
 svc_run()
 {
@@ -51,6 +56,16 @@  svc_run()
   struct pollfd *my_pollfd = NULL;
   int last_max_pollfd = 0;
 
+  pthread_mutex_lock(&svc_run_lock);
+  if (svc_loop_running) {
+    pthread_mutex_unlock(&svc_run_lock);
+    syslog (LOG_ERR, "svc_run: svc loop is already running in current process %d", getpid());
+    return;
+  }
+
+  svc_loop_running = true;
+  pthread_mutex_unlock(&svc_run_lock);
+
   for (;;) {
     int max_pollfd = svc_max_pollfd;
     if (max_pollfd == 0 && svc_pollfd == NULL)
@@ -111,4 +126,8 @@  svc_exit()
 	svc_pollfd = NULL;
 	svc_max_pollfd = 0;
 	rwlock_unlock(&svc_fd_lock);
+
+    pthread_mutex_lock(&svc_run_lock);
+    svc_loop_running = false;
+    pthread_mutex_unlock(&svc_run_lock);
 }