diff --git a/src/junction.c b/src/junction.c index 88e999d..e65890c 100644 --- a/src/junction.c +++ b/src/junction.c @@ -2566,7 +2566,10 @@ static int relay_stream(j_conn *client, j_conn *up, const char *pre, size_t pre_ long content_length, double idle_s, double deadline) { char err[192]; - static char buf[RELAY_CHUNK]; + /* Per connection: a static buffer here is shared by every connection + * thread, so two live streams read into the same bytes and each client + * gets pieces of the other's answer. */ + char buf[RELAY_CHUNK]; long remaining = content_length; if (pre && pre_len) { size_t n = pre_len; @@ -2604,7 +2607,9 @@ static int relay_dechunk(j_conn *client, j_conn *up, const char *pre, size_t pre double idle_s, double deadline) { char err[192]; - static char in[RELAY_CHUNK * 8]; + /* Per connection, for the same reason as relay_stream: concurrent + * streams must never share the read buffer. */ + char in[RELAY_CHUNK * 8]; size_t have = 0; if (pre && pre_len) { if (pre_len > sizeof in) { diff --git a/tests/run.sh b/tests/run.sh index c2e3cba..2528631 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -412,6 +412,16 @@ case "$SSE2" in *) bad "capital-Chunked missed: $(printf '%s' "$SSE2" | head -c 30 | tr -d '\r\n')" ;; esac +# Two live streams used to read into the same static relay buffers: every +# client got deltas from several completions, which splinter assembled into +# text with words missing and sentences spliced in. The fake upstream tags +# every delta with its completion id and index, so a mixture is visible. +if RACE=$(python3 tests/stream_race.py ./junction 2>&1); then + ok "concurrent streams keep their own bytes" +else + bad "concurrent streams mixed: $(printf '%s' "$RACE" | tail -3 | tr '\n' ' ')" +fi + echo "== 10c. management endpoints are loopback-only ==" MG=$((PORT+80)) cat > "$TMP/mgmt.conf" < 1 else "./junction" + if JUNCTION_PORT == 0: + JUNCTION_PORT = free_port() + if UPSTREAM_PORT == 0: + UPSTREAM_PORT = free_port() + while UPSTREAM_PORT == JUNCTION_PORT: + UPSTREAM_PORT = free_port() + tmp = tempfile.mkdtemp(prefix="junction-race-") + config = os.path.join(tmp, "race.conf") + with open(config, "w") as f: + f.write("""listen 127.0.0.1:%d +max_inflight %d +cache_ttl 0 +probe_interval 0 + +endpoint fake { + base http://127.0.0.1:%d/v1 + kind local + context 100000 + timeout 30 + idle_timeout 15 +} + +req for "*" check none pass fake +""" % (JUNCTION_PORT, CLIENTS * 2, UPSTREAM_PORT)) + + upstream = ThreadingHTTPServer(("127.0.0.1", UPSTREAM_PORT), Upstream) + upstream.daemon_threads = True + upstream.next_id = 0 + upstream.id_lock = threading.Lock() + threading.Thread(target=upstream.serve_forever, daemon=True).start() + + log = open(os.path.join(tmp, "junction.log"), "w") + junction = subprocess.Popen([binary, "--config", config], + stdout=log, stderr=subprocess.STDOUT) + try: + if not wait_port(JUNCTION_PORT) or not wait_port(UPSTREAM_PORT): + print("junction or fake upstream did not come up", file=sys.stderr) + return 1 + url = "http://127.0.0.1:%d/v1/chat/completions" % JUNCTION_PORT + failures = 0 + for round_no in range(ROUNDS): + results, lock = [], threading.Lock() + threads = [threading.Thread(target=one, args=(i, url, results, lock)) + for i in range(CLIENTS)] + for t in threads: + t.start() + for t in threads: + t.join() + for client_no, detail in sorted(results): + if detail: + print("round %d client %d: %s" % (round_no + 1, client_no, detail)) + failures += 1 + if failures: + print("stream race: %d of %d streams carried foreign or missing deltas" + % (failures, ROUNDS * CLIENTS)) + log.flush() + with open(log.name) as f: + tail = f.read()[-1500:] + if tail: + print("junction log tail:\n" + tail, file=sys.stderr) + return 1 + print("%d/%d streams clean" % (ROUNDS * CLIENTS, ROUNDS * CLIENTS)) + return 0 + finally: + junction.terminate() + try: + junction.wait(timeout=5) + except subprocess.TimeoutExpired: + junction.kill() + upstream.shutdown() + log.close() + + +if __name__ == "__main__": + sys.exit(main())