Skip to content

Commit

Permalink
Fix encoding error in sqlite
Browse files Browse the repository at this point in the history
* When `Socket.gethostname` returns a hostname with a special character like apostrophe in `Basecamp’s-Computer`, an encoding error is raised `Encoding::UndefinedConversionError: "\xE2" from ASCII-8BIT to UTF-8`

* By forcing a utf-8 encoding, the error goes away and the process row properly inserts.

* A similar issue occurred in the newrelic ruby gem, and required the same solution newrelic/newrelic-ruby-agent@e1f65a8
  • Loading branch information
jpcamara authored and rosa committed Feb 7, 2024
1 parent bf6bcf2 commit 3413d29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/solid_queue/processes/registrable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def kind
end

def hostname
@hostname ||= Socket.gethostname
@hostname ||= Socket.gethostname.force_encoding(Encoding::UTF_8)
end

def process_pid
Expand Down
12 changes: 12 additions & 0 deletions test/models/solid_queue/process_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "test_helper"
require "minitest/mock"

class SolidQueue::ProcessTest < ActiveSupport::TestCase
test "prune processes with expired heartbeats" do
Expand All @@ -13,4 +14,15 @@ class SolidQueue::ProcessTest < ActiveSupport::TestCase
SolidQueue::Process.prune
end
end

test "hostname's with special characters are properly loaded" do
worker = SolidQueue::Worker.new(queues: "*", threads: 3, polling_interval: 0.2)
hostname = "Basecamp’s-Computer"

Socket.stub :gethostname, hostname.force_encoding("ASCII-8BIT") do
worker.start
wait_for_registered_processes(1, timeout: 1.second)
assert_equal hostname, SolidQueue::Process.last.hostname
end
end
end

0 comments on commit 3413d29

Please sign in to comment.