You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: Debugging a weird case where a websocket server under high load was growing to 20GB+ of RAM.
In this example, the client and the server "take turns" in sending messages, so it shouldn't need to allocate 1GB worth of RAM. And in fact, for some reason we can prevent it from doing that by adding setInterval(() => console.log("hello"), 1000) to the top of the server script. If we do that, the RSS memory doesn't go past 200MB.
Server:
// deno run --allow-net server.jsDeno.serve({port:3086},(req)=>{if(req.headers.get("upgrade")!="websocket"){returnnewResponse(null,{status: 501});}const{ socket, response }=Deno.upgradeWebSocket(req);socket.addEventListener("message",(event)=>{if(event.data==="ping"){socket.send("pong");}});returnresponse;});
Client:
// deno run --allow-net client.jsfunctionconnect(){constsocket=newWebSocket("ws://localhost:3086");socket.onopen=()=>socket.send("ping");socket.onerror=()=>socket.close();socket.onclose=()=>setTimeout(connect,1000);socket.onmessage=(e)=>{if(e.data==="pong"){// First send some extra messages that don't trigger a "pong" response (necessary to reproduce the issue):for(leti=0;i<50;i++){socket.send("foo");}// Then trigger another response:socket.send("ping");}};}for(leti=0;i<1000;i++){connect();}
Notes:
I'm also guessing that it's expected behavior that even after all clients disconnect, the memory isn't freed. I.e. the buffer never shrinks. I can deal with that, though it's definitely not ideal, especially with bugs like this one (i.e. spike in traffic causes allocations, which then "permanently" hog RAM).
On the same server that I was debugging this issue on, I also got an issue like this:
I.e. sockets not closing properly, even after I'd completely disconnected all clients. They were in a CLOSING state, but never triggered the close event. And note that the above-linked issue uses websocat, whereas in my case I was just using Deno as the client, like in the above example. I'm unsure if this is related to this issue. It seems separate.
Although the reason I title this issue "over-allocated" instead of "leak" is because if I terminate all the clients, then start them again, the memory doesn't go to 2GB, it just stays at 1GB. So this indicates that it's just allocating too much space, and not actually leaking.
The text was updated successfully, but these errors were encountered:
In this example, the client and the server "take turns" in sending messages, so it shouldn't need to allocate 1GB worth of RAM. And in fact, for some reason we can prevent it from doing that by adding
setInterval(() => console.log("hello"), 1000)
to the top of the server script. If we do that, the RSS memory doesn't go past 200MB.Server:
Client:
Notes:
CLOSING
state, but never triggered theclose
event. And note that the above-linked issue useswebsocat
, whereas in my case I was just using Deno as the client, like in the above example. I'm unsure if this is related to this issue. It seems separate.The text was updated successfully, but these errors were encountered: