fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
477 stars 66 forks source link

Cannot read properties of null (reading 'key') — /app/node_modules/tiny-lru/dist/tiny-lru.cjs in LRU.evict at line 65:27 #343

Closed kevbarns closed 9 months ago

kevbarns commented 9 months ago

Prerequisites

Fastify version

4.23.2

Plugin version

8.0.3

Node.js version

20

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

alpine

Description

We are experiencing an issue coming from the rate-limiter plugin when facing a lot of request on a single endpoint, more specifically from the tiny-lru package here :

TypeError: Cannot read properties of null (reading 'key')
  File "/app/node_modules/tiny-lru/dist/tiny-lru.cjs", line 65, col 27, in LRU.evict
    delete this.items[item.key];
  File "/app/node_modules/tiny-lru/dist/tiny-lru.cjs", line 144, col 10, in LRU.set
    this.evict(true);
  File "/app/node_modules/@fastify/rate-limit/store/LocalStore.js", line 27, col 14, in LocalStore.incr
    this.lru.set(ip, current)
  File "/app/node_modules/@fastify/rate-limit/index.js", line 217, col 18, in res
    theStore.incr(key, function (err, res) {
  File "<anonymous>", in new Promise
  File "/app/node_modules/@fastify/rate-limit/index.js", line 216, col 25, in Object.onRequestRateLimiter
    const res = await new Promise(function (resolve, reject) {
  File "node:internal/process/task_queues", line 95, col 5, in process.processTicksAndRejections

Logs comes from sentry detection.

Steps to Reproduce

Set a basic endpoint with the following rate limit configuration and target it with enough request to trigger the rate limit :

  rateLimit: {
    max: 7,
    timeWindow: "1s",
  },

Expected Behavior

no issues

mcollina commented 9 months ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

gurgunday commented 9 months ago

Also, new major version does away with tiny-lru, can you try that one?

kevbarns commented 9 months ago

Thanks @gurgunday, thaht might just fix my issue !