]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
htsp: fixed wrong mutex usage and leaks in htsp_write_scheduler()
authorJaroslav Kysela <perex@perex.cz>
Mon, 22 Sep 2014 20:03:45 +0000 (22:03 +0200)
committerJaroslav Kysela <perex@perex.cz>
Mon, 22 Sep 2014 20:03:45 +0000 (22:03 +0200)
src/htsp_server.c

index 1f8f9d04b04c42ea3a0228fd21bf5f5d494a1aaf..f07c51f50cdb20f202ee1837940f8038695d72b6 100644 (file)
@@ -2350,6 +2350,7 @@ htsp_write_scheduler(void *aux)
   htsp_msg_t *hm;
   void *dptr;
   size_t dlen;
+  int r;
 
   pthread_mutex_lock(&htsp->htsp_out_mutex);
 
@@ -2358,7 +2359,7 @@ htsp_write_scheduler(void *aux)
     if((hmq = TAILQ_FIRST(&htsp->htsp_active_output_queues)) == NULL) {
       /* No active queues at all */
       if(!htsp->htsp_writer_run)
-             break; /* Should not run anymore, bail out */
+        break; /* Should not run anymore, bail out */
       
       /* Nothing to be done, go to sleep */
       pthread_cond_wait(&htsp->htsp_out_cond, &htsp->htsp_out_mutex);
@@ -2374,7 +2375,7 @@ htsp_write_scheduler(void *aux)
     if(hmq->hmq_length) {
       /* Still messages to be sent, put back in active queues */
       if(hmq->hmq_strict_prio) {
-             TAILQ_INSERT_HEAD(&htsp->htsp_active_output_queues, hmq, hmq_link);
+        TAILQ_INSERT_HEAD(&htsp->htsp_active_output_queues, hmq, hmq_link);
       } else {
         TAILQ_INSERT_TAIL(&htsp->htsp_active_output_queues, hmq, hmq_link);
       }
@@ -2385,18 +2386,22 @@ htsp_write_scheduler(void *aux)
     if (htsmsg_binary_serialize(hm->hm_msg, &dptr, &dlen, INT32_MAX) != 0) {
       tvhlog(LOG_WARNING, "htsp", "%s: failed to serialize data",
              htsp->htsp_logname);
+      htsp_msg_destroy(hm);
+      pthread_mutex_lock(&htsp->htsp_out_mutex);
+      continue;
     }
 
     htsp_msg_destroy(hm);
 
-    if (tvh_write(htsp->htsp_fd, dptr, dlen)) {
+    r = tvh_write(htsp->htsp_fd, dptr, dlen);
+    free(dptr);
+    pthread_mutex_lock(&htsp->htsp_out_mutex);
+    
+    if (r) {
       tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s",
              htsp->htsp_logname, strerror(errno));
       break;
     }
-
-    free(dptr);
-    pthread_mutex_lock(&htsp->htsp_out_mutex);
   }
   // Shutdown socket to make receive thread terminate entire HTSP connection