Skip to content

v0.2.54..v0.2.55 changeset HttpTestServer.cpp

Garret Voltz edited this page Aug 14, 2020 · 1 revision
diff --git a/hoot-core-test/src/test/cpp/hoot/core/util/HttpTestServer.cpp b/hoot-core-test/src/test/cpp/hoot/core/util/HttpTestServer.cpp
index 41589a1..ad030d7 100644
--- a/hoot-core-test/src/test/cpp/hoot/core/util/HttpTestServer.cpp
+++ b/hoot-core-test/src/test/cpp/hoot/core/util/HttpTestServer.cpp
@@ -104,14 +104,21 @@ void HttpTestServer::wait()
   _thread->join();
 }
 
-void HttpTestServer::shutdown()
+void HttpTestServer::stop()
 {
   //  Interupt the threads
   _interupt = true;
-  //  Cancel the acceptor
-  _acceptor->cancel();
-  //  Stop the IO service and wait for the server to stop
+  //  Close the acceptor
+  _acceptor->close();
+  //  Stop the IO service
   _io_service.stop();
+}
+
+void HttpTestServer::shutdown()
+{
+  //  Stop the server
+  stop();
+  //  Wait for the server to shutdown
   wait();
 }
 
@@ -130,7 +137,9 @@ void HttpTestServer::run_server(int port)
   catch (std::exception& e)
   {
     LOG_ERROR(e.what());
-    shutdown();
+    //  Stop the server here, do not `shutdown`
+    //  Joining the thread inside of the thread causes resource deadlocks
+    stop();
   }
 }
 
@@ -156,10 +165,7 @@ void HttpTestServer::handle_accept(HttpConnection::HttpConnectionPtr new_connect
   if (continue_processing)
     start_accept();
   else
-  {
-    _interupt = true;
-    _io_service.stop();
-  }
+    stop();
 }
 
 bool HttpTestServer::respond(HttpConnection::HttpConnectionPtr& connection)
@@ -186,9 +192,9 @@ std::string HttpTestServer::read_request_body(const std::string& headers, HttpCo
 {
   //  Parse the headers to get the content length
   long content_length = parse_content_length(headers);
-  std::vector<char> buf(content_length);
+  std::vector<char> buf(content_length + 1, 0);
   //  Read the HTTP request body
-  boost::asio::read(connection->socket(), boost::asio::buffer(buf));
+  boost::asio::read(connection->socket(), boost::asio::buffer(buf, content_length));
   return buf.data();
 }
 
Clone this wiki locally