Skip to content

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

Closed

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.

Activity

  1. byroot commented on Mar 8, 2026

    @byroot
    Member

    Thank you for the repro, I actually get a SEGV on Linux ARM64. Git bisect points to fff25c9

  2. dalibor commented on Mar 8, 2026

    @dalibor
    Author

    Thank you for looking into it! I haven't tested on Linux ARM64, but on Linux x86_64 it shows up consistently in more resource-constrained environments even with smaller payloads. It took us some time until we isolated it since in production it was showing up intermittently. Thanks!

  3. byroot commented on Mar 8, 2026

    @byroot
    Member

    The SEGV was likely a red herring, I cleaned up the compilation cache and it no longer repro.

    I might need to find an x86 machine because I can't effectively debug with docker. I might try virtual box or something like that.

  4. byroot commented on Mar 8, 2026

    @byroot
    Member

    The SEGV was likely a red herring,

    I need to stop posting before finishing my coffee, not a red herring, I know what's going on and have a fix. Can't say for sure it's the same bug though.

  5. byroot commented on Mar 8, 2026

    @byroot
    Member

    Would you mind trying this PR: #952 ?

  6. dalibor commented on Mar 8, 2026

    @dalibor
    Author

    I just tested with 2.18.0 and good news is that I cannot reproduce it on Linux x86_64. So, most likely related to the commit you pointed out!

  7. dalibor commented on Mar 8, 2026

    @dalibor
    Author

    Sure, will test now with PR: #952

  8. dalibor commented on Mar 8, 2026

    @dalibor
    Author

    I can confirm PR: #952 fixes it. I cannot reproduce it anymore on Linux x86_64. Thank you!

  9. byroot commented on Mar 8, 2026

    @byroot
    Member

    Alright, 2.19.1 is out with the fix. THanks again for the repro script and for trying the fix, saved me a lot of time.

  10. dalibor commented on Mar 8, 2026

    @dalibor
    Author

    Thank you for your time and contributions!

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