Skip to content

JSON.dump returns NULL bytes for large HashWithIndifferentAccess objects (Linux x86_64) #951

@dalibor

Description

@dalibor

JSON.dump() intermittently returns all NULL bytes when serializing large ActiveSupport::HashWithIndifferentAccess objects on Linux x86_64. The same code works correctly on macOS ARM with identical Ruby/gem versions.

Details

  • Platform: x86_64-linux
  • Ruby: 4.0.1
  • JSON: 2.19.0
  • ActiveSupport: 8.1.2
  • CPU: .25 vCPU
  • Memory: 1 GiB

I wasn't able to reproduce it on arm64-darwin25.

Reproduction (x86_64-linux only)

Try multiple serialization attempts with larger payloads as it's an intermittent bug, though I'm able to reproduce it consistently.

require "bundler/inline"

gemfile do
  gem "activesupport", "8.1.2"
  gem "json", "2.19.0"
end

require 'active_support/core_ext/hash'

large_string = "x" * (35 * 1024 * 1024); nil
data = { "content" => large_string }.with_indifferent_access; nil

100.times do |i|
  json_data = JSON.dump(data)

  if json_data.getbyte(0) == 0
    puts "BUG: JSON.dump returned NULL bytes on attempt #{i}"
    puts "Size: #{json_data.bytesize} bytes"
    break
  end
end

The workaround is to convert to plain Hash before serialization using .to_h.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions