Conversation
There was a problem hiding this comment.
Review Summary
Assessment: 🟡 NEEDS ATTENTION
The PR successfully fixes the 64KB line limit issue by replacing bufio.Scanner with json.NewDecoder. The implementation is mostly correct, but there's one notable concern with error handling.
Findings
1 NOTABLE issue found:
- Medium severity: Silent error suppression when decoding malformed JSON
Details
The change improves resilience to very long lines, but the new error handling silently skips malformed entries without logging or reporting, which could mask corruption issues and lead to silent data loss in the history file.
| for { | ||
| var message string | ||
| if err := json.Unmarshal([]byte(line), &message); err != nil { | ||
| if err := dec.Decode(&message); err != nil { |
There was a problem hiding this comment.
Silent error suppression when decoding malformed JSON entries
When json.NewDecoder.Decode() encounters a non-EOF error (e.g., malformed JSON from file corruption or incomplete writes), the code silently continues with continue, skipping that line but proceeding to decode subsequent lines.
This differs from the previous bufio.Scanner behavior where scanner.Err() would return the error after the loop. While this makes the code more tolerant of corrupt lines, it could mask serious issues like file truncation or encoding problems. A corrupted history file could lead to silently losing valid entries that appear after a corrupt line, with no indication to the user that data was skipped.
Suggestion: Consider logging skipped entries or maintaining an error count:
if err := dec.Decode(&message); err != nil {
if err == io.EOF {
break
}
// Log or track decode errors
slog.Warn("skipping malformed history entry", "error", err)
continue
}Replace bufio.NewScanner with json.NewDecoder for reading history entries. The scanner has a default 64KB line limit which causes failures when loading very long history entries. json.Decoder handles newline-delimited JSON streams naturally without size restrictions, simplifying the code and fixing the long message issue. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
188579b to
bbece1b
Compare
|
@vvoland The review agent seems to be right about the error handling |
Fix cagent crashing and then failing to start with
failed to initialize command history: bufio.Scanner: token too long.Replace bufio.NewScanner with json.NewDecoder for reading history entries.
The scanner has a default 64KB line limit which causes failures when loading very long history entries.
json.Decoder handles newline-delimited JSON streams naturally without size restrictions, simplifying the code and fixing the long message issue.