Skip to content

history: Fix loading very long lines#1940

Open
vvoland wants to merge 1 commit intodocker:mainfrom
vvoland:history-fix-long-lines
Open

history: Fix loading very long lines#1940
vvoland wants to merge 1 commit intodocker:mainfrom
vvoland:history-fix-long-lines

Conversation

@vvoland
Copy link
Contributor

@vvoland vvoland commented Mar 5, 2026

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.

@vvoland vvoland requested a review from a team as a code owner March 5, 2026 12:42
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

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

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 {
Copy link

Choose a reason for hiding this comment

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

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>
@vvoland vvoland force-pushed the history-fix-long-lines branch from 188579b to bbece1b Compare March 5, 2026 15:13
@dgageot
Copy link
Member

dgageot commented Mar 6, 2026

@vvoland The review agent seems to be right about the error handling

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.

2 participants