Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the Implementation of get() in MultiItemCache to Prevent Stack Exhaustion #15047

Merged
merged 2 commits into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/CacheFacade.js
Expand Up @@ -5,6 +5,7 @@

"use strict";

const { forEachBail } = require("enhanced-resolve");
const asyncLib = require("neo-async");
const getLazyHashedEtag = require("./cache/getLazyHashedEtag");
const mergeEtags = require("./cache/mergeEtags");
Expand Down Expand Up @@ -46,15 +47,7 @@ class MultiItemCache {
* @returns {void}
*/
get(callback) {
const next = i => {
this._items[i].get((err, result) => {
if (err) return callback(err);
if (result !== undefined) return callback(null, result);
if (++i >= this._items.length) return callback();
next(i);
});
};
next(0);
forEachBail(this._items, (item, callback) => item.get(callback), callback);
}

/**
Expand Down
65 changes: 65 additions & 0 deletions test/MultiItemCache.unittest.js
@@ -0,0 +1,65 @@
"use strict";

const Cache = require("../lib/Cache");
const { ItemCacheFacade, MultiItemCache } = require("../lib/CacheFacade");

describe("MultiItemCache", () => {
it("Throws when getting items from an empty Cache", () => {
const multiItemCache = new MultiItemCache(generateItemCaches(0));
expect(() => multiItemCache.get(_ => _())).toThrowError();
});

it("Returns the single ItemCacheFacade when passed an array of length 1", () => {
const itemCaches = generateItemCaches(1);
const multiItemCache = new MultiItemCache(itemCaches);
expect(multiItemCache).toBe(itemCaches[0]);
});

it("Retrieves items from the underlying Cache when get is called", () => {
const itemCaches = generateItemCaches(10);
const multiItemCache = new MultiItemCache(itemCaches);
const callback = (err, res) => {
expect(err).toBeNull();
expect(res).toBeInstanceOf(Object);
};
for (let i = 0; i < 10; ++i) {
multiItemCache.get(callback);
}
});

it("Can get() a large number of items without exhausting the stack", () => {
const itemCaches = generateItemCaches(10000, () => undefined);
const multiItemCache = new MultiItemCache(itemCaches);
let callbacks = 0;
const callback = (err, res) => {
expect(err).toBeNull();
expect(res).toBeUndefined();
++callbacks;
};
multiItemCache.get(callback);
expect(callbacks).toEqual(1);
});

function generateItemCaches(howMany, dataGenerator) {
const ret = [];
for (let i = 0; i < howMany; ++i) {
const name = `ItemCache${i}`;
const tag = `ItemTag${i}`;
const dataGen =
dataGenerator ||
(() => {
return { name: tag };
});
const cache = new Cache();
cache.hooks.get.tapAsync(
"DataReturner",
(_identifier, _etag, _gotHandlers, callback) => {
callback(undefined, dataGen());
}
);
const itemCache = new ItemCacheFacade(cache, name, tag);
ret[i] = itemCache;
}
return ret;
}
});