From a2922cd5e9bc8744dfdb0fdc22c0a69d8495a3c7 Mon Sep 17 00:00:00 2001 From: Bruno Costanzo Date: Tue, 3 Nov 2026 15:40:39 +0100 Subject: [PATCH 1/2] Store errors whose message is not valid UTF-8 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. --- app/models/solid_queue/failed_execution.rb | 10 ++++++++-- test/models/solid_queue/failed_execution_test.rb | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/models/solid_queue/failed_execution.rb b/app/models/solid_queue/failed_execution.rb index 1fe2b1a50..e34c80f0e 100644 --- a/app/models/solid_queue/failed_execution.rb +++ b/app/models/solid_queue/failed_execution.rb @@ -49,11 +49,17 @@ def exception_class_name end def exception_message - exception.message + valid_utf8(exception.message) end def exception_backtrace - exception.backtrace + exception.backtrace&.map { |line| valid_utf8(line) } + end + + # The error column is JSON, which can't encode binary strings or invalid UTF-8, so an error + # built from an HTTP body could never be stored, and its job would stay claimed + def valid_utf8(string) + string.dup.force_encoding(Encoding::UTF_8).scrub end # Columns with a size limit, like MySQL's TEXT, reject (or truncate into invalid JSON) diff --git a/test/models/solid_queue/failed_execution_test.rb b/test/models/solid_queue/failed_execution_test.rb index e6e3480fb..9146126b6 100644 --- a/test/models/solid_queue/failed_execution_test.rb +++ b/test/models/solid_queue/failed_execution_test.rb @@ -85,6 +85,20 @@ class SolidQueue::FailedExecutionTest < ActiveSupport::TestCase end end + test "store an error whose message is a binary string" do + # Net::HTTP returns bodies as ASCII-8BIT, so an error built from one is binary + execution = fail_job_with ExpectedTestError.new("upstream said: Zürich".b), backtrace: [ "app/models/thing.rb:1:in 'do_it'" ] + + assert_equal "upstream said: Zürich", execution.message + end + + test "store an error whose message is not valid UTF-8" do + execution = fail_job_with ExpectedTestError.new("upstream said: \xFF\xFE"), backtrace: [ "app/models/thing.rb:1:in 'do_it'".b ] + + assert_equal "upstream said: ��", execution.message + assert_equal [ "app/models/thing.rb:1:in 'do_it'" ], execution.backtrace + end + test "retry failed job" do RaisingJob.perform_later(ExpectedTestError, "A") @worker.start From 0265b832fdb3f4ca1643c197c44cc05c25fb4eaa Mon Sep 17 00:00:00 2001 From: Rosa Gutierrez Date: Wed, 25 Nov 2026 09:35:09 +0100 Subject: [PATCH 2/2] Convert error messages in other encodings instead of relabeling them 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 --- app/models/solid_queue/failed_execution.rb | 11 ++++++++--- test/models/solid_queue/failed_execution_test.rb | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/models/solid_queue/failed_execution.rb b/app/models/solid_queue/failed_execution.rb index e34c80f0e..0e4ca1f6a 100644 --- a/app/models/solid_queue/failed_execution.rb +++ b/app/models/solid_queue/failed_execution.rb @@ -56,10 +56,15 @@ def exception_backtrace exception.backtrace&.map { |line| valid_utf8(line) } end - # The error column is JSON, which can't encode binary strings or invalid UTF-8, so an error - # built from an HTTP body could never be stored, and its job would stay claimed + # The error column is JSON, which can't encode binary strings or invalid UTF-8. Read binary + # strings, like HTTP bodies from Net::HTTP, as UTF-8, convert anything else, and replace what + # still doesn't fit def valid_utf8(string) - string.dup.force_encoding(Encoding::UTF_8).scrub + if string.encoding == Encoding::BINARY + string.dup.force_encoding(Encoding::UTF_8).scrub + else + string.encode(Encoding::UTF_8, invalid: :replace, undef: :replace).scrub + end end # Columns with a size limit, like MySQL's TEXT, reject (or truncate into invalid JSON) diff --git a/test/models/solid_queue/failed_execution_test.rb b/test/models/solid_queue/failed_execution_test.rb index 9146126b6..b22a613a9 100644 --- a/test/models/solid_queue/failed_execution_test.rb +++ b/test/models/solid_queue/failed_execution_test.rb @@ -99,6 +99,12 @@ class SolidQueue::FailedExecutionTest < ActiveSupport::TestCase assert_equal [ "app/models/thing.rb:1:in 'do_it'" ], execution.backtrace end + test "store an error whose message is in another encoding" do + execution = fail_job_with ExpectedTestError.new("upstream said: Zürich".encode(Encoding::ISO_8859_1)), backtrace: [ "app/models/thing.rb:1:in 'do_it'" ] + + assert_equal "upstream said: Zürich", execution.message + end + test "retry failed job" do RaisingJob.perform_later(ExpectedTestError, "A") @worker.start