Fixed libwebsocket_context race condition. 83/10983/3
authorOssama Othman <ossama.othman@intel.com>
Mon, 14 Oct 2013 22:55:47 +0000 (15:55 -0700)
committerOssama Othman <ossama.othman@intel.com>
Wed, 16 Oct 2013 21:57:29 +0000 (14:57 -0700)
Tie the lifetime of the websocket_server to this thread, not the
main thread, so that we have no lifetime related race conditions
with respect to the underlying libwebsocket_context object.

Change-Id: Icb49f5fa21e7cea5fdc7f4f0023a551f40ef8b49
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
src/daemon.cpp
src/websocket_server.cpp

index 071d596..17b52d7 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <glib.h>
 #include <iostream>
-
+#include <thread>
 
 /**
  * settingsd program entry point.
@@ -54,8 +54,15 @@ main(int argc, char * argv[])
     settings::manager manager(config.settings_dir());
 
     // The websocket server will run in its own thread.
-    settings::websocket_server server(config, manager);
-    server.run();
+    std::thread(
+      [&config, &manager](){
+        // Tie the lifetime of the websocket_server to this thread,
+        // not the main thread, so that we have no lifetime related
+        // race conditions with respect to the underlying
+        // libwebsocket_context object.
+        settings::websocket_server server(config, manager);
+        server.run();
+      }).detach();
 
     // Glib related events, including GDbus related signal handlers,
     // will handled in this (main) thread.
index f6a82b3..636e893 100644 (file)
@@ -30,7 +30,7 @@
 #include "../lib/manager.hpp"
 
 #include <stdexcept>
-#include <thread>
+
 
 
 namespace
@@ -136,7 +136,7 @@ ivi::settings::websocket_server::websocket_server(
 
   context_ = libwebsocket_create_context(&info);
   if (context_ == nullptr)
-    throw std::runtime_error("Unable to initialize websocket.\n");
+    throw std::runtime_error("Unable to initialize websocket.");
 
   std::cout << "\n" PACKAGE_NAME " listening for requests on port "
             << info.port << ".\n";
@@ -150,23 +150,13 @@ ivi::settings::websocket_server::~websocket_server()
 void
 ivi::settings::websocket_server::run()
 {
-  // Run the websocket event loop in its own thread.
-  std::thread(
-    [](libwebsocket_context * context){
-      try {
-        // Run the libwebsockets event loop with an infinite timeout.
-        // The negative timeout causes the underlying call poll() to
-        // block indefinitely until an event occurs.
-        constexpr int const timeout = -1;
-        while (libwebsocket_service(context, timeout) >= 0) {
-        }
-
-        std::cerr << "WebSocket event loop ended.\n";
-      } catch(std::exception & e) {
-        std::cerr << e.what() << std::endl;
-      }
-    },
-    context_).detach();
+  // Run the libwebsockets event loop with an infinite timeout.  The
+  // negative timeout causes the underlying call poll() to block
+  // indefinitely until an event occurs.
+  constexpr int const timeout = -1;
+
+  while (libwebsocket_service(context_, timeout) == 0) {
+  }
 }