diff --git a/lib/hk-util.js b/lib/hk-util.js index ccf8c4465..ed5f43297 100644 --- a/lib/hk-util.js +++ b/lib/hk-util.js @@ -325,6 +325,9 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash) { } })); }).nThen((waitFor) => { +/* TODO we can skip updating the index if there's nobody in the channel. + Populating it might actually be the cause of a memory leak. +*/ getIndex(Env, id, waitFor((err, index) => { if (err) { Log.warn("HK_STORE_MESSAGE_INDEX", err.stack); @@ -340,7 +343,12 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash) { line: ((index.line || 0) + 1) }); } - if (optionalMessageHash) { +/* This 'getIndex' call will construct a new index if one does not already exist. + If that is the case then our message will already be present and updating our offset map + can actually cause it to become incorrect, leading to incorrect behaviour when clients connect + with a lastKnownHash. We avoid this by only assigning new offsets to the map. +*/ + if (optionalMessageHash && typeof(index.offsetByHash[optionalMessageHash]) === 'undefined') { index.offsetByHash[optionalMessageHash] = index.size; index.offsets++; }