diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 037cd96..bde01d3 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -674,9 +674,7 @@ zstream_expand_buffer(struct zstream *z) rb_obj_reveal(z->buf, rb_cString); } - rb_mutex_unlock(z->mutex); - rb_protect(rb_yield, z->buf, &state); - rb_mutex_lock(z->mutex); + rb_protect(rb_yield, z->buf, &state); if (ZSTREAM_REUSE_BUFFER_P(z)) { rb_str_modify(z->buf); @@ -729,6 +727,7 @@ zstream_expand_buffer_protect(struct zstream *z) return state; } + static int zstream_expand_buffer_non_stream(struct zstream *z) { @@ -1207,7 +1206,11 @@ zstream_run(struct zstream *z, Bytef *src, long len, int flush) .jump_state = 0, .stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(), }; - rb_mutex_lock(z->mutex); + + if (rb_mutex_trylock(z->mutex) == Qfalse) { + rb_raise(cInProgressError, "zlib stream is in progress"); + } + rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args); if (args.jump_state) rb_jump_tag(args.jump_state);