From 1750583a156a1405a3d1bb5e87bbff19ce7b1035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 9 Aug 2016 15:35:23 +0200 Subject: [PATCH] kv-memory: fix crash in regular cleanup Fix bug in "_setupRegularCleanup()" where the interval callback was trying to access an object that has been garbage-collected in the meantime. --- lib/connectors/kv-memory.js | 11 +++++++++-- test/kv-memory.js | 8 +------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/connectors/kv-memory.js b/lib/connectors/kv-memory.js index 8cd92a46..bd4355bb 100644 --- a/lib/connectors/kv-memory.js +++ b/lib/connectors/kv-memory.js @@ -31,8 +31,15 @@ KeyValueMemoryConnector.prototype._setupRegularCleanup = function() { // key expiration too, the scheduled cleanup is merely a performance // optimization. var self = this; - this._cleanupTimer = setInterval( - function() { self._removeExpiredItems(); }, + var timer = this._cleanupTimer = setInterval( + function() { + if (self && self._removeExpiredItems) { + self._removeExpiredItems(); + } else { + // The datasource/connector was destroyed - cancel the timer + clearInterval(timer); + } + }, 1000); this._cleanupTimer.unref(); }; diff --git a/test/kv-memory.js b/test/kv-memory.js index 72d21f42..f04b49a5 100644 --- a/test/kv-memory.js +++ b/test/kv-memory.js @@ -2,15 +2,9 @@ var kvMemory = require('../lib/connectors/kv-memory'); var DataSource = require('..').DataSource; describe('KeyValue-Memory connector', function() { - var lastDataSource; var dataSourceFactory = function() { - lastDataSource = new DataSource({ connector: kvMemory }); - return lastDataSource; + return new DataSource({ connector: kvMemory }); }; - afterEach(function disconnectKVMemoryConnector() { - if (lastDataSource) return lastDataSource.disconnect(); - }); - require('./kvao.suite')(dataSourceFactory); });