Commit d6643d7
Stop RateLimiter from registering a listener per queued task
Summary:
## Why
Every task that `RateLimiter.enqueueRun` had to queue parked itself on a fresh `'run'` listener
registered on the limiter's private `TypedEventEmitter`, and nothing ever removed it -- there was no
`.off()` anywhere in `RateLimiter.ts`. The listener set therefore grew with every task the limiter
had *ever* queued and only died with the limiter itself.
Three instances leak:
| Site | Concurrency | Scope |
| --- | --- | --- |
| `isl-server/src/Repository.ts` `configRateLimiter` | 1 | per `Repository` |
| `isl-server/src/Repository.ts` `catLimiter` | 4 | per `Repository` |
| `isl-server/src/facebook/phabricator/queryGraphql.ts` | 7 | module-level, process-wide singleton |
The visible symptom on every isl-server spawn, on both macOS and Linux, in production:
```
MaxListenersExceededWarning: Possible EventTarget memory leak detected.
11 run listeners added to EventTarget. MaxListeners is 10.
```
It says `EventTarget` rather than `EventEmitter` because `TypedEventEmitter` wraps a plain
`EventTarget`.
The warning badly under-reports the problem. Node latches a warned-once flag per
`(target, eventType)` pair, so it fires exactly once per process no matter how far past 11 the count
climbs -- the log can never show the growth. And memory is not the only cost: every stale listener
is still invoked on each subsequent `emit('run')`, so dequeuing one task is O(tasks ever queued) and
a long session is O(n^2).
Calling `off` in a `finally` would fix the leak but leave the warning reachable, because the
listener set would still be one live `EventTarget` listener per *currently waiting* task. That is
enough to re-emit the byte-identical warning on ordinary backpressure:
`vscode/extension/facebook/comments/LineRealigner.ts` issues one `repo.cat` per unique commented
`(hash, path)` inside a single `Promise.all`, into the per-`Repository` `catLimiter` whose
concurrency is `MAX_SIMULTANEOUS_CAT_CALLS = 4`, so a diff with more than 14 distinct commented
pairs trips it; the process-wide GraphQL limiter (concurrency 7) does the same at 18 concurrent
queries. Such a warning would be a false positive on transient backpressure, indistinguishable in a
log from the leak this diff exists to eliminate, and would cost the next engineer the same
investigation over again.
## What
`RateLimiter` no longer uses `TypedEventEmitter` at all; the import and the `runs` field are gone.
`queued` changes from `Array<Id>` to `Array<{id, allowedToRun}>`, where `allowedToRun` is that
waiter's own `Deferred`, and `run` resolves it directly instead of broadcasting `emit('run', id)`
for every waiter to filter on by id.
This makes the `EventTarget` flavor of `MaxListenersExceededWarning` structurally impossible rather
than merely less likely: it can only be raised from `addEventListener`, and the limiter now never
calls it, at any concurrency, for any number of waiters. (Node raises the same warning text from
`EventEmitter.addListener` as well, so parking waiters on a `node:events` emitter would reintroduce
it -- this removes the reachable path, not the warning from the runtime.) Handing out a turn also stops walking the
whole waiter set, and the `TypedEventEmitter` listener-to-wrapper `Map` that was leaking alongside
the `EventTarget` is gone with it.
The deferred is created before the task is pushed onto `queued`, so `run` can hand out a turn
whether or not `enqueueRun` has reached its `await` yet. Correctness therefore no longer rests on
the "`queued` non-empty implies `running` is at max" ordering argument that the emitter version
needed to guarantee a waiter had subscribed before its own wake-up fired.
The public API, the concurrency limits, the FIFO ordering and the `log` callback strings are all
unchanged. The one timing difference is that `now allowing ID:N to run` is logged by the resumed
waiter rather than from inside the dequeue, one microtask later. `setMaxListeners` was deliberately
*not* used, since that only silences the warning.
Reviewed By: evangrayk
Differential Revision: D115241638
fbshipit-source-id: 098ba01734776ce092c98ed17eebae602508500b1 parent 2b18be9 commit d6643d7
2 files changed
Lines changed: 171 additions & 15 deletions
File tree
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
12 | 20 | | |
13 | 21 | | |
14 | 22 | | |
| |||
23 | 31 | | |
24 | 32 | | |
25 | 33 | | |
26 | | - | |
| 34 | + | |
27 | 35 | | |
28 | | - | |
29 | 36 | | |
30 | 37 | | |
31 | 38 | | |
| |||
39 | 46 | | |
40 | 47 | | |
41 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
42 | 52 | | |
43 | | - | |
| 53 | + | |
44 | 54 | | |
45 | 55 | | |
46 | 56 | | |
47 | 57 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 58 | + | |
| 59 | + | |
56 | 60 | | |
57 | 61 | | |
58 | 62 | | |
| |||
76 | 80 | | |
77 | 81 | | |
78 | 82 | | |
79 | | - | |
80 | | - | |
81 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
82 | 86 | | |
83 | 87 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
12 | 23 | | |
13 | 24 | | |
14 | 25 | | |
| |||
130 | 141 | | |
131 | 142 | | |
132 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
133 | 285 | | |
0 commit comments