microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
8.01k stars 1.66k forks source link

Linked CancellationTokenSource Runtime Leaking #589

Open SeverTopan opened 7 years ago

SeverTopan commented 7 years ago

https://github.com/Microsoft/cpprestsdk/blob/a7e49f51866b3c743024ec919977356b06eb9bdd/Release/include/pplx/pplxcancellation_token.h#L1003-L1012

Consider the creation of a linked cancellation source between Cancellation sources A and B. B is a long-lived cancellation token. When A is canceled, the cancellation registration for the callback on B is never unregistered, so it lives on in memory. This can be a significant source of runtime leaks, in a pattern similar to those caused by pplx task cancellation semantics.

Note that this is not technically a memory leak, since everything is still referenced, though it is hugely problematic. Certain architectures can rely on long-lived cancellation tokens from which many linked tokens are created. These long-lived tokens can accumulate an unbounded number of functionally useless cancellation registrations over time, wasting system resources.

This problem can be solved by adding a clause to the cancellation callback on line 1009 that unregisters other remaining callbacks.

john4744 commented 4 years ago

I think there's a similar issue here when using when_all/when_any: https://github.com/microsoft/cpprestsdk/blob/f4124a9ee10042582c795578d69022368877282e/Release/include/pplx/pplxtasks.h#L6520