2525#include < string>
2626#include < iostream>
2727#include < mutex>
28+ #include < condition_variable>
2829#include < cerrno>
2930#include " log.h"
3031
@@ -178,6 +179,7 @@ class HttpTransport : public McpTransportInterface
178179 Error (" [MCP] HTTP send() failed: %d" , sent);
179180 SOCKET_CLOSE (m_current_client);
180181 m_current_client = INVALID_SOCKET_VALUE;
182+ m_client_cv.notify_all ();
181183 return false ;
182184 }
183185
@@ -190,6 +192,7 @@ class HttpTransport : public McpTransportInterface
190192 // Close connection after response (VS Code will reconnect for next request)
191193 SOCKET_CLOSE (m_current_client);
192194 m_current_client = INVALID_SOCKET_VALUE;
195+ m_client_cv.notify_all ();
193196
194197 return true ;
195198 }
@@ -232,6 +235,7 @@ class HttpTransport : public McpTransportInterface
232235 {
233236 std::lock_guard<std::mutex> lock (m_mutex);
234237 m_current_client = INVALID_SOCKET_VALUE;
238+ m_client_cv.notify_all ();
235239 }
236240 return false ;
237241 }
@@ -240,7 +244,22 @@ class HttpTransport : public McpTransportInterface
240244 {
241245 while (!m_closed && m_server_socket != INVALID_SOCKET_VALUE)
242246 {
243- // Always accept new connection (we close after each response)
247+ // Wait until any previous client has been responded to and closed
248+ {
249+ std::unique_lock<std::mutex> lock (m_mutex);
250+ m_client_cv.wait (lock, [this ]
251+ {
252+ return m_closed || m_current_client == INVALID_SOCKET_VALUE;
253+ });
254+
255+ if (m_closed)
256+ {
257+ Debug (" [MCP] Server closed, stopping recv loop" );
258+ return false ;
259+ }
260+ }
261+
262+ // Accept new connection (we close after each response)
244263 struct sockaddr_in client_addr;
245264 socklen_t client_len = sizeof (client_addr);
246265 socket_t client = accept (m_server_socket, (struct sockaddr *)&client_addr, &client_len);
@@ -346,6 +365,7 @@ class HttpTransport : public McpTransportInterface
346365 std::lock_guard<std::mutex> lock (m_mutex);
347366 SOCKET_CLOSE (m_current_client);
348367 m_current_client = INVALID_SOCKET_VALUE;
368+ m_client_cv.notify_all ();
349369 continue ; // Wait for next connection
350370 }
351371
@@ -367,6 +387,7 @@ class HttpTransport : public McpTransportInterface
367387 std::lock_guard<std::mutex> lock (m_mutex);
368388 SOCKET_CLOSE (m_current_client);
369389 m_current_client = INVALID_SOCKET_VALUE;
390+ m_client_cv.notify_all ();
370391 continue ; // Wait for next connection
371392 }
372393
@@ -406,6 +427,7 @@ class HttpTransport : public McpTransportInterface
406427 std::lock_guard<std::mutex> lock (m_mutex);
407428 SOCKET_CLOSE (m_current_client);
408429 m_current_client = INVALID_SOCKET_VALUE;
430+ m_client_cv.notify_all ();
409431 continue ; // Wait for next request
410432 }
411433
@@ -421,6 +443,7 @@ class HttpTransport : public McpTransportInterface
421443 std::lock_guard<std::mutex> lock (m_mutex);
422444 SOCKET_CLOSE (m_current_client);
423445 m_current_client = INVALID_SOCKET_VALUE;
446+ m_client_cv.notify_all ();
424447 continue ; // Wait for next request
425448 }
426449
@@ -439,6 +462,7 @@ class HttpTransport : public McpTransportInterface
439462 std::lock_guard<std::mutex> lock (m_mutex);
440463 SOCKET_CLOSE (m_current_client);
441464 m_current_client = INVALID_SOCKET_VALUE;
465+ m_client_cv.notify_all ();
442466 continue ; // Wait for next connection
443467 }
444468 }
@@ -449,6 +473,7 @@ class HttpTransport : public McpTransportInterface
449473 void close ()
450474 {
451475 m_closed = true ;
476+ m_client_cv.notify_all ();
452477
453478 if (m_current_client != INVALID_SOCKET_VALUE)
454479 {
@@ -465,6 +490,7 @@ class HttpTransport : public McpTransportInterface
465490
466491private:
467492 std::mutex m_mutex;
493+ std::condition_variable m_client_cv;
468494 bool m_closed;
469495 socket_t m_server_socket;
470496 socket_t m_current_client;
0 commit comments