Skip to content

engine+graph: thread-safe last_error + JSON option-parse hardening - #702

Merged
jakmro merged 1 commit into
mainfrom
audit/thread-safe-last-error
Jun 11, 2026
Merged

jakmro merged 1 commit into
mainfrom
audit/thread-safe-last-error

Conversation

@ncylich

@ncylich ncylich commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Make last_error_message thread_local (utils.h declaration, cactus_graph.h declaration, last_error.cpp definition must match for ODR) so concurrent FFI calls don't corrupt each other's error string. utils.h: add try_parse_json_uint to reject negative/overflowing size fields (top_k, max_tokens, tool_rag_top_k, cloud_timeout_ms) that std::stoul would wrap.

Signed-off-by: Noah Cylich noahcylich@gmail.com

Make last_error_message thread_local (utils.h declaration, cactus_graph.h declaration, last_error.cpp definition must match for ODR) so concurrent FFI calls don't corrupt each other's error string. utils.h: add try_parse_json_uint to reject negative/overflowing size fields (top_k, max_tokens, tool_rag_top_k, cloud_timeout_ms) that std::stoul would wrap.

Signed-off-by: Noah Cylich <noahcylich@gmail.com>
@kar-m

kar-m commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

LGTM @jakmro @HenryNdubuaku
Reproduced the bug on main and confirmed it doesnt happen anymore after the fix is introduced

@jakmro
jakmro marked this pull request as ready for review June 11, 2026 22:02
Copilot AI review requested due to automatic review settings June 11, 2026 22:02
@jakmro
jakmro merged commit 366d9e2 into main Jun 11, 2026
7 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make error reporting safer under concurrency by moving the shared last_error_message state to thread-local storage, and to harden JSON option parsing so numeric fields can’t be silently mis-parsed via negative/overflowing inputs.

Changes:

  • Make last_error_message thread_local in both declarations and the definition to avoid cross-thread clobbering in concurrent FFI usage.
  • Introduce try_parse_json_uint and switch selected option fields (top_k, max_tokens, tool_rag_top_k, cloud_timeout_ms) to use it instead of std::stoul.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cactus-graph/src/last_error.cpp Defines last_error_message as thread_local to isolate per-thread error state.
cactus-graph/cactus_graph.h Updates the public declaration of last_error_message to match the TLS definition.
cactus-engine/src/utils.h Updates TLS declaration and adds hardened parsing for several unsigned numeric JSON options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cactus-engine/src/utils.h
Comment on lines +1059 to +1078
inline bool try_parse_json_uint(const std::string& json, const std::string& key, size_t& out_value) {
std::string pattern = "\"" + key + "\":";
size_t pos = json.find(pattern);
if (pos == std::string::npos) return false;

size_t start = pos + pattern.size();
while (start < json.size() && std::isspace(static_cast<unsigned char>(json[start]))) ++start;

size_t end = start;
while (end < json.size() && std::string(",}] \t\n\r").find(json[end]) == std::string::npos) ++end;

try {
long long parsed = std::stoll(json.substr(start, end - start));
if (parsed < 0) return false;
out_value = static_cast<size_t>(parsed);
return true;
} catch (...) {
return false;
}
}
Comment thread cactus-engine/src/utils.h
Comment on lines +1419 to 1422
size_t parsed_top_k = options.top_k;
if (try_parse_json_uint(json, "top_k", parsed_top_k)) {
options.top_k = parsed_top_k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants