Skip to content

Commit 287cb36

Browse files
committed
Never block for read(0), return blank string. Fixes #1637.
1 parent 7dee6bf commit 287cb36

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

core/src/main/java/org/jruby/RubyIO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,8 @@ private IRubyObject readNotAll(ThreadContext context, OpenFile myOpenFile, int l
31363136
Ruby runtime = context.runtime;
31373137
str.empty();
31383138

3139+
if (length == 0) return str;
3140+
31393141
try {
31403142
ByteList newBuffer = readNotAllCommon(context, myOpenFile, length);
31413143

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

3166+
if (length == 0) return RubyString.newEmptyString(runtime);
3167+
31643168
try {
31653169
ByteList newBuffer = readNotAllCommon(context, myOpenFile, length);
31663170

test/test_socket.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'thread'
44
require 'test/test_helper'
55
require 'ipaddr'
6+
require 'timeout'
67

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

@@ -512,5 +513,23 @@ def test_syswrite_raises_epipe
512513
assert Errno::EPIPE === ex
513514
end
514515
end
516+
517+
# jruby/jruby#1637
518+
def test_read_zero_never_blocks
519+
assert_nothing_raised do
520+
server = TCPServer.new(nil, 12345)
521+
t = Thread.new do
522+
s = server.accept
523+
end
524+
client = TCPSocket.new(nil, 12345)
525+
Timeout.timeout(1) do
526+
assert_equal "", client.read(0)
527+
end
528+
t.join
529+
end
530+
ensure
531+
server.close rescue nil
532+
client.close rescue nil
533+
end
515534
end
516535

0 commit comments

Comments
 (0)