Skip to content
Open
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
34 changes: 5 additions & 29 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,6 @@ struct zstream_run_args {
rb_len_t len;
rb_len_t rest_in; /* input after next_in + avail_in, as avail_in is a uInt */
int flush; /* stream flush value for inflate() or deflate() */
int interrupt; /* stop processing the stream and return to ruby */
int jump_state; /* for buffer expansion block break or exception */
int stream_output; /* for streaming zlib processing */
};
Expand Down Expand Up @@ -1042,18 +1041,6 @@ zstream_run_once(void *_arguments)
return (void*)error;
}

/*
* There is no safe way to interrupt z->run->func().
* async-signal-safe
*/
static void
zstream_unblock_func(void *ptr)
{
struct zstream_run_args *args = (struct zstream_run_args *)ptr;

args->interrupt = 1;
}

#ifndef RB_NOGVL_OFFLOAD_SAFE
// Default to no-op if it's not defined:
#define RB_NOGVL_OFFLOAD_SAFE 0
Expand All @@ -1067,10 +1054,12 @@ zstream_run_once_begin(VALUE _arguments)

rb_str_locktmp(z->buf);

/* There is no safe way to interrupt z->run->func(), so there is no unblock
* function. Ruby handles pending interrupts when each call returns. */
#ifndef RB_NOGVL_UBF_ASYNC_SAFE
return (VALUE)rb_thread_call_without_gvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments);
return (VALUE)rb_thread_call_without_gvl(zstream_run_once, (void *)arguments, NULL, NULL);
#else
return (VALUE)rb_nogvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments, RB_NOGVL_UBF_ASYNC_SAFE | RB_NOGVL_OFFLOAD_SAFE);
return (VALUE)rb_nogvl(zstream_run_once, (void *)arguments, NULL, NULL, RB_NOGVL_OFFLOAD_SAFE);
#endif
}

Expand All @@ -1093,7 +1082,7 @@ zstream_run_func(struct zstream_run_args *args)
uInt n;

int err = Z_OK;
while (!args->interrupt) {
while (1) {
n = z->stream.avail_out;

err = (int)(VALUE)rb_ensure(zstream_run_once_begin, (VALUE)args, zstream_run_once_ensure, (VALUE)args);
Expand Down Expand Up @@ -1193,18 +1182,6 @@ zstream_run_try(VALUE value_arg)
loop:
err = zstream_run_func(args);

/* retry if no exception is thrown */
if (err == Z_OK && args->interrupt) {
args->interrupt = 0;

/* Retry only if both avail_in > 0 (more input to process) and avail_out > 0
* (output buffer has space). If avail_out == 0, the buffer is full and should
* be consumed by the caller first. If avail_in == 0, there's nothing more to process. */
if (z->stream.avail_in > 0 && z->stream.avail_out > 0) {
goto loop;
}
}

if (err == Z_BUF_ERROR && z->func == &deflate_funcs && z->stream.avail_in == 0) {
/* deflate() reports Z_BUF_ERROR for a flush when no input was consumed
* since the previous flush of the same or higher rank. Nothing is lost,
Expand Down Expand Up @@ -1263,7 +1240,6 @@ zstream_run(struct zstream *z, Bytef *src, rb_len_t len, int flush)
.len = len,
.rest_in = 0,
.flush = flush,
.interrupt = 0,
.jump_state = 0,
.stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(),
};
Expand Down
31 changes: 31 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,37 @@ def test_close
assert_raise(Zlib::Error) { z << "foo" }
assert_raise(Zlib::Error) { z.reset }
end

# An interrupt must neither raise Zlib::BufError on a deflate call that
# already consumed all its input, nor stop Zlib::Deflate#finish before the
# stream ends.
def test_thread_wakeup_interrupt
pend 'fails' if RUBY_ENGINE == 'truffleruby'

content = SecureRandom.random_bytes(256 * 1024)
outputs = []

thr = Thread.new do
100.times do
z = Zlib::Deflate.new
outputs << (z.deflate(content) << z.finish)
z.close
end
end

while thr.alive?
begin
thr.wakeup
rescue ThreadError
end
Thread.pass
end
thr.join

outputs.each do |output|
assert_equal(content, Zlib::Inflate.inflate(output))
end
end
end

class TestZlibInflate < Test::Unit::TestCase
Expand Down