Skip to content

Store errors whose message is not valid UTF-8 - #832

Open
bruno-costanzo wants to merge 2 commits into
rails:mainfrom
bruno-costanzo:store-errors-that-are-not-valid-utf8
Open

bruno-costanzo wants to merge 2 commits into
rails:mainfrom
bruno-costanzo:store-errors-that-are-not-valid-utf8

Conversation

@bruno-costanzo

Copy link
Copy Markdown

The error column of solid_queue_failed_executions is serialized with JSON, which can't encode a binary (ASCII-8BIT) string with non-ASCII bytes, or a string that is not valid UTF-8. An error message built from an HTTP body hits the first case, because Net::HTTP returns bodies as ASCII-8BIT:

class SyncJob < ApplicationJob
  def perform
    response = Net::HTTP.get_response(uri)
    raise ApiError, "upstream said: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
  end
end
# body "Zürich" → message is ASCII-8BIT → JSON::GeneratorError: "\xC3" from ASCII-8BIT to UTF-8

Saving the failed execution then raises JSON::GeneratorError, and the finalization rolls back:

  • On 1.7.0 the job stays claimed while the worker lives. It's not in the failed jobs, and it can't be discarded ("Can't discard a job in progress"). A worker restart releases it, and it runs again.
  • On main the error becomes a FinalizationError, which is unrecoverable, so the worker shuts down. The supervisor starts a new one, the claim is released, the job runs again, fails the same way, and the worker shuts down again. The job's side effects repeat on every round.

This converts the message and each backtrace line to valid UTF-8 before storing them (force_encoding + scrub): valid bytes keep their characters ("Zürich" stays "Zürich") and invalid bytes become U+FFFD. The job then fails like any other.

The two new tests in failed_execution_test.rb raise JSON::GeneratorError on main and pass with the change. TARGET_DB=sqlite bin/rails test test/models test/unit: 288 runs, 0 failures, 0 errors. RuboCop: no offenses.

bruno-costanzo and others added 2 commits November 24, 2026 16:47
The error column is JSON, which can't encode a binary string or invalid
UTF-8. An error built from an HTTP body (Net::HTTP returns ASCII-8BIT)
raised JSON::GeneratorError when the failed execution was saved, so the
job stayed claimed, and since the finalization error is unrecoverable,
the worker stopped and the job ran again on restart.
Forcing the UTF-8 encoding on every message turned a valid message in
another encoding, like Latin-1, into replacement characters, although
JSON could store it converted. Only binary strings are read as UTF-8 now;
anything else is converted, and what still doesn't fit is replaced.

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

rosa commented Nov 25, 2026

Copy link
Copy Markdown
Member

Thanks a lot, @bruno-costanzo, great catch and great write-up! I rebased this onto main and added one commit on top: forcing the UTF-8 encoding on every message turned a message that's valid in another encoding, like Latin-1, into replacement characters, although JSON can store it once converted. So now only binary strings are read as UTF-8, anything else is converted, and what still doesn't fit is replaced. There's a test for the Latin-1 case too.

@rosa
rosa force-pushed the store-errors-that-are-not-valid-utf8 branch from dd19c55 to 0265b83 Compare November 25, 2026 08:39
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