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

fix: TimeCache handle key collision to prevent leak #358

Merged
merged 2 commits into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,12 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
const messageId = { msgId, msgIdStr }

// Add the message to the duplicate caches
if (fastMsgIdStr !== undefined) this.fastMsgIdCache?.put(fastMsgIdStr, msgIdStr)
if (fastMsgIdStr !== undefined && this.fastMsgIdCache) {
const collision = this.fastMsgIdCache.put(fastMsgIdStr, msgIdStr)
if (collision) {
this.metrics?.fastMsgIdCacheCollision.inc()
}
}

if (this.seenCache.has(msgIdStr)) {
return { code: MessageStatus.duplicate, msgIdStr }
Expand Down
5 changes: 5 additions & 0 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,11 @@ export function getMetrics(
help: 'Current mcache msg count not validated'
}),

fastMsgIdCacheCollision: register.gauge({
name: 'gossipsub_fastmsgid_cache_collision_total',
help: 'Total count of key collisions on fastmsgid cache put'
}),

topicStrToLabel: topicStrToLabel,

toTopic(topicStr: TopicStr): TopicLabel {
Expand Down
16 changes: 14 additions & 2 deletions src/utils/time-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ export class SimpleTimeCache<T> {
return this.entries.size
}

put(key: string | number, value: T): void {
/** Returns true if there was a key collision and the entry is dropped */
put(key: string | number, value: T): boolean {
if (this.entries.has(key)) {
// Key collisions break insertion order in the entries cache, which break prune logic.
// prune relies on each iterated entry to have strictly ascending validUntilMs, else it
// won't prune expired entries and SimpleTimeCache will grow unexpectedly.
// As of Oct 2022 NodeJS v16, inserting the same key twice with different value does not
// change the key position in the iterator stream. A unit test asserts this behaviour.
return true
}

this.entries.set(key, { value, validUntilMs: Date.now() + this.validityMs })
return false
}

prune(): void {
Expand All @@ -38,7 +49,8 @@ export class SimpleTimeCache<T> {
if (v.validUntilMs < now) {
this.entries.delete(k)
} else {
// sort by insertion order
// Entries are inserted with strictly ascending validUntilMs.
// Stop early to save iterations
break
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/time-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,28 @@ describe('SimpleTimeCache', () => {
expect(timeCache.has('bFirst')).to.be.false()
expect(timeCache.has('cFirst')).to.be.false()
})

it('Map insertion order', () => {
const key1 = 'key1'
const key2 = 'key2'
const key3 = 'key3'

const map = new Map<string, number>()
map.set(key1, Date.now())
map.set(key2, Date.now())
map.set(key3, Date.now())

expect(Array.from(map.keys())).deep.equals([key1, key2, key3], 'Map iterator order')

// Does not change key position
map.set(key2, Date.now())

expect(Array.from(map.keys())).deep.equals([key1, key2, key3], 'Map iterator order after re-set')

// Changes key position
map.delete(key2)
map.set(key2, Date.now())

expect(Array.from(map.keys())).deep.equals([key1, key3, key2], 'Map iterator order after delete set')
})
})