Skip to content

Backport !2745 “gsocketclient: Fix still-reachable references to cancellables” to glib-2-72

GSocketClient chains its internal GCancellable objects to ones provided by the caller in two places using g_cancellable_connect(). However, it never calls g_cancellable_disconnect(), instead relying (incorrectly) on the GCancellable provided by the caller being short-lived.

In the (valid) situation where a caller reuses one GCancellable for multiple socket client calls, or for calls across multiple socket clients, this will cause the internal GCancellable objects from those GSocketClients to accumulate, with one reference left each (which is the reference from the g_cancellable_connect() closure).

These GCancellable instances aren’t technically leaked, as they will all be freed when the caller’s GCancellable is disposed, but they are no longer useful and there is no bound on the number of them which will hang around.

For a program doing a lot of socket operations, this still-reachable memory usage can become significant.

Fix the problem by adding paired g_cancellable_disconnect() calls. It’s not possible to add a unit test as we can’t measure still-reachable memory growth before the end of a unit test when everything has to be freed.

Signed-off-by: Philip Withnall pwithnall@endlessos.org

Fixes: #2670 (closed)


Trivial backport of !2745 (merged) to glib-2-72.

Merge request reports