Panic on multi-byte socket payload in event handler log truncation
Summary
openhuman_core::openhuman::socket::event_handlers::handle_sio_event panics with byte index 500 is not a char boundary when the incoming Socket.IO event payload contains a multi-byte UTF-8 character that straddles byte 500 of its JSON serialization. The core thread aborts and Sentry captures it as a fatal panic.
Problem
Real Sentry event: a WhatsApp ingest payload containing Cyrillic 'н' at bytes 499..501 of the JSON string panicked the socket event handler. Title was panic: byte index 500 is not a char boundary; it is inside 'н' (bytes 499..501) of {"metadata":{"id":"ti_z0x_LwO_nwx7", ....
Sentry issue: OPENHUMAN-TAURI-KC. Tags: os.name=windows, level=fatal, mechanism=panic. Will fire for any incoming socket event whose JSON serialization is at least 500 bytes and lands a multi-byte char at byte 500.
Root cause is one naked byte slice used purely for debug-log truncation in src/openhuman/socket/event_handlers.rs:38:
log::debug!(
"[socket] event payload: name={} data={}",
event_name,
&data.to_string()[..data.to_string().len().min(500)] // line 38
);
Steps to reproduce:
- Send any Socket.IO event whose JSON payload serializes to at least 500 bytes and contains a multi-byte UTF-8 char straddling byte 500 (any Cyrillic, Greek, CJK, or Arabic content of the right length will do).
handle_sio_eventpanics, core thread dies, Sentry captures a fatal.
Solution (optional)
Replace the naked byte slice with a char-boundary-safe truncation helper — same shape as the markdown_body_preview() helper in src/openhuman/memory/tree/ingest.rs:366 (shipped in #1751):
fn truncate_at_char_boundary(s: &str, max_bytes: usize) -> &str {
let mut end = s.len().min(max_bytes);
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
Bonus: avoid the double data.to_string() call by binding the string once.
Acceptance criteria
- [ ] Slice site in
event_handlers.rsno longer panics for inputs straddling a multi-byte boundary at byte 500. - [ ] Regression test: a unit test that hands
handle_sio_eventa payload whose JSON-serialized string has a multi-byte char straddling byte 500, asserts no panic. - [ ] Diff coverage at least 80% on changed lines.
Related
- Sentry: OPENHUMAN-TAURI-KC
- Sibling panic in the same code shape on
policy.rscommand log truncation — #1813. - Prior fix-pattern reference:
markdown_body_preview_respects_utf8_boundary_and_byte_captest in src/openhuman/memory/tree/ingest.rs:501 (#1751).
The bug is in the .min(500) guard — it clamps the byte length to 500, but &str slicing operates on bytes, so it still panics when byte 500 is mid-char. Here’s the complete fix: The Fix
rust:
// src/openhuman/socket/event_handlers.rs
// Before (line 38) — .min(500) only prevents over-slicing, not mid-char slicing log::debug!( "[socket] event payload: name={} data={}", event_name, &data.to_string()[..data.to_string().len().min(500)] );
// After — walk back from byte 500 to the nearest valid char boundary log::debug!( "[socket] event payload: name={} data={}", event_name, truncate_at_char_boundary(&data.to_string(), 500) );
Add this helper in the same file (or a shared utils module):
/// Truncate s to at most max_bytes bytes without splitting a UTF-8 char.
/// Walks back from max_bytes at most 3 bytes to find a valid boundary.
fn truncate_at_char_boundary(s: &str, max_bytes: usize) -> &str {
if s.len() <= max_bytes {
return s;
}
let mut boundary = max_bytes;
while !s.is_char_boundary(boundary) {
boundary -= 1;
}
&s[..boundary]
}
Two secondary issues also fixed by this change