Re: [Cppcms-users] Simple long polling application has memory leaks (sockets left in CLOSE_WAIT stat
Brought to you by:
artyom-beilis
From: Cody S. <cod...@gu...> - 2016-01-15 16:00:15
|
>>Cody Sheffler <cody.sheffler@...> writes: >> >> >> 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. redred77 <redred77@...> writes: > > > Maybe you can reuse socket connection which is on CLOSE_WAIT state by changing linux system conf. I don't remember the actual command but you will easily find it from Google. > Well I'm not sure whether this is the right solution but I experienced problem when linux system took too long wait time on CLOSE_WAIT and it caused blocking new socket connection. I hope it helps. Thanks for the reply, redred! I don't want to poke around in the linux system config unless someone can tell me with confidence that that's where the issue lies. I feel like I should be able to handle this from either the client or the server application. Is there some cleanup I'm forgetting to perform in the server or client code I've posted? Or is cppcms not built to support long-polling? I apologize for my lack of expertise on this matter; I'm new to the nuances of the network stack. |