Skip to content

Socket.send freezes string unexpectedly (RuntimeError: can't modify frozen string) #3249

@boazsegev

Description

@boazsegev

The issue
Opening a TCPSocket and using the send method, freezes the string being sent on JRuby 9000.

This is unexpected and isn't experienced in MRI.

This issue doesn't occur when using socket.write (blocks until done, inherited from the IO class).

How to replicate

  1. Open a TCPSocket object.
  2. Create a long string (so string is a pointer).
  3. Send string using socket.send data, 0
  4. Try to modify string.

Example code

It's possible to replicate the error using the following code:

require 'socket'
Thread.new do
    begin
        server = TCPServer.new 3000
        while sock = server.accept
            sleep 10
            sock.close
        end
    rescue => e
        server.close
    end
end

long_string = "
JRuby is an implementation of the Ruby language using the JVM.

It aims to be a complete, correct and fast implementation of Ruby, at the same time as providing powerful new features such as concurrency without a global-interpreter-lock, true parallelism, and tight integration to the Java language to allow you to uses Java classes in your Ruby program and to allow JRuby to be embedded into a Java application.

You can use JRuby simply as a faster version of Ruby, you can use it to run Ruby on the JVM and access powerful JVM libraries such as highly tuned concurrency primitives, you can use it to embed Ruby as a scripting language in your Java program, or many other possibilites.
"

20.times do |i|
    begin
        s = TCPSocket.new 'localhost', 3000
        str = long_string.dup
        s.send str, 0
        str.clear
        # => JRUBY: RuntimeError: can't modify frozen string
        # => MRI: ""
    rescue => e
        puts "Test \##{i} raised an exception: #{e.message}"
    ensure
        s.close
    end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions