From: Fedor Indutny Date: Thu, 13 Feb 2014 13:17:59 +0000 (+0400) Subject: zlib: introduce pending close state X-Git-Tag: v0.10.26~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=829a9b8cba0680f1d451551e12491e7b0e1ea92f;p=platform%2Fupstream%2Fnodejs.git zlib: introduce pending close state zlib should not crash in `close()` if the write is still in progress. fix #7101 --- diff --git a/src/node_zlib.cc b/src/node_zlib.cc index d522676..5c8578d 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 0000000..f0f1760 --- /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); +});