Skip to content

Commit 4978cd8

Browse files
committed
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.
1 parent 8e81185 commit 4978cd8

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

lib/connectors/kv-memory.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ KeyValueMemoryConnector.prototype._setupRegularCleanup = function() {
3131
// key expiration too, the scheduled cleanup is merely a performance
3232
// optimization.
3333
var self = this;
34-
this._cleanupTimer = setInterval(
35-
function() { self._removeExpiredItems(); },
34+
var timer = this._cleanupTimer = setInterval(
35+
function() {
36+
if (self && self._removeExpiredItems) {
37+
self._removeExpiredItems();
38+
} else {
39+
// The datasource/connector was destroyed - cancel the timer
40+
clearInterval(timer);
41+
}
42+
},
3643
1000);
3744
this._cleanupTimer.unref();
3845
};

test/kv-memory.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ var kvMemory = require('../lib/connectors/kv-memory');
22
var DataSource = require('..').DataSource;
33

44
describe('KeyValue-Memory connector', function() {
5-
var lastDataSource;
65
var dataSourceFactory = function() {
7-
lastDataSource = new DataSource({ connector: kvMemory });
8-
return lastDataSource;
6+
return new DataSource({ connector: kvMemory });
97
};
108

11-
afterEach(function disconnectKVMemoryConnector() {
12-
if (lastDataSource) return lastDataSource.disconnect();
13-
});
14-
159
require('./kvao.suite')(dataSourceFactory);
1610
});

0 commit comments

Comments
 (0)