diff --git a/.gitignore b/.gitignore index a7848fdd27..617887ef52 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ filament/docs/html/** .vscode gltf_baker.ini *tmp*.png +civetweb.txt diff --git a/filament/src/Engine.cpp b/filament/src/Engine.cpp index 966e150e9d..baff3f353a 100644 --- a/filament/src/Engine.cpp +++ b/filament/src/Engine.cpp @@ -163,7 +163,14 @@ void FEngine::init() { #if FILAMENT_ENABLE_MATDBG // Disable the web server for regression tests that occur in hermetic environments. if (mBackend != backend::Backend::NOOP) { - debug.server = new matdbg::DebugServer(matdbg::ENGINE); + debug.server = new matdbg::DebugServer(matdbg::ENGINE, 8080); + + // Sometimes the server can fail to spin up (e.g. if the above port is already in use). + // When this occurs, carry onward, developers can look at civetweb.txt for details. + if (!debug.server->isReady()) { + delete debug.server; + debug.server = nullptr; + } } #endif diff --git a/libs/matdbg/include/matdbg/DebugServer.h b/libs/matdbg/include/matdbg/DebugServer.h index 27bd021e84..74fe7ff396 100644 --- a/libs/matdbg/include/matdbg/DebugServer.h +++ b/libs/matdbg/include/matdbg/DebugServer.h @@ -54,6 +54,8 @@ public: */ void setEditCallback(EditCallback callback) { mEditCallback = callback; } + bool isReady() const { return mServer; } + private: using MaterialKey = uint32_t; @@ -75,9 +77,9 @@ private: utils::CString mCss; EditCallback mEditCallback = nullptr; - class FileRequestHandler* mFileHandler; - class RestRequestHandler* mRestHandler; - class WebSocketHandler* mWebSocketHandler; + class FileRequestHandler* mFileHandler = nullptr; + class RestRequestHandler* mRestHandler = nullptr; + class WebSocketHandler* mWebSocketHandler = nullptr; friend class FileRequestHandler; friend class RestRequestHandler; diff --git a/libs/matdbg/src/DebugServer.cpp b/libs/matdbg/src/DebugServer.cpp index e17ecdde06..6ad297e1db 100644 --- a/libs/matdbg/src/DebugServer.cpp +++ b/libs/matdbg/src/DebugServer.cpp @@ -345,11 +345,25 @@ DebugServer::DebugServer(ServerMode mode, int port) : mServerMode(mode) { mCss = CString((const char*) MATDBG_RESOURCES_STYLE_DATA, MATDBG_RESOURCES_STYLE_SIZE - 1); #endif - const char* kServerOptions[] = { "listening_ports", "8080", nullptr }; + // By default the server spawns 50 threads so we override this to 2. This limits the server + // to having no more than 2 HTTP clients, which is perfectly fine for debugging purposes. + const char* kServerOptions[] = { + "listening_ports", "8080", + "num_threads", "2", + "error_log_file", "civetweb.txt", + nullptr + }; std::string portString = std::to_string(port); kServerOptions[1] = portString.c_str(); mServer = new CivetServer(kServerOptions); + if (!mServer->getContext()) { + delete mServer; + mServer = nullptr; + slog.e << "Unable to start DebugServer, see civetweb.txt for details." << io::endl; + return; + } + mFileHandler = new FileRequestHandler(this); mRestHandler = new RestRequestHandler(this); mWebSocketHandler = new WebSocketHandler(this); diff --git a/third_party/civetweb/src/CivetServer.cpp b/third_party/civetweb/src/CivetServer.cpp index 5fa4501159..f3d9766cb2 100644 --- a/third_party/civetweb/src/CivetServer.cpp +++ b/third_party/civetweb/src/CivetServer.cpp @@ -290,8 +290,6 @@ CivetServer::CivetServer(const char **options, } callbacks.connection_close = closeHandler; context = mg_start(&callbacks, this, options); - if (context == NULL) - abort(); } CivetServer::CivetServer(std::vector options, diff --git a/third_party/civetweb/tnt/README b/third_party/civetweb/tnt/README index 53d7c86ec9..185dee0320 100644 --- a/third_party/civetweb/tnt/README +++ b/third_party/civetweb/tnt/README @@ -2,4 +2,5 @@ This is how third_party/civetweb was created: 1. Downloaded from https://github.com/civetweb/civetweb/tree/3fadd57 2. `rm -rf Qt VisualStudio ci conan zephyr ci distribution contrib test .git src/third_party` -3. Replaced "throw" statements in CivetServer.cpp with abort(). +3. Removed "throw" statements from CivetServer::CivetServer. +4. Replaced all other "throw" statements with abort().