From 5538219de32101f4d7f7c290c8ee728d6385cb34 Mon Sep 17 00:00:00 2001 From: Bruno Costanzo Date: Mon, 12 Oct 2026 09:33:56 +0200 Subject: [PATCH 1/2] Do not stop the zstream loop for an interrupt Each deflate() and inflate() call runs in its own rb_nogvl since #88, and rb_nogvl handles pending interrupts when the call returns. So the loop does not need to stop for an interrupt and resume. The resume check from #121 skipped the resume when avail_in == 0. Under Z_FINISH, deflate can still have pending output then, so Zlib::Deflate#finish could return before Z_STREAM_END and drop the end of the stream. --- ext/zlib/zlib.c | 14 +------------- test/zlib/test_zlib.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index f184895..6997ac5 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -1093,7 +1093,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); @@ -1193,18 +1193,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, diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index a3531d3..5f156ef 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -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 From f5cfd3e55abb2c335384b1e7e5d43d5b6c170721 Mon Sep 17 00:00:00 2001 From: Bruno Costanzo Date: Mon, 9 Nov 2026 13:51:11 +0100 Subject: [PATCH 2/2] Remove the unused interrupt flag and unblock function zstream_run_func no longer reads args->interrupt, so the unblock function did nothing useful. Pass NULL instead: Ruby handles pending interrupts when each deflate() or inflate() call returns. --- ext/zlib/zlib.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 6997ac5..b96ad45 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -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 */ }; @@ -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 @@ -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 } @@ -1251,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(), };