fix mem leak (when streaming large data streams)
authorxrs <xrs@mail36.net>
Wed, 27 Jun 2018 17:16:02 +0000 (19:16 +0200)
committerxrs <xrs@mail36.net>
Wed, 27 Jun 2018 17:16:02 +0000 (19:16 +0200)
src/cadet/gnunet-cadet.c

index 67cebf02bf394aadca98a632244b14d26e2d7bdc..4fedf1c053fed0ae9d2e148777d2bee1ae6986c0 100644 (file)
@@ -27,6 +27,8 @@
 #include "gnunet_cadet_service.h"
 #include "cadet.h"
 
+#define STREAM_BUFFER_SIZE 1024  // Pakets
+//#define STREAM_BUFFER_SIZE 5*1024*1024  // 5 MB
 
 /**
  * Option -P.
@@ -123,6 +125,8 @@ static struct GNUNET_SCHEDULER_Task *rd_task;
  */
 static struct GNUNET_SCHEDULER_Task *job;
 
+static unsigned int sent_pkt;
+
 
 /**
  * Wait for input on STDIO and send it out over the #ch.
@@ -228,6 +232,12 @@ shutdown_task (void *cls)
   }
 }
 
+void *
+mq_cb(void *cls)
+{
+  listen_stdio ();
+}
+
 
 /**
  * Task run in stdio mode, after some data is available at stdin.
@@ -248,6 +258,8 @@ read_stdio (void *cls)
                     60000);
   if (data_size < 1)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "read() returned  %s\n", strerror(errno));
     GNUNET_SCHEDULER_shutdown();
     return;
   }
@@ -262,9 +274,21 @@ read_stdio (void *cls)
                  data_size);
   GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
                   env);
+
+  sent_pkt++;
+
   if (GNUNET_NO == echo)
   {
-    listen_stdio ();
+    // Use MQ's notification if the data of stdin is pooring in too fast.
+    if (STREAM_BUFFER_SIZE < sent_pkt) 
+    {
+      GNUNET_MQ_notify_sent (env, mq_cb, cls);
+      sent_pkt = 0;
+    }
+    else 
+    {
+      listen_stdio ();
+    }
   }
   else
   {