Skip to content

Raise InProgressError from other methods while the stream is running - #207

Merged
mame merged 2 commits into
ruby:masterfrom
mame:zlib-in-progress-checks
Dec 2, 2026
Merged

mame merged 2 commits into
ruby:masterfrom
mame:zlib-in-progress-checks

Conversation

@mame

@mame mame commented Dec 1, 2026

Copy link
Copy Markdown
Member

Only #inflate, #deflate and the methods that run the stream checked ZSTREAM_IN_PROGRESS. The others, such as #close, #reset, #flush_next_out, #avail_out=, Deflate#params and the GzipFile methods, could change the zlib state or the output buffer while another thread was running zlib without the GVL, or from the block of #inflate/#deflate.

zd = Zlib::Deflate.new
Thread.new { sleep 0.1; zd.close }
zd.deflate(Random.bytes(50_000_000), Zlib::FINISH)
#=> [BUG] Segmentation fault at 0x0000000000000000

This adds the same check to them, as #168 did for Inflate#sync.

mame and others added 2 commits December 2, 2026 01:01
Only #inflate, #deflate and the methods that run the stream checked
ZSTREAM_IN_PROGRESS.  The others could change the zlib state or the
output buffer while another thread was running zlib without the GVL,
or from the block of #inflate/#deflate.

  zd = Zlib::Deflate.new
  Thread.new { sleep 0.1; zd.close }
  zd.deflate(Random.bytes(50_000_000), Zlib::FINISH)
  #=> [BUG] Segmentation fault at 0x0000000000000000

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GzipFile methods did not check ZSTREAM_IN_PROGRESS either, so another
thread could close the stream or replace its buffer while zlib was
running without the GVL.

  gz = Zlib::GzipWriter.new(StringIO.new)
  q = Thread::Queue.new
  Thread.new { q.pop; gz.close }
  gz.write("x")
  q << true
  gz.write(Random.bytes(10_000_000))
  #=> can't modify frozen NilClass: nil (FrozenError)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame force-pushed the zlib-in-progress-checks branch from 5e9de79 to 4daca17 Compare December 2, 2026 02:52
@mame
mame merged commit 93ae60a into ruby:master Dec 2, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant