{"id":5687,"date":"2026-05-11T18:43:37","date_gmt":"2026-05-11T09:43:37","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5687"},"modified":"2026-05-11T19:00:14","modified_gmt":"2026-05-11T10:00:14","slug":"webserverminiwebserver-10","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/11\/webserverminiwebserver-10\/","title":{"rendered":"[Webserver]miniWebserver(thread detach->join)-(10)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc8fc\uc694 \ubcc0\uacbd\uc0ac\ud56d\uc740 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<br>The major changes are as follows.<\/p>\n\n\n\n<p>\u2714\ufe0f \u2757\ufe0f \ud45c\uc2dc\ub41c \ubd80\ubd84\uc774 \ubcc0\uacbd\uc0ac\ud56d\uc785\ub2c8\ub2e4.<br>The parts marked with \u2757\ufe0f are the changes.<\/p>\n\n\n\n<p>\u2714\ufe0f 5080\uc740http \uc11c\ubc84\uc774\uace0 6443\uc740 https\uc11c\ubc84\uc785\ub2c8\ub2e4. 6080\uc11c\ubc84\ub294 \uc0ad\uc81c\ud588\uc2b5\ub2c8\ub2e4.<br>5080 is an HTTP server and 6443 is an HTTPS server. Server 6080 has been deleted.<\/p>\n\n\n\n<p>\u2714\ufe0f\uc11c\ubc84 \uc885\ub8cc\uc2dc\uc5d0\ub3c4 handle_client\ud568\uc218\uac00 \uc2e4\ud589\ub418\uc5b4 &#8220;[SSL] SSL_new() failed&#8221;\uba54\uc138\uc9c0 \ucd9c\ub825\ub418\ub294 \ubb38\uc81c\ub97c \uad50\uc815\ud588\uc2b5\ub2c8\ub2e4.<br>Fixed an issue where the handle_client function was executed and the &#8220;[SSL] SSL_new() failed&#8221; message was displayed even when the server was terminated.<\/p>\n\n\n\n<p>&#8212; \uc11c\ubc84 \uc885\ub8cc \uc2dc\uc810\uc5d0 ssl_ctx\uac00 \ud574\uc81c\ub418\uc5b4 \uae30\uc874\uc5d0 \uc791\uc5c5 \uc911\uc774\uac70\ub098 \uc7ac\uc2e4\ud589\ub418\ub358 \uc2a4\ub808\ub4dc\uc5d0\uc11c \uc624\ub958\ub97c \ubc1c\uc0dd\uc2dc\ud0a4\ub294 \ubb38\uc81c\uac00 \uc788\uc5c8\uc2b5\ub2c8\ub2e4.<br>There was an issue where ssl_ctx was released at the time of server shutdown, causing errors in existing working or restarted threads.<\/p>\n\n\n\n<p>&#8212; \ubaa8\ub4e0 \uc2a4\ub808\ub4dc\ub97c \ud574\uc81c\ud55c \ud6c4 ssl_ctx\ub97c \ud574\uc81c\ub97c \uc2e4\ud589\ud568\uc73c\ub85c\uc368 \uc624\ub958\ub97c \ud574\uacb0\ud588\uc2b5\ub2c8\ub2e4.<br>I resolved the error by releasing ssl_ctx after releasing all threads.<\/p>\n\n\n\n<p>\u2714\ufe0f \uc624\ub958 \uba54\uc138\uc9c0\ub294 SSL (https)\uc0ac\uc6a9\ud558\ub294 HttpsServer.cpp\uc5d0\uc11c \ubc1c\uc0dd\ud588\uc9c0\ub9cc http\ub3c4 \ub3d9\uc77c\ud558\uac8c \uad6c\uc870\ub97c \ubcc0\uacbd\ud588\uc2b5\ub2c8\ub2e4.<br>The error message occurred in HttpsServer.cpp using SSL (https), but the structure was changed in the same way for http.<\/p>\n\n\n\n<p>\u2714\ufe0f exit \uba85\ub839\uc5b4\ub85c \uc11c\ubc84 \uc885\ub8cc\uc2dc \uc2a4\ub808\ub4dc\uac00 \uc644\uc804\ud788 \uc815\ub9ac\ub418\uae30\uae4c\uc9c0 \uc57d\uac04\uc758 \uc2dc\uac04\uc774 \uc18c\uc694\ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>When terminating the server with the exit command, it may take some time for threads to be completely cleaned up.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>\u2714\ufe0f \uc2a4\ub808\ub4dc \ubc31\ud130\uc120\uc5b8 \/ Thread vector declaration<\/p>\n\n\n\n<p>&#8212; HttpServer.h\uc5d0 \uc2a4\ub808\ub4dc\ub97c \uc62e\uaca8 \ub2f4\uc744 \ubc31\ud130\ub97c \uc120\uc5b8\ud569\ub2c8\ub2e4.<br>Declare a vector to store threads in HttpServer.h.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::vector&lt;std::thread&gt; client_threads;<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ub8e8\ud504\ud0c8\ucd9c,\uc2a4\ub808\ub4dc \uc800\uc7a5 \/ Loop escape, thread save<\/p>\n\n\n\n<p>&#8212; HttpsServer.cpp,Server.cpp<\/p>\n\n\n\n<p>&#8212; running.load() \uac00 false\uc774\uba74 while\ub8e8\ud504 \ud0c8\ucd9c\ud558\uace0 \uc2a4\ub808\ub4dc\ub97c \ubc31\ud130\uc5d0 \uc800\uc7a5\ud569\ub2c8\ub2e4.<br>If running.load() is false, exit the while loop and save the thread to the vector.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    while (running.load()) {\n\n        \/\/ \ub8e8\ud504\ud0c8\ucd9c \/ Loop escape \u2757\ufe0f\n        if (!running.load()) {\n            break;  \/\/ \uc885\ub8cc \uc2e0\ud638 \ubc1b\uc73c\uba74 \ub8e8\ud504 \uc885\ub8cc \/ Terminate the loop when the termination signal is received.\n        }\n\n        ... \uc911\ub7b5 \/ skipping the middle...\n\n        if (running.load() &amp;&amp; client_socket &lt; 0) {\n\n            \/\/ \ub8e8\ud504\ud0c8\ucd9c \/ Loop escape \u2757\ufe0f\n            if (!running.load()) {\n                break;  \/\/ \uc18c\ucf13 \ub2eb\ud600\uc11c \uc2e4\ud328\ud55c \uacbd\uc6b0 \uc885\ub8cc \/ Terminate if failed due to socket closing\n            }\n\n            perror(\"accept failed\");\n            continue; \n        }\n        \/\/ \ud074\ub77c\uc774\uc5b8\ud2b8 \uc811\uc18d \/ Client connected\n        std::thread client_thread(&#91;this, client_socket]() {\n\n            \/\/\uc678\ubd80\uc5d0\uc11c \uba54\uc138\uc9c0 \ubc14\uafb8\uae30 \/ Change message externally\n            this-&gt;handle_client(client_socket);\n        });\n        \/\/ \uc2a4\ub808\ub4dc - \ubc31\ud130\uc5d0 \uc800\uc7a5 \/ Thread - Stored in a vector\u2757\ufe0f\n        client_threads.push_back(std::move(client_thread));\n\n        \/\/client_thread.detach(); \/\/ \uc774\uc804\ucf54\ub4dc \/ previous code\u2757\ufe0f\n    }<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2a4\ub808\ub4dc \uc885\ub8cc \ud6c4 ssl_ctx\uc2e4\ud589<br>Execute ssl_ctx after thread termination<\/p>\n\n\n\n<p>&#8212; for\ubb38\uc73c\ub85c \ubc31\ud130\uc5d0 \uc800\uc7a5\ub41c \uc2a4\ub808\ub4dc\ub97c \ud558\ub098\uc529 \uc0ac\uc6a9\uc911\uc778\uc9c0 \ud655\uc778(joinable()\ud558\uace0 \uc0ac\uc6a9\uc911\uc774 \uc544\ub2c8\uba74(joinable() == true) \uc2a4\ub808\ub4dc\uac00 \uc885\ub8cc\ub420\ub54c\uae4c\uc9c0 \ub300\uae30(join())\ud569\ub2c8\ub2e4.<br>Use a for loop to check each thread stored in the vector one by one if it is in use (joinable()), and if it is not in use (joinable() == true), wait until the thread terminates (join()).<\/p>\n\n\n\n<p>&#8212; \ubaa8\ub4e0 \uc2a4\ub808\ub4dc\uac00 \ub300\uae30\uc0c1\ud0dc\uac00\ub418\uba74 \uc2a4\ub808\ub4dc\ub97c \uc885\ub8cc \ud569\ub2c8\ub2e4.(client_threads.clear();)<br>Terminates threads when all threads are in a waiting state.(client_threads.clear();)<\/p>\n\n\n\n<p>&#8212; \uc2a4\ub808\ub4dc \uc885\ub8cc\uac00 \uc644\ub8cc\ub418\uba74 ssl_ctx\ub97c \ud574\uc81c\ud569\ub2c8\ub2e4.<br>Release ssl_ctx when thread termination is complete.<\/p>\n\n\n\n<p>&#8212; HttpsServer.cpp stop() \ud568\uc218 \/ function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void https::Server::stop() {\n  try {\n    if (running.load() &amp;&amp; server_fd != -1) { \n\n        ... \uc911\ub7b5 \/ skipping the middle ...\n\n        \/\/ \ubaa8\ub4e0 \ud074\ub77c\uc774\uc5b8\ud2b8 \uc2a4\ub808\ub4dc \uc885\ub8cc \ub300\uae30\u2757\ufe0f\u2757\ufe0f\n        for (auto&amp; t : client_threads) {\n            if (t.joinable()) {\n                t.join();\n            }\n        }\n        client_threads.clear();\n\n        \/\/ SSL \ucee8\ud14d\uc2a4\ud2b8 \ud574\uc81c (\uc874\uc7ac\ud55c\ub2e4\uba74) \/  Release SSL context (if any)\n         if (ssl_ctx) {\n            SSL_CTX_free(ssl_ctx);\n            ssl_ctx = nullptr;\n        }\n\n        \/\/ OpenSSL \ub77c\uc774\ube0c\ub7ec\ub9ac \uc815\ub9ac \/ OpenSSL Library Cleanup \u2757\ufe0f\u2757\ufe0f\n        \/\/ OpenSSL 3.x\uc5d0\uc11c\ub294 \ubcc4\ub3c4\uc758 \uc815\ub9ac \ud568\uc218 \ud638\ucd9c \ubd88\ud544\uc694\ud569\ub2c8\ub2e4.\n        \/\/EVP_cleanup();\n\n        std::cout &lt;&lt; \"&#91;Server] Shutdown initiated.\" &lt;&lt; std::endl;\n    }\n      } catch (const std::exception&amp; e) {\n            std::cerr &lt;&lt; \"&#91;ERROR] Exception during shutdown: \" &lt;&lt; e.what() &lt;&lt; std::endl;\n    } catch (...) {\n            std::cerr &lt;&lt; \"&#91;ERROR] Unknown exception during shutdown.\" &lt;&lt; std::endl;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ube4c\ub4dc \/ Build<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd build\ncmake ..\nmake<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/main<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ud130\ubbf8\ub110 \uc2e4\ud589 \uacb0\uacfc \/ Terminal execution result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MacBookAir build % .\/main\n&#91;SSL] SSL context successfully initialized\n--- Starting Server Application ---\nServer listening on port 5080...\nHttps Server listening on port 6443...\nCMD&gt; exit\n&#91;INFO] \uc885\ub8cc \uba85\ub839\uc744 \ubc1b\uc558\uc2b5\ub2c8\ub2e4. \uc11c\ube44\uc2a4 \uc885\ub8cc\ub97c \uc2dc\uc791\ud569\ub2c8\ub2e4.\n&#91;INFO] Received a shutdown command. Starting service shutdown.\n&#91;Server] Shutdown initiated.\n&#91;Server] Shutdown initiated.\n&#91;MAIN] Server Application Finished.\nMacBookAir build % <\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc8fc\uc694 \ubcc0\uacbd\uc0ac\ud56d\uc740 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.The major changes are as follows. \u2714\ufe0f \u2757\ufe0f \ud45c\uc2dc\ub41c \ubd80\ubd84\uc774 \ubcc0\uacbd\uc0ac\ud56d\uc785\ub2c8\ub2e4.The parts marked with \u2757\ufe0f are the changes. \u2714\ufe0f 5080\uc740http \uc11c\ubc84\uc774\uace0 6443\uc740 https\uc11c\ubc84\uc785\ub2c8\ub2e4. 6080\uc11c\ubc84\ub294 \uc0ad\uc81c\ud588\uc2b5\ub2c8\ub2e4.5080 is an HTTP server and 6443 is an HTTPS server. Server 6080 has been deleted. \u2714\ufe0f\uc11c\ubc84 \uc885\ub8cc\uc2dc\uc5d0\ub3c4 handle_client\ud568\uc218\uac00 \uc2e4\ud589\ub418\uc5b4 &#8220;[SSL] SSL_new() failed&#8221;\uba54\uc138\uc9c0 \ucd9c\ub825\ub418\ub294 \ubb38\uc81c\ub97c \uad50\uc815\ud588\uc2b5\ub2c8\ub2e4.Fixed an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5687","post","type-post","status-publish","format-standard","hentry","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5687"}],"version-history":[{"count":4,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5687\/revisions"}],"predecessor-version":[{"id":5692,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5687\/revisions\/5692"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}