Skip to content

Commit

Permalink
Never block for read(0), return blank string. Fixes #1637.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Dec 22, 2014
1 parent 7dee6bf commit 287cb36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions test/test_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'thread'
require 'test/test_helper'
require 'ipaddr'
require 'timeout'

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

Expand Down Expand Up @@ -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.