Commit 89c5405
authored
Fix memory leak by preventing stale SSE unregister dropping active heandlers (#109)
## Problem
After adding Anubis 0.17.1 I noticed that memory usage of my Phoenix
application slowly climbed over the hours until it ran out of memory and
died. It's a pre-production app with little usage and when that happened
there were just logs to connect to the MCP server.
## Solution
After adding some tracking, GPT-5.3-Codex diagnosed this as follows:
- In streamable HTTP transport, SSE handlers are keyed by session_id.
- Reconnects on the same `session_id` can overwrite map entries, and old
stream processes can become orphaned (no longer in `sse_handlers`).
- Orphaned stream loops sit in receive forever.
- `expiry_timers` map in Anubis server is not cleaned on
`:session_expired` path.
With this fix:
- adding pid-aware `unregister_sse_handler/3` so only the expected
handler can remove itself
- replacing existing handlers safely on register (demonitor old ref,
close old handler, monitor/store the new handler)
- updating plug on-close and stale-handler cleanup paths to pass the
handler pid
## Rationale
I've tested this and deployed it to my app, and memory usage has been
stable the last few hours.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
## Release Notes
* **Bug Fixes**
* Enhanced Server-Sent Events (SSE) handler registration logic to
properly manage concurrent handler instances for the same session,
preventing race conditions and ensuring consistent handler state.
* **Tests**
* Added test case to verify SSE handler lifecycle behavior when multiple
handlers are registered for the same session.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 7df9c0d commit 89c5405
3 files changed
Lines changed: 72 additions & 8 deletions
File tree
- lib/anubis/server/transport
- streamable_http
- test/anubis/server/transport
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
196 | | - | |
197 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
255 | 268 | | |
256 | | - | |
| 269 | + | |
| 270 | + | |
257 | 271 | | |
258 | 272 | | |
259 | 273 | | |
| |||
299 | 313 | | |
300 | 314 | | |
301 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
302 | 321 | | |
303 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
304 | 326 | | |
305 | 327 | | |
306 | 328 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
278 | | - | |
| 278 | + | |
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
| 291 | + | |
291 | 292 | | |
292 | 293 | | |
293 | 294 | | |
| |||
296 | 297 | | |
297 | 298 | | |
298 | 299 | | |
299 | | - | |
| 300 | + | |
300 | 301 | | |
301 | 302 | | |
302 | 303 | | |
| |||
525 | 526 | | |
526 | 527 | | |
527 | 528 | | |
| 529 | + | |
528 | 530 | | |
529 | 531 | | |
530 | 532 | | |
531 | 533 | | |
532 | 534 | | |
533 | 535 | | |
534 | | - | |
| 536 | + | |
535 | 537 | | |
536 | 538 | | |
537 | 539 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
50 | 90 | | |
51 | 91 | | |
52 | 92 | | |
| |||
0 commit comments