HttpFilteringEngine icon indicating copy to clipboard operation
HttpFilteringEngine copied to clipboard

We've really done it now - every connection will live forever in memory

Open TechnikEmpire opened this issue 7 years ago • 4 comments

At some unknown point in history, our use enable_shared_from_this has ended up causing every connection to get pinned in memory forever. Massive leak, unsure how this snuck by unnoticed for what appears to be some time.

TechnikEmpire avatar Oct 17 '17 11:10 TechnikEmpire

Even more interesting, a single destructor for ~TlsCapableHttpBridge() will get call on application exit, but not all the rest.

TechnikEmpire avatar Oct 17 '17 11:10 TechnikEmpire

I believe the cause of this is the use of lambda's for the async asio functions, where we use a shared_ptr self reference and pass it to the lambda via the capture list. This smells like the kind of situation that would pin a shared_ptr forever because you're defining it inside of a member function and capturing inside of another member function.

TechnikEmpire avatar Oct 17 '17 11:10 TechnikEmpire

Methinks the best solution here is to entirely eliminate shared_from_this and just have the acceptors keep instances to clients, and make them drop instances when a client flags itself as finito. This will avoid the problem both now and forever.

TechnikEmpire avatar Oct 17 '17 12:10 TechnikEmpire

Sigh, even if we do the above change, the asio reactor will hold on to callback references for an indeterminate period of time. If we allow this to expire while the reactor is still holding those callbacks, then we'll get a hard crash because the object is gone when the callback is finally invoked.

TechnikEmpire avatar Oct 18 '17 22:10 TechnikEmpire