Skip to content

CSV#shift raises the same InvalidEncodingError forever on File or IO input #398

Backlog

Description

@orien

When File or IO input contains an invalid byte sequence, CSV#shift raises CSV::InvalidEncodingError. Every later call raises the same error again without reading any more input, and CSV#eof? stays false. A loop that rescues CSV::MalformedCSVError to skip bad rows therefore never ends.

require "csv"

File.binwrite("bad.csv", "id,name\n1,bob\xFF\n2,carol\n")

CSV.open("bad.csv") do |csv|
  3.times do
    p csv.shift
  rescue CSV::MalformedCSVError => e
    puts "#{e.class}: #{e.message}"
  end
  p csv.eof?
end

Output with csv 3.3.7 on ruby 4.0.10:

CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2.
CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2.
CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2.
false

The same bytes as a String (CSV.new("id,name\n1,bob\xFF\n2,carol\n")) raise once, and then shift returns nil.

I expected the IO case to end the same way: either raise once and then return nil with eof? true, or skip the bad row and continue. The max_field_size documentation on master says "You can continue to read the next record after the error", so rescue-and-continue looks like a supported pattern.

Cause

  • Parser#parse builds the scanner lazily with @scanner ||= build_scanner (lib/csv/parser.rb:424).
  • For File and IO input, build_scanner starts from @samples, the data read earlier to detect the row separator (lib/csv/parser.rb:886-910). That is the first 32 KB of a file, or 1 KB of other IO.
  • When the sample holds an invalid byte, build_scanner raises before @scanner is assigned, either directly (:899) or through InputsScanner#read_chunk (:316, :325).
  • @samples is never cleared, so the next call builds the scanner from the same data and raises again.

String input ends because @input.read consumes the StringIO, so the next call sees empty input.

A second case, 3.3.6 and later

An invalid byte after the sample, in a file whose quoted fields contain escaped quotes, also loops on 3.3.6, 3.3.7 and master:

require "csv"

data = (1..6000).map { |i| %Q{#{i},"user ""#{i}""",ok\n} }.join.b
data[60_000] = "\xFF".b
File.binwrite("bad2.csv", data)

CSV.open("bad2.csv") do |csv|
  rows = errors = 0
  10_000.times do
    break unless csv.shift
    rows += 1
  rescue CSV::MalformedCSVError => e
    errors += 1
    puts "#{e.class}: #{e.message}" if errors <= 3
  end
  puts "rows=#{rows} errors=#{errors} eof?=#{csv.eof?}"
end
CSV::InvalidEncodingError: Invalid byte sequence in UTF-8 in line 2592.
CSV::MalformedCSVError: TODO: Meaningful message in line 2593.
CSV::MalformedCSVError: TODO: Meaningful message in line 2594.
rows=2591 errors=7409 eof?=false

After the first InvalidEncodingError, each call raises "TODO: Meaningful message", and the line number goes up each time without any input being read. Twelve releases from 3.1.2 to 3.3.5 raise once and then finish the file. I haven't found the cause, but 3.3.6 includes c94763a ("Don't use enumerator in CSV"), which changed how shift re-enters the parser, so I'd look there first.

Versions

Environment: ruby 4.0.10, arm64 macOS.

Activity

  1. changed the title [-]CSV#shift raises the same InvalidEncodingError forever on File or IO input[/-] [+]`CSV#shift` raises the same InvalidEncodingError forever on File or IO input[/+] on Nov 29, 2026
  2. changed the title [-]`CSV#shift` raises the same InvalidEncodingError forever on File or IO input[/-] [+]`CSV#shift` raises the same `InvalidEncodingError` forever on File or IO input[/+] on Nov 29, 2026
  3. added 11 commits that reference this issue on Nov 29, 2026
    6202919
    c7fabc9
    f20f01e
    2f242ad
    76eecb8
    dd1b1d1
    3c21c19
    6c3f9b8
    2d4cb5d
    bbeab91
    c92d09b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions