From 9c185f1d73dd92ddbf4c8fd5ec88a31543e54c51 Mon Sep 17 00:00:00 2001 From: lavochka Date: Tue, 27 Oct 2026 13:08:18 +0700 Subject: [PATCH 1/2] Isolate HTTP workers from idle chat clients --- README.md | 9 +++- src/main.cpp | 9 ++-- tests/http_under_idle_chat_test.py | 72 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 tests/http_under_idle_chat_test.py diff --git a/README.md b/README.md index a8cef2a..229aa87 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ In the terminal, `/chat ` opens a private dialog, `/back` leaves it, ` ## How it works -The HTTP listener uses an accept loop. The chat listener uses `epoll` to wait for incoming connections. Both hand accepted sockets to one pool of eight workers; each worker handles its client with blocking I/O. The current `epoll` loop does **not** monitor established chat connections. +The HTTP listener uses an accept loop. The chat listener uses `epoll` to wait for incoming connections. Each listener has its own pool of eight workers, so idle TCP chat clients cannot consume HTTP workers. Each worker handles its client with blocking I/O. The current `epoll` loop does **not** monitor established chat connections. Messages and sessions live in process memory, so restarting the server clears them. The browser polls `/chat-data/` every second for new messages. @@ -104,6 +104,13 @@ sh tests/run.sh The runnable server uses Linux `epoll`; the tests can also run on macOS because they compile the portable components separately. +To check that idle chat connections do not stall HTTP on Linux, build the server and run: + +```sh +make -C src +python3 tests/http_under_idle_chat_test.py ./server +``` + ## Security and limits This is a local demo, not an Internet-facing chat service. Login checks a username but does not verify identity. HTTP and TCP traffic is unencrypted. Session IDs are random, but they cannot compensate for the missing authentication and transport encryption. Messages and sessions are not persisted. diff --git a/src/main.cpp b/src/main.cpp index e623314..020ae15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,11 +85,12 @@ int main() std::cout << "Server is listening on port 8080...\n"; std::cout << "Server is listening on port 8081...\n"; - ThreadPool pool(8); + ThreadPool http_pool(8); + ThreadPool chat_pool(8); - std::thread http_thread(run_http_server, http_fd, std::ref(pool)); - EventLoop chat_loop(chat_fd, [&pool](int client_fd, sockaddr_in client_addr) { - pool.submit([client_fd, client_addr] { + std::thread http_thread(run_http_server, http_fd, std::ref(http_pool)); + EventLoop chat_loop(chat_fd, [&chat_pool](int client_fd, sockaddr_in client_addr) { + chat_pool.submit([client_fd, client_addr] { std::cerr << "New client fd=" << client_fd << "\n"; handle_client(client_fd, client_addr); }); diff --git a/tests/http_under_idle_chat_test.py b/tests/http_under_idle_chat_test.py new file mode 100644 index 0000000..a771529 --- /dev/null +++ b/tests/http_under_idle_chat_test.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +"""Linux smoke test: idle chat connections must not block HTTP requests.""" + +import socket +import subprocess +import sys +import time +from pathlib import Path + + +def connect_when_ready(process, port): + deadline = time.monotonic() + 5 + while time.monotonic() < deadline: + if process.poll() is not None: + raise RuntimeError(f"server exited with status {process.returncode}") + try: + return socket.create_connection(("127.0.0.1", port), timeout=1) + except OSError: + time.sleep(0.05) + raise TimeoutError(f"server did not listen on port {port}") + + +def recv_until(sock, delimiter): + data = b"" + while delimiter not in data: + chunk = sock.recv(64) + if not chunk: + raise AssertionError(f"connection closed before {delimiter!r}: {data!r}") + data += chunk + return data + + +def main(): + if len(sys.argv) != 2: + raise SystemExit("usage: python3 tests/http_under_idle_chat_test.py ./server") + + binary = Path(sys.argv[1]).resolve() + root = Path(__file__).resolve().parents[1] + process = subprocess.Popen( + [str(binary)], cwd=root / "src", stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + clients = [] + try: + for _ in range(8): + client = connect_when_ready(process, 8081) + clients.append(client) + client.settimeout(3) + prompt = recv_until(client, b"Login: ") + if prompt != b"Login: ": + raise AssertionError(f"unexpected chat prompt: {prompt!r}") + + with connect_when_ready(process, 8080) as http: + http.settimeout(3) + http.sendall(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n") + response = recv_until(http, b"\r\n") + if not response.startswith(b"HTTP/1.1 200 OK\r\n"): + raise AssertionError(f"HTTP unavailable with eight idle chat clients: {response!r}") + finally: + for client in clients: + client.close() + if process.poll() is None: + process.terminate() + try: + process.wait(timeout=5) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + + +if __name__ == "__main__": + main() From e7758a370c6503bc426781113ece8d61ed2ad426 Mon Sep 17 00:00:00 2001 From: lavochka Date: Tue, 27 Oct 2026 13:08:23 +0700 Subject: [PATCH 2/2] Check Linux build and idle-chat HTTP behavior in CI --- .github/workflows/ci.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..7664a77 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,17 @@ +name: CI + +on: + push: + pull_request: + +jobs: + linux: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v7.0.1 + - name: Run C++ regression tests + run: sh tests/run.sh + - name: Build server + run: make -C src + - name: Keep HTTP responsive with idle chat clients + run: python3 tests/http_under_idle_chat_test.py ./server