From 6339e8610da7d55b09e78e1b9517ce0aaf597924 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 13 Oct 2026 00:28:26 +0200 Subject: [PATCH] serve: the stdin reader shares its state with main instead of borrowing main's locals (QUIT race) The stdin reader thread is detached and captured main's stop flag, mutex, condition variable, line queue and EOF flag by reference. The server writes QUIT and closes stdin right after it, so the reader often wakes with EOF while main is returning: it then locked a destroyed mutex and wrote to a destroyed deque (undefined behaviour on every platform; the Metal port aborted on it). The state now lives in a shared_ptr that the thread holds a copy of, so it outlives main's frame for as long as the reader runs. CUDA/HIP and SYCL engines alike; no change to what the engine reads or answers. --- src/program/generate.cpp | 37 +++++++++++++++++++++++------------ sycl/src/program/generate.cpp | 37 +++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 7a93c0a3..7dbb4226 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -9042,12 +9042,23 @@ int main(int argc, char** argv) { }; // stdin is read on its own thread, so a STOP line reaches a request that is still running (the client went // away, or pressed Esc): the flag is checked between prompt chunks and between verify windows. - std::atomic stop_req{false}; - std::mutex in_mu; - std::condition_variable in_cv; - std::deque in_lines; - bool in_eof = false; - std::thread([&] { + // 0.1.44: the reader is detached and may sit in read(2) past QUIT (the server writes more, or closes stdin, while + // main returns and the process exits), so what it touches is shared with it, never main's locals: a line or + // EOF after QUIT used to lock a destroyed mutex (undefined behaviour; an abort on macOS). + struct StdinShared { + std::atomic stop_req{false}; + std::mutex mu; + std::condition_variable cv; + std::deque lines; + bool eof = false; + }; + const auto in_shared = std::make_shared(); + std::atomic& stop_req = in_shared->stop_req; + std::mutex& in_mu = in_shared->mu; + std::condition_variable& in_cv = in_shared->cv; + std::deque& in_lines = in_shared->lines; + bool& in_eof = in_shared->eof; + std::thread([st = in_shared] { strata::aux_cpus::pin_current_thread(); // read(2) on the descriptor, not std::cin: glibc's exit() flushes every stdio stream and waits for // stdin's lock, which getline holds while it waits for input - an engine ending on an error (every @@ -9079,14 +9090,14 @@ int main(int argc, char** argv) { }; while (getline_fd(l)) { if (!l.empty() && l.back() == '\r') l.pop_back(); - if (l == "STOP") { stop_req.store(true); continue; } - std::lock_guard lk(in_mu); - in_lines.push_back(l); - in_cv.notify_one(); + if (l == "STOP") { st->stop_req.store(true); continue; } + std::lock_guard lk(st->mu); + st->lines.push_back(l); + st->cv.notify_one(); } - std::lock_guard lk(in_mu); - in_eof = true; - in_cv.notify_one(); + std::lock_guard lk(st->mu); + st->eof = true; + st->cv.notify_one(); }).detach(); auto next_line = [&](std::string& out) -> bool { std::unique_lock lk(in_mu); diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp index 5d6f5866..4d97cb30 100644 --- a/sycl/src/program/generate.cpp +++ b/sycl/src/program/generate.cpp @@ -8402,12 +8402,23 @@ int main(int argc, char **argv) try { }; // stdin is read on its own thread, so a STOP line reaches a request that is still running (the client went // away, or pressed Esc): the flag is checked between prompt chunks and between verify windows. - std::atomic stop_req{false}; - std::mutex in_mu; - std::condition_variable in_cv; - std::deque in_lines; - bool in_eof = false; - std::thread([&] { + // 0.1.44: the reader is detached and may sit in read(2) past QUIT (the server writes more, or closes stdin, while + // main returns and the process exits), so what it touches is shared with it, never main's locals: a line or + // EOF after QUIT used to lock a destroyed mutex (undefined behaviour; an abort on macOS). + struct StdinShared { + std::atomic stop_req{false}; + std::mutex mu; + std::condition_variable cv; + std::deque lines; + bool eof = false; + }; + const auto in_shared = std::make_shared(); + std::atomic& stop_req = in_shared->stop_req; + std::mutex& in_mu = in_shared->mu; + std::condition_variable& in_cv = in_shared->cv; + std::deque& in_lines = in_shared->lines; + bool& in_eof = in_shared->eof; + std::thread([st = in_shared] { // read(2) on the descriptor, not std::cin: glibc's exit() flushes every stdio stream and waits for // stdin's lock, which getline holds while it waits for input - an engine ending on an error (every // std::exit) would hang in exit() on Linux, and the server would wait for it forever @@ -8438,14 +8449,14 @@ int main(int argc, char **argv) try { }; while (getline_fd(l)) { if (!l.empty() && l.back() == '\r') l.pop_back(); - if (l == "STOP") { stop_req.store(true); continue; } - std::lock_guard lk(in_mu); - in_lines.push_back(l); - in_cv.notify_one(); + if (l == "STOP") { st->stop_req.store(true); continue; } + std::lock_guard lk(st->mu); + st->lines.push_back(l); + st->cv.notify_one(); } - std::lock_guard lk(in_mu); - in_eof = true; - in_cv.notify_one(); + std::lock_guard lk(st->mu); + st->eof = true; + st->cv.notify_one(); }).detach(); auto next_line = [&](std::string& out) -> bool { std::unique_lock lk(in_mu);