#!/usr/bin/env python3 """splinter -- prove the gateway gives up on a client that goes quiet. The accept loop serves one connection at a time. Without a read timeout a client that sends half a header and then nothing held the whole gateway: the second connection below was accepted by the kernel and never answered. argv: port. Exit 0 when the second client got a reply. """ import socket import sys import time port = int(sys.argv[1]) lazy = socket.create_connection(("127.0.0.1", port), timeout=10) lazy.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Le") # the server's read timeout is 2s here; wait past it so the lazy client is # dropped before the second one is queued time.sleep(3.0) good = socket.create_connection(("127.0.0.1", port), timeout=10) body = b'{"model":"x","messages":[{"role":"user","content":"hi"}]}' good.sendall( b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nConnection: close\r\n" b"Content-Type: application/json\r\nContent-Length: %d\r\n\r\n" % len(body) + body) good.settimeout(10) try: data = good.recv(400) except socket.timeout: print("second client got nothing: the gateway is still held") sys.exit(1) finally: lazy.close() good.close() if data.startswith(b"HTTP/1.1 200"): print("second client answered") sys.exit(0) print("second client got %r" % data[:80]) sys.exit(1)