[PR #205] [CLOSED] fix(engine): clear stale error/cancellation string between requests and before prompt prefill #2505

Closed
opened 2026-11-04 02:00:48 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/205
Author: @praveshkhatana
Created: 9/30/2026
Status: ❌ Closed

Base: main ← Head: fix-prompt-cancel-state-leak


📝 Commits (1)

  • c507666 fix(engine): clear stale error/cancellation state between requests and before prompt prefill

📊 Changes

2 files changed (+3 additions, -0 deletions)

View changed files

📝 src/prefill/prefill.cpp (+1 -0)
📝 src/program/generate.cpp (+2 -0)

📄 Description

Summary

In v0.1.26 and v0.1.27, if any request is cancelled while reading prompt chunks (e.g. client disconnects or times out on long contexts), the variable err is set to "cancelled".

Because std::string err is declared in int main() (around line 1311 of src/program/generate.cpp) and is never cleared at the start of a request, this string lingers into subsequent requests.

In commit c6c65947 (E-9), sp.on_chunk was changed to:

const bool batched = !multi_gpu && sp.draft_kv(mtp, R_rows, nxt.data(), T, p0, e);
if (!e.empty() || (!batched && !mtp.prefill(R_rows, nxt.data(), T, p0, e))) return false;

Because e is passed by reference from Prefill::run(..., err), !e.empty() evaluates to true on chunk 0 of the next request. This causes sp.on_chunk to immediately abort without executing, Prefill::run returns false, and generate.cpp hits:

if (!sp_ok) {
    if (!stop_req.load()) {
        std::fprintf(stderr, "strata serve: %s\n", err.c_str());
        std::printf("ERR %s\n", err.c_str());
        return 1;
    }
...

Because the current request was not stopped (stop_req is false), the engine outputs strata serve: cancelled, sends ERR cancelled to stdout, and terminates with exit code 1.


Reproduction Steps

  1. Start strata --serve on v0.1.26 or v0.1.27.
  2. Send a request with a large context and abort/disconnect the client midway through prompt processing (triggers STOP -> err = "cancelled").
  3. Send a new valid request that exceeds --short-read (e.g. prompt length > 64 tokens, requiring batched prefill).
  4. Result: The engine aborts on chunk 0, prints strata serve: cancelled, and exits with code 1.

Fix

  1. Call err.clear() right after stop_req.store(false) in while (next_line(line)) in src/program/generate.cpp.
  2. Call err.clear() before iterating prompt segments in for (const int64_t to : {reread_to, root_at, turn_at, n - 1}).
  3. Call err.clear() on entry to Prefill::run(...) in src/prefill/prefill.cpp.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/205 **Author:** [@praveshkhatana](https://github.com/praveshkhatana) **Created:** 9/30/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix-prompt-cancel-state-leak` --- ### 📝 Commits (1) - [`c507666`](https://github.com/Niko1221/Strata/commit/c50766693fbc036ef7f022b2f99224af74e7c80b) fix(engine): clear stale error/cancellation state between requests and before prompt prefill ### 📊 Changes **2 files changed** (+3 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `src/prefill/prefill.cpp` (+1 -0) 📝 `src/program/generate.cpp` (+2 -0) </details> ### 📄 Description ## Summary In v0.1.26 and v0.1.27, if any request is cancelled while reading prompt chunks (e.g. client disconnects or times out on long contexts), the variable `err` is set to `"cancelled"`. Because `std::string err` is declared in `int main()` (around line 1311 of `src/program/generate.cpp`) and is never cleared at the start of a request, this string lingers into subsequent requests. In commit `c6c65947` (E-9), `sp.on_chunk` was changed to: ```cpp const bool batched = !multi_gpu && sp.draft_kv(mtp, R_rows, nxt.data(), T, p0, e); if (!e.empty() || (!batched && !mtp.prefill(R_rows, nxt.data(), T, p0, e))) return false; ``` Because `e` is passed by reference from `Prefill::run(..., err)`, `!e.empty()` evaluates to `true` on chunk 0 of the *next* request. This causes `sp.on_chunk` to immediately abort without executing, `Prefill::run` returns `false`, and `generate.cpp` hits: ```cpp if (!sp_ok) { if (!stop_req.load()) { std::fprintf(stderr, "strata serve: %s\n", err.c_str()); std::printf("ERR %s\n", err.c_str()); return 1; } ... ``` Because the current request was not stopped (`stop_req` is false), the engine outputs `strata serve: cancelled`, sends `ERR cancelled` to stdout, and terminates with exit code 1. --- ## Reproduction Steps 1. Start `strata --serve` on v0.1.26 or v0.1.27. 2. Send a request with a large context and abort/disconnect the client midway through prompt processing (triggers `STOP` -> `err = "cancelled"`). 3. Send a new valid request that exceeds `--short-read` (e.g. prompt length > 64 tokens, requiring batched prefill). 4. **Result:** The engine aborts on chunk 0, prints `strata serve: cancelled`, and exits with code 1. --- ## Fix 1. Call `err.clear()` right after `stop_req.store(false)` in `while (next_line(line))` in `src/program/generate.cpp`. 2. Call `err.clear()` before iterating prompt segments in `for (const int64_t to : {reread_to, root_at, turn_at, n - 1})`. 3. Call `err.clear()` on entry to `Prefill::run(...)` in `src/prefill/prefill.cpp`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:00:48 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2505
No description provided.