Skip to content

fix: surface PGN read errors instead of looping forever - #132

Merged
CorentinGS merged 2 commits into
v3from
fix/v3-framer-read-error
Oct 25, 2026
Merged

CorentinGS merged 2 commits into
v3from
fix/v3-framer-read-error

Conversation

@CorentinGS

Copy link
Copy Markdown
Owner

Problem

pgnFramer.next() called readMore() again after a read failure whenever the buffer still held bytes. A reader that keeps failing — a dropped connection, for instance — therefore made the decoder spin forever instead of reporting the error:

for {
    game, err := dec.Decode()   // never returns
}

Reproduced against v3 with a reader that returns one game and then a persistent error; the call never returned (killed after 300s).

Fix

The bytes left in the buffer after a failed read are an incomplete record, so the read error is now returned to the caller instead of reading again. A record that was fully read before the failure is still returned first.

for {
    advance, token, err := splitPGNGames(f.buffer, f.eof)
    ...
    if advance == 0 && token == nil {
        if f.readErr != nil {
            // The reader failed and the buffered bytes do not form a
            // complete record: report the error instead of reading
            // again, which would spin forever on a failing reader.
            return PGNRecord{}, f.readErr
        }
        ...

readMore no longer writes f.readErr itself; the framer owns that state.

Tests

pgn_read_error_test.go (new):

  • TestPGNDecoderReportsReadError — the decoder returns the reader's error. Fails on v3 (times out), passes here.
  • TestPGNGamesReportsReadError — the iterator surfaces the error too.
  • TestPGNDecoderReturnsCompleteRecordBeforeReadError — a record fully read before the failure is still returned; the error surfaces on the next call.
  • TestPGNDecoderCleanEndStillReturnsEOF — a clean end still reports io.EOF.

go test ./... passes. golangci-lint run --new-from-rev=origin/v3 reports 0 issues.

Note

This is the v3 counterpart of #129, not a cherry-pick of it: v3 has no Scanner (it was replaced by PGNDecoder / PGNGames / PGNRecords), so #129's Scanner.LastError() has no target here. The underlying need — a read error reaching the user — is already served in v3 by returning errors in-band, except for the hang fixed here.

Related: #129 is what surfaced the v2 half of this problem — bufio.Scanner's default 64 KiB token limit rejects the longest possible chess game outright (bufio.Scanner: token too long), so Scanner.LastError() reports a failure the caller can do nothing about. On v2 that needs a token-limit fix, not just visibility.

Corentin GS and others added 2 commits October 25, 2026 10:34
The PGN framer called readMore again after a read failure whenever the
buffer still held bytes, so a reader that kept failing (a dropped
connection, for instance) made the decoder spin instead of reporting the
error. The buffered bytes of a failed read are an incomplete record, so
the read error is now returned to the caller, and any complete record
already buffered is returned first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.53%. Comparing base (ff4e815) to head (5a976ec).

Additional details and impacted files
@@            Coverage Diff             @@
##               v3     #132      +/-   ##
==========================================
+ Coverage   85.38%   85.53%   +0.14%     
==========================================
  Files          46       46              
  Lines        6399     6395       -4     
==========================================
+ Hits         5464     5470       +6     
+ Misses        686      680       -6     
+ Partials      249      245       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CorentinGS
CorentinGS merged commit 0996e10 into v3 Oct 25, 2026
8 checks passed
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.

1 participant