diff --git a/src/node_zlib.cc b/src/node_zlib.cc index d522676284..5c8578d86e 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -72,18 +72,25 @@ class ZCtx : public ObjectWrap { , flush_(0) , chunk_size_(0) , write_in_progress_(false) + , pending_close_(false) , mode_(mode) { } ~ZCtx() { + assert(!write_in_progress_ && "write in progress"); 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(mode_ <= UNZIP); @@ -122,6 +129,7 @@ class ZCtx : public ObjectWrap { assert(ctx->mode_ != NONE && "already finalized"); assert(!ctx->write_in_progress_ && "write already in progress"); + assert(!ctx->pending_close_ && "close is pending"); ctx->write_in_progress_ = true; ctx->Ref(); @@ -279,6 +287,8 @@ class ZCtx : public ObjectWrap { MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args); ctx->Unref(); + if (ctx->pending_close_) + ctx->Close(); } static void Error(ZCtx *ctx, const char *msg_) { @@ -299,6 +309,8 @@ class ZCtx : public ObjectWrap { // no hope of rescue. ctx->write_in_progress_ = false; ctx->Unref(); + if (ctx->pending_close_) + ctx->Close(); } static Handle New(const Arguments& args) { @@ -495,6 +507,7 @@ class ZCtx : public ObjectWrap { int chunk_size_; bool write_in_progress_; + bool pending_close_; uv_work_t work_req_; node_zlib_mode mode_; diff --git a/test/simple/test-zlib-close-after-write.js b/test/simple/test-zlib-close-after-write.js new file mode 100644 index 0000000000..f0f176008f --- /dev/null +++ b/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); +});