hapijs / catbox

Multi-strategy object caching service
Other
494 stars 72 forks source link

Catbox policy timers are not cleaned after stopping the Catbox Client #230

Closed KeKs0r closed 4 years ago

KeKs0r commented 4 years ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

After stopping the cache client, there are still open handles open from catbox

const log = require("why-is-node-running");
const Catbox = require("@hapi/catbox");
const CatboxMemory = require("@hapi/catbox-memory");

cacheClient = new Catbox.Client(CatboxMemory);

const policy = new Catbox.Policy(
  {
    expiresIn: 5000,
    staleIn: 100,
    staleTimeout: 5,
    generateTimeout: 100,
    generateFunc: () => "5",
  },
  cacheClient,
  "segment"
);

runPolicy();
async function runPolicy() {
  await cacheClient.start();

  await policy.set("id", 5, 500);

  await new Promise((resolve) => setTimeout(resolve, 50));
  log();

  await cacheClient.stop();
  console.log("--------- After stoped cache ----------");
  log();

  await new Promise((resolve) => setTimeout(resolve, 50));
  console.log("--------- After another wait ----------");
  log();
}

What was the result you got?

After running this script, even after stopping the cacheClient this handle was still open:

# Timeout
/Users/marchofflprivat/Workspace/catbox-open-handle/node_modules/@hapi/catbox-memory/lib/index.js:84  - this._timer = setTimeout(cleanup, timeout);
/Users/marchofflprivat/Workspace/catbox-open-handle/node_modules/@hapi/catbox-memory/lib/index.js:187 - this._scheduleCleanup(ttl);
/Users/marchofflprivat/Workspace/catbox-open-handle/node_modules/@hapi/catbox/lib/client.js:91        - await this.connection.set(key, value, ttl);
/Users/marchofflprivat/Workspace/catbox-open-handle/node_modules/@hapi/catbox/lib/policy.js:311       - await this._cache.set({ segment: this._segment, id: internals.id(key) }, value, ttl || internals.Policy.ttl(this.rule));

Full log output can be found in the readme in my repdroduction repo: https://github.com/KeKs0r/catbox-open-handle

What result did you expect?

That after stopping the cache client the timer should be cleaned.

kanongil commented 4 years ago

Thanks for the detailed report. A quick analysis shows that this is not an issue with catbox. The process is exited as it should, without waiting for any previously scheduled timer. The logging from why-is-node-running must be incorrect.

KeKs0r commented 4 years ago

thanks for coming back so quick. I investigated further, and it turns out, if I wrap the log into a setImmediate(log) the handle from catbox is cleaned up.

I guess I need to go into the Jest space and see why it is complaining about open handles there. Thanks for the feedback.