Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix streaming to file descriptor (#943) #944

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions ext/oj/stream_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ static void stream_writer_reset_buf(StreamWriter sw) {
}

static void stream_writer_write(StreamWriter sw) {
ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
const char* buf = sw->sw.out.buf;

switch (sw->type) {
case STRING_IO:
case STREAM_IO: {
volatile VALUE rs = rb_str_new(sw->sw.out.buf, size);
volatile VALUE rs = rb_str_new(buf, sw->sw.out.cur - buf);

// Oddly enough, when pushing ASCII characters with UTF-8 encoding or
// even ASCII-8BIT does not change the output encoding. Pushing any
Expand All @@ -54,8 +54,17 @@ static void stream_writer_write(StreamWriter sw) {
break;
}
case FILE_IO:
if (size != write(sw->fd, sw->sw.out.buf, size)) {
rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
while (buf < sw->sw.out.cur) {
ssize_t num_written = write(sw->fd, buf, sw->sw.out.cur - buf);
if (num_written == -1) {
if (errno == EAGAIN) {
continue;
Comment on lines +60 to +61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem quite right yet - should we poll(2) here? Also I think we need to handle EWOULDBLOCK and EINTR for full compatibility. Is this related to #723 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also potential issues around the GVL here - on second thought I think maybe better to simply remove the special case for FILE_IO altogether.

} else {
rb_raise(rb_eIOError, "Write failed. [_%d_:%s]\n", errno, strerror(errno));
break;
}
}
buf += num_written;
}
break;
default: rb_raise(rb_eArgError, "expected an IO Object.");
Expand Down
14 changes: 14 additions & 0 deletions test/test_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$LOAD_PATH << __dir__

require 'helper'
require 'open3'

class OjWriter < Minitest::Test

Expand Down Expand Up @@ -377,4 +378,17 @@ def test_stream_writer_push_null_value_with_key
w.pop()
assert_equal(%|{"nothing":null}\n|, output.string())
end

def test_stream_writer_subprocess
Open3.popen3("/bin/bash", "-c", "cat > /dev/null") do |stdin, _stdout, _stderr, _wait_thr|
w = Oj::StreamWriter.new(stdin, :indent => 0)
w.push_array()
chunk = "{\"foo\":\"#{"bar"*1000}\"}"
1000.times do |_|
w.push_json(chunk)
end
w.pop()
stdin.close
end
end
end # OjWriter