From 84b18a0cdd16eb26665f1a05b2c10fbace75f890 Mon Sep 17 00:00:00 2001 From: Tom Boettger Date: Mon, 10 Oct 2022 21:08:17 +0200 Subject: [PATCH 1/4] Unref the interval to allow server to exit If not unref'ed the event loop would always keep a single item in the loop and not exit --- src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0917205..9d4e33e 100644 --- a/src/index.js +++ b/src/index.js @@ -30,6 +30,8 @@ module.exports = ({ Store }) => { intervalMs: (options.expired && options.expired.intervalMs) || clearExpiredInterval, + unrefInterval: (options.expired && options.expired.unrefInterval) || + false, }; this.client = options.client; this.createDb(); @@ -40,10 +42,13 @@ module.exports = ({ Store }) => { } startInterval() { - setInterval( + const timeout = setInterval( this.clearExpiredSessions.bind(this), this.expired.intervalMs ); + if (this.expired.unrefTimeout) { + timeout.unref(); + } } clearExpiredSessions() { From 69bb7128a671bc7f315e1b0417ccb054333c42a4 Mon Sep 17 00:00:00 2001 From: Tom Boettger Date: Mon, 10 Oct 2022 21:16:57 +0200 Subject: [PATCH 2/4] Fix old variable name --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 9d4e33e..9075034 100644 --- a/src/index.js +++ b/src/index.js @@ -46,7 +46,7 @@ module.exports = ({ Store }) => { this.clearExpiredSessions.bind(this), this.expired.intervalMs ); - if (this.expired.unrefTimeout) { + if (this.expired.unrefInterval) { timeout.unref(); } } From f5406436d59a6815cae3c7c672529dd3a1692863 Mon Sep 17 00:00:00 2001 From: Tom Boettger Date: Wed, 12 Oct 2022 13:56:42 +0200 Subject: [PATCH 3/4] Write tests for unreferencing the expiration interval --- test/index_test.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/index_test.js b/test/index_test.js index 00f8fc0..7fa4868 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -5,6 +5,7 @@ const session = require("express-session"); const { unlinkSync, existsSync } = require("fs"); const differenceInSeconds = require("date-fns/differenceInSeconds"); const add = require("date-fns/add"); +const async_hooks = require("node:async_hooks"); const SqliteStore = require("../src/index.js")(session); @@ -428,3 +429,65 @@ test("what happens when table is deleted and all methods are invoked (they shoul s.length((err, res) => t.assert(err)); s.destroy(sid, (err, res) => t.assert(err)); }); + +function getSymbolValue(obj, symbol) { + const refedSymbol = Object.getOwnPropertySymbols(obj).find( + (s) => s.toString() === symbol + ); + return obj[refedSymbol]; +} + +test("if the expiration interval continues being referenced", async (t) => { + const timeoutMs = 555; + let loggedResource; + + function init(asyncId, type, triggerAsyncId, resource) { + if (type === "Timeout" && resource._idleTimeout === timeoutMs) { + loggedResource = resource; + t.assert(getSymbolValue(loggedResource, "Symbol(refed)")); + } + } + const asyncHook = async_hooks.createHook({ + init, + }); + asyncHook.enable(); + + const db = new sqlite(dbName, dbOptions); + const s = new SqliteStore({ + client: db, + expired: { + clear: true, + intervalMs: timeoutMs, + }, + }); + + t.assert(getSymbolValue(loggedResource, "Symbol(refed)")); +}); + +test("if the expiration interval is going to be unreferenced", async (t) => { + const timeoutMs = 555; + let loggedResource; + + function init(asyncId, type, triggerAsyncId, resource) { + if (type === "Timeout" && resource._idleTimeout === timeoutMs) { + loggedResource = resource; + t.assert(getSymbolValue(loggedResource, "Symbol(refed)")); + } + } + const asyncHook = async_hooks.createHook({ + init, + }); + asyncHook.enable(); + + const db = new sqlite(dbName, dbOptions); + const s = new SqliteStore({ + client: db, + expired: { + clear: true, + intervalMs: timeoutMs, + unrefInterval: true, + }, + }); + + t.assert(!getSymbolValue(loggedResource, "Symbol(refed)")); +}); From 18b3081fc0f6c88ff2cf6530db2e1b9f4c1442af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Thu, 13 Oct 2022 12:42:45 -0500 Subject: [PATCH 4/4] Update test/index_test.js --- test/index_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index_test.js b/test/index_test.js index 7fa4868..d3e6bcc 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -5,7 +5,7 @@ const session = require("express-session"); const { unlinkSync, existsSync } = require("fs"); const differenceInSeconds = require("date-fns/differenceInSeconds"); const add = require("date-fns/add"); -const async_hooks = require("node:async_hooks"); +const async_hooks = require("async_hooks"); const SqliteStore = require("../src/index.js")(session);