Browse Source

zlib: introduce pending close state

zlib should not crash in `close()` if the write is still in progress.

fix #7101
v0.10.26-release
Fedor Indutny 11 years ago
parent
commit
829a9b8cba
  1. 15
      src/node_zlib.cc
  2. 38
      test/simple/test-zlib-close-after-write.js

15
src/node_zlib.cc

@ -72,18 +72,25 @@ class ZCtx : public ObjectWrap {
, flush_(0) , flush_(0)
, chunk_size_(0) , chunk_size_(0)
, write_in_progress_(false) , write_in_progress_(false)
, pending_close_(false)
, mode_(mode) , mode_(mode)
{ {
} }
~ZCtx() { ~ZCtx() {
assert(!write_in_progress_ && "write in progress");
Close(); Close();
} }
void Close() { void Close() {
assert(!write_in_progress_ && "write in progress"); if (write_in_progress_) {
pending_close_ = true;
return;
}
pending_close_ = false;
assert(init_done_ && "close before init"); assert(init_done_ && "close before init");
assert(mode_ <= UNZIP); assert(mode_ <= UNZIP);
@ -122,6 +129,7 @@ class ZCtx : public ObjectWrap {
assert(ctx->mode_ != NONE && "already finalized"); assert(ctx->mode_ != NONE && "already finalized");
assert(!ctx->write_in_progress_ && "write already in progress"); assert(!ctx->write_in_progress_ && "write already in progress");
assert(!ctx->pending_close_ && "close is pending");
ctx->write_in_progress_ = true; ctx->write_in_progress_ = true;
ctx->Ref(); ctx->Ref();
@ -279,6 +287,8 @@ class ZCtx : public ObjectWrap {
MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args); MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args);
ctx->Unref(); ctx->Unref();
if (ctx->pending_close_)
ctx->Close();
} }
static void Error(ZCtx *ctx, const char *msg_) { static void Error(ZCtx *ctx, const char *msg_) {
@ -299,6 +309,8 @@ class ZCtx : public ObjectWrap {
// no hope of rescue. // no hope of rescue.
ctx->write_in_progress_ = false; ctx->write_in_progress_ = false;
ctx->Unref(); ctx->Unref();
if (ctx->pending_close_)
ctx->Close();
} }
static Handle<Value> New(const Arguments& args) { static Handle<Value> New(const Arguments& args) {
@ -495,6 +507,7 @@ class ZCtx : public ObjectWrap {
int chunk_size_; int chunk_size_;
bool write_in_progress_; bool write_in_progress_;
bool pending_close_;
uv_work_t work_req_; uv_work_t work_req_;
node_zlib_mode mode_; node_zlib_mode mode_;

38
test/simple/test-zlib-close-after-write.js

@ -0,0 +1,38 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common.js');
var assert = require('assert');
var zlib = require('zlib');
var closed = false;
zlib.gzip('hello', function(err, out) {
var unzip = zlib.createGunzip();
unzip.write(out);
unzip.close(function() {
closed = true;
});
});
process.on('exit', function() {
assert(closed);
});
Loading…
Cancel
Save