From 406846fcc5193b587d724e0d9a516b581458d499 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 5 Oct 2013 19:48:33 +0200 Subject: [PATCH] zlib: fix write request reference counting Keep track of the reference count, don't make the wrapper object weak when there are pending write requests. Fixes a regression from c79d516. --- src/node_zlib.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/node_zlib.cc b/src/node_zlib.cc index de5f1d5c96..2d411ab29a 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -84,7 +84,8 @@ class ZCtx : public WeakObject { , mode_(mode) , strategy_(0) , windowBits_(0) - , write_in_progress_(false) { + , write_in_progress_(false) + , refs_(0) { } @@ -137,7 +138,7 @@ class ZCtx : public WeakObject { assert(!ctx->write_in_progress_ && "write already in progress"); ctx->write_in_progress_ = true; - ctx->ClearWeak(); + ctx->Ref(); assert(!args[0]->IsUndefined() && "must provide flush value"); @@ -291,7 +292,7 @@ class ZCtx : public WeakObject { Local args[2] = { avail_in, avail_out }; MakeCallback(env, handle, env->callback_string(), ARRAY_SIZE(args), args); - ctx->MakeWeak(); + ctx->Unref(); } static void Error(ZCtx* ctx, const char* message) { @@ -314,7 +315,7 @@ class ZCtx : public WeakObject { // no hope of rescue. ctx->write_in_progress_ = false; - ctx->MakeWeak(); + ctx->Unref(); } static void New(const FunctionCallbackInfo& args) { @@ -517,6 +518,19 @@ class ZCtx : public WeakObject { } private: + void Ref() { + if (++refs_ == 1) { + ClearWeak(); + } + } + + void Unref() { + assert(refs_ > 0); + if (--refs_ == 0) { + MakeWeak(); + } + } + static const int kDeflateContextSize = 16384; // approximate static const int kInflateContextSize = 10240; // approximate @@ -535,6 +549,7 @@ class ZCtx : public WeakObject { int windowBits_; uv_work_t work_req_; bool write_in_progress_; + unsigned int refs_; };