Hibernation API Memory Management #1590
Replies: 1 comment 3 replies
-
I don't quite understand the concern here. The WebSocket objects should go away when they are closed; if not, that is a bug we should fix. Assuming no such bug, can't the app just close connections if it needs to free up memory? FWIW these objects are not very big, I think you'd need hundreds of thousands of them in order to impact the memory limit. On another note, if you want to force eviction of your DO, you can do so by doing |
Beta Was this translation helpful? Give feedback.
-
It was pointed out to me today by @MellowYarker that using the Hibernatable WebSocket API does not preclude running out of memory due to WebSocket objects not being automatically deleted when no longer referenced in user code. While at a smaller scale, this wouldn't be much of an issue, when you get into the hundreds/thousands of WebSockets being hibernated, it could become a real problem, especially if your DO isn't evicted very often.
We ended on going on a discussion on possible fixes, here are two we could come up with:
state.evict
API, which would allow user code to manually trigger an eviction. I had envisioned this as a non-blocking API, that would cause incoming events to be queued, rather than passed in to the DO. Then, once all currently processing events had completed, you could evict the DO from memory, allowing it to be restarted fresh. The evict function could also include the ability to pass a function that would be called right before eviction, allowing you to flush to storage(this would only apply for scheduled evictions called by user code, not unscheduled evictions caused by runtime/code updates).Neither of these options are entirely ideal. The runtime tracking requires some finicky manipulation of object lifetimes, and the eviction API would have to be limited in what it can do to prevent it from holding up the DO from restarting in a timely manner.
I'm curious to hear if y'all have any other ideas for how management of WebSocket objects in regards to the Hibernation API could be handled efficiently.
Beta Was this translation helpful? Give feedback.
All reactions