[Cppcms-users] Simple long polling application has memory leaks (sockets left in CLOSE_WAIT state in
Brought to you by:
artyom-beilis
From: Cody S. <cod...@gu...> - 2016-01-14 23:20:13
|
Hi all, I'm just starting to get my hands dirty with cppcms and long-polling, and I'm having some troubles. I've followed the example provided in the "chat" application of the examples directory and made some tweaks (code will be provided below). The problem arises in an ajax function called "listen()", which makes an ajax POST request to the server and waits until the server has responded or 10 seconds have passed, at which point it will fire up another "long- polling" request. However, each of these requests remains open in the case that they time out, and then the lighttpd server refuses any more connections after too many requests have been issued. I'm not sure what I'm doing wrong with my long-polling approach, but for some reason, these sockets are not getting closed and I've been unable to determine why after 2 days of investigating. Here is the client code (using jQuery): function listen(){ $.ajax({ url: "/chat/listen", timeout: 10000, // 10 seconds type: "GET", dataType: "json", success: function(r, st, xhr){ console.log(r); listen(); }, error: function(xhr, st, e){ if(st !== "timeout"){ return alert("error in listen(): " + st); } listen(); // If we timed out, fire another long poll } }); } $(function(){ listen(); }) And here is the server code: std::set<booster::shared_ptr<cppcms::http::context> > waiters_; ... void remove_context(booster::shared_ptr<cppcms::http::context> context) { waiters_.erase(context); } void listen() { booster::shared_ptr<cppcms::http::context> context = release_context(); waiters_.insert(context); context->async_on_peer_reset( std::bind( &chat::remove_context, this, context)); } After running the application for a few minutes, I run `sudo lsof -n | grep lighttpd` and the printout indicates that each request that has timed out has a corresponding socket that is hung in the CLOSE_WAIT state and remains that way until I bring the lighttpd server down and back up again. Any insight you can offer regarding this issue would be greatly appreciated. Please let me know if I should provide any more information; I have the lighttpd and cppcms configuration files on hand, as well as the rest of the client and server code, I just didn't want to have too much noise in this post. |