From 64ec307e69fb2c46825996e417e1777015511670 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Sun, 25 Oct 2026 07:59:18 +0900 Subject: [PATCH 1/3] Do not unlock an output buffer locked by another thread zstream_run_func took the temporary lock on the output buffer inside rb_ensure, so when the buffer given as `buffer:` was already locked, for example by `IO#read` in another thread, the failed lock still ran the ensure and released that thread's lock. Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 6 +++--- test/zlib/test_zlib.rb | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index f13ef4d..8d3c4d5 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -1060,9 +1060,6 @@ static VALUE zstream_run_once_begin(VALUE _arguments) { struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments; - struct zstream *z = arguments->z; - - rb_str_locktmp(z->buf); #ifndef RB_NOGVL_UBF_ASYNC_SAFE return (VALUE)rb_thread_call_without_gvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments); @@ -1093,6 +1090,9 @@ zstream_run_func(struct zstream_run_args *args) while (!args->interrupt) { n = z->stream.avail_out; + /* lock outside rb_ensure, so that a lock held by someone else is not + released when rb_str_locktmp fails */ + rb_str_locktmp(z->buf); err = (int)(VALUE)rb_ensure(zstream_run_once_begin, (VALUE)args, zstream_run_once_ensure, (VALUE)args); rb_str_set_len(z->buf, ZSTREAM_BUF_FILLED(z) + (n - z->stream.avail_out)); diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index e1ecbf8..185c380 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -572,6 +572,28 @@ def test_inflate_buffer_modified_while_handling_need_dict assert_equal "A" * 12_000, z.inflate("", buffer: buf) end + def test_inflate_buffer_locked_by_another_thread + dict = "dictionary-" * 4 + d = Zlib::Deflate.new + d.set_dictionary(dict) + deflated = d.deflate("A" * 12_000, Zlib::FINISH) + + # The buffer is kept across NeedDict, so the retry locks it without + # modifying it first. + z = Zlib::Inflate.new + buf = String.new(capacity: 1 << 14) + assert_raise(Zlib::NeedDict) { z.inflate(deflated, buffer: buf) } + z.set_dictionary(dict) + + IO.pipe do |r, w| + reader = Thread.new { r.read(3, buf) } + Thread.pass until reader.stop? + assert_raise(RuntimeError) { z.inflate("", buffer: buf) } + w.write("foo") + assert_equal "foo", reader.value + end + end + def test_inflate_chunked_break # zeros = Zlib::Deflate.deflate("0" * 100_000) zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \ From a62f34a5dd1670fbba743ed37f3969788f993d25 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Sun, 25 Oct 2026 09:34:58 +0900 Subject: [PATCH 2/3] Keep the pending input when the output buffer cannot be locked A failure of `rb_str_locktmp` escaped `zstream_run_try` before the unconsumed input was put back into the stream, so a retry after the error ended in `Zlib::BufError`. Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 10 ++++++++-- test/zlib/test_zlib.rb | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 8d3c4d5..afee432 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -1091,8 +1091,14 @@ zstream_run_func(struct zstream_run_args *args) n = z->stream.avail_out; /* lock outside rb_ensure, so that a lock held by someone else is not - released when rb_str_locktmp fails */ - rb_str_locktmp(z->buf); + released when rb_str_locktmp fails, and raise the failure after + zstream_run_try has put back the pending input */ + rb_protect(rb_str_locktmp, z->buf, &state); + if (state) { + err = Z_OK; + args->jump_state = state; + break; + } err = (int)(VALUE)rb_ensure(zstream_run_once_begin, (VALUE)args, zstream_run_once_ensure, (VALUE)args); rb_str_set_len(z->buf, ZSTREAM_BUF_FILLED(z) + (n - z->stream.avail_out)); diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 185c380..44233a2 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -592,6 +592,8 @@ def test_inflate_buffer_locked_by_another_thread w.write("foo") assert_equal "foo", reader.value end + buf.clear + assert_equal "A" * 12_000, z.inflate("", buffer: buf) end def test_inflate_chunked_break From 79068f54f5dbaea4ecda56ebcca3e669d8c7ed9a Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 10 Nov 2026 18:29:15 +0900 Subject: [PATCH 3/3] Make the locked buffer test reach the lock on older Rubies `IO#read` up to Ruby 3.1 resizes a buffer shorter than the read, so `Zlib::Inflate#inflate` failed on the resize before reaching the lock. TruffleRuby's `IO#read` does not lock the buffer at all. Co-Authored-By: Claude Opus 5.5 --- test/zlib/test_zlib.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 48de93e..470dbf0 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -624,17 +624,22 @@ def test_inflate_buffer_modified_while_handling_need_dict end def test_inflate_buffer_locked_by_another_thread + omit 'IO#read does not lock the buffer' if RUBY_ENGINE == 'truffleruby' + dict = "dictionary-" * 4 d = Zlib::Deflate.new d.set_dictionary(dict) deflated = d.deflate("A" * 12_000, Zlib::FINISH) # The buffer is kept across NeedDict, so the retry locks it without - # modifying it first. + # modifying it first. It is long enough that the first call keeps its + # capacity, and it gets a few bytes so that IO#read up to Ruby 3.1 + # does not resize it. z = Zlib::Inflate.new - buf = String.new(capacity: 1 << 14) + buf = "\0".b * (1 << 15) assert_raise(Zlib::NeedDict) { z.inflate(deflated, buffer: buf) } z.set_dictionary(dict) + buf << "...." IO.pipe do |r, w| reader = Thread.new { r.read(3, buf) }