Skip to content
Permalink
Browse files
Never block for read(0), return blank string. Fixes #1637.
  • Loading branch information
headius committed Dec 22, 2014
1 parent 7dee6bf commit 287cb36d78cc53189342e0a3c8a005d264532f3a
Showing with 23 additions and 0 deletions.
  1. +4 −0 core/src/main/java/org/jruby/RubyIO.java
  2. +19 −0 test/test_socket.rb
@@ -3136,6 +3136,8 @@ private IRubyObject readNotAll(ThreadContext context, OpenFile myOpenFile, int l
Ruby runtime = context.runtime;
str.empty();

if (length == 0) return str;

try {
ByteList newBuffer = readNotAllCommon(context, myOpenFile, length);

@@ -3161,6 +3163,8 @@ private IRubyObject readNotAll(ThreadContext context, OpenFile myOpenFile, int l
private IRubyObject readNotAll(ThreadContext context, OpenFile myOpenFile, int length) {
Ruby runtime = context.runtime;

if (length == 0) return RubyString.newEmptyString(runtime);

try {
ByteList newBuffer = readNotAllCommon(context, myOpenFile, length);

@@ -3,6 +3,7 @@
require 'thread'
require 'test/test_helper'
require 'ipaddr'
require 'timeout'

WINDOWS = RbConfig::CONFIG['host_os'] =~ /Windows|mswin/

@@ -512,5 +513,23 @@ def test_syswrite_raises_epipe
assert Errno::EPIPE === ex
end
end

# jruby/jruby#1637
def test_read_zero_never_blocks
assert_nothing_raised do
server = TCPServer.new(nil, 12345)
t = Thread.new do
s = server.accept
end
client = TCPSocket.new(nil, 12345)
Timeout.timeout(1) do
assert_equal "", client.read(0)
end
t.join
end
ensure
server.close rescue nil
client.close rescue nil
end
end

0 comments on commit 287cb36

Please sign in to comment.