Skip to content

Concurrent PTY.spawn crashes the JVM in libfixposix #9977

Open

Description

@sampokuokkanen

Calling PTY.spawn from a few threads at once crashes the JVM with a SIGBUS inside libfixposix. It also happens with a single thread, just less often.

require "pty"

threads = Integer(ARGV[0] || 4)
iterations = Integer(ARGV[1] || 50)

threads.times.map {
  Thread.new {
    iterations.times {
      r, w, pid = PTY.spawn("true")
      Process.waitpid(pid)
      r.close
      w.close
    }
  }
}.each(&:join)
puts "ok"

jruby pty_crash.rb 4 50 crashes on most runs on macOS arm64 (JRuby master, Java 21):

#  SIGBUS (0xa) at pc=0x000000018cdd1050
# C  [libsystem_platform.dylib+0x3050]  __bzero+0x20
C  [libfixposix.dylib+0x69d8]
...
j  org.jruby.ext.ffi.jffi.DefaultMethodFactory$Signed32Invoker.invoke(...)

The cause is in the bundled ffi-bindings-libfixposix 0.5.1.0. It allocates 32 bytes for lfp_spawn_file_actions_t, but the real struct is 40 bytes on 64-bit platforms. lfp_spawn_file_actions_init writes 8 bytes past the end of the buffer and corrupts the next FFI allocation. The next addclose/adddup2 call then reads a broken struct and crashes.

With SIZEOF = 40 in lib/ruby/stdlib/libfixposix/ffi.rb, the script above passed 15 of 15 runs with 1, 4 and 8 threads. Before the change, about 11 of 15 runs crashed.

Upstream fix: byteit101/subspawn#4. Once a new gem is released, we need to bump ffi-bindings-libfixposix in lib/pom.rb.

Activity

  1. sampokuokkanen commented on Nov 2, 2026

    @sampokuokkanen
    ContributorAuthor

    There's a second PTY bug in the same gem that shows up alongside this one. PTY.spawn returns the same IO as both reader and writer, so closing the writer closes the reader too:

    require "pty"
    
    r, w, pid = PTY.spawn("echo", "hi")
    p r.equal?(w) # true on JRuby, false on MRI
    w.close
    p r.closed?   # true on JRuby, false on MRI

    This breaks Rails' bin/ci runner, which closes the writer and then reads the output. It also runs groups in parallel, so it hits the crash above as well.

    Fix: byteit101/subspawn#5.

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