From 6e16ad7a63ba843aafa1284e51ce220a775e11d8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 30 Jun 2018 14:58:19 +0200 Subject: [PATCH] zlib: fix memory leak for unused zlib instances MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An oversight in an earlier commit led to a memory leak in the untypical situation that zlib instances are created but never used, because zlib handles no longer started out their life as weak handles. The bug was introduced in bd201102862a194f3f5ce669e0a3c8143eafc900. Refs: https://github.com/nodejs/node/pull/20455 PR-URL: https://github.com/nodejs/node/pull/21607 Reviewed-By: Tobias Nießen --- src/node_zlib.cc | 1 + test/parallel/test-zlib-unused-weak.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/parallel/test-zlib-unused-weak.js diff --git a/src/node_zlib.cc b/src/node_zlib.cc index de8b0335892f65..8e30241f4e6d45 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -90,6 +90,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { refs_(0), gzip_id_bytes_read_(0), write_result_(nullptr) { + MakeWeak(); } diff --git a/test/parallel/test-zlib-unused-weak.js b/test/parallel/test-zlib-unused-weak.js new file mode 100644 index 00000000000000..7a5a6728533e18 --- /dev/null +++ b/test/parallel/test-zlib-unused-weak.js @@ -0,0 +1,18 @@ +'use strict'; +// Flags: --expose-gc +require('../common'); +const assert = require('assert'); +const zlib = require('zlib'); + +// Tests that native zlib handles start out their life as weak handles. + +const before = process.memoryUsage().external; +for (let i = 0; i < 100; ++i) + zlib.createGzip(); +const afterCreation = process.memoryUsage().external; +global.gc(); +const afterGC = process.memoryUsage().external; + +assert((afterGC - before) / (afterCreation - before) <= 0.05, + `Expected after-GC delta ${afterGC - before} to be less than 5 %` + + ` of before-GC delta ${afterCreation - before}`);