Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: CI

on:
push:
pull_request:

jobs:
linux:
runs-on: ubuntu-24.04
steps:
- uses: actions/[email protected]
- 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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ In the terminal, `/chat <username>` 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/<username>` every second for new messages.

Expand All @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
72 changes: 72 additions & 0 deletions tests/http_under_idle_chat_test.py
Original file line number Diff line number Diff line change
@@ -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()