Bug 117276 - std::sort(par_unseq ,...) leaks memory when called repeatedly
Summary: std::sort(par_unseq ,...) leaks memory when called repeatedly
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: ---
Assignee: Jonathan Wakely
URL: https://github.com/oneapi-src/oneTBB/...
Keywords:
Depends on:
Blocks:
 
Reported: 2024-10-23 17:00 UTC by Oliver Schönrock
Modified: 2024-10-24 15:24 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-10-23 00:00:00


Attachments
code to reproduce problem (966 bytes, text/x-csrc)
2024-10-23 17:00 UTC, Oliver Schönrock
Details
patch which solves the problem (511 bytes, patch)
2024-10-23 17:03 UTC, Oliver Schönrock
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Oliver Schönrock 2024-10-23 17:00:49 UTC
Created attachment 59418 [details]
code to reproduce problem

Environment 
-----------

ubuntu 24.04
libstdc++ 13.2 from ubuntu repos but also 14.2 and judging by libstdc++ github mirror the latest head also has this problem

sudo apt install libtbb-dev ## this is obviously required

Problem
-------
when calling `std::sort(std::execution::par_unseq` repeatedly it leaks memory, and will ultimately deplete the machine and get killed by the kernel. 

the attached
Comment 1 Oliver Schönrock 2024-10-23 17:03:56 UTC
Created attachment 59419 [details]
patch which solves the problem

this patch applies cleanly to libstdc++ 13.2 and 14.2 and also to head I believe. 

It solves the problem. 

The code change is from here: 

https://github.com/oneapi-src/oneDPL/pull/1589/files

By one of the people on the TBB / oneDPL team.

it cannot be applied to TBB ( I did ask there) because the bug is in the "glue/interface" code between libstdc++ and TBB and managed by libstdc++
Comment 2 Oliver Schönrock 2024-10-23 17:13:01 UTC
Sorry, premature submit on original report comment. 

continuing... 

The attached sort_leak.cpp  (59418) shows the problem. Single threaded sort is fine, par_unseq is not. 

output:

Single threaded sort:
VM:    14544   RSS:    11648 (kB)
VM:    14616   RSS:    11588 (kB)
VM:    14616   RSS:    11588 (kB)
VM:    14616   RSS:    11588 (kB)
VM:    14616   RSS:    11588 (kB)

par_unseq sort:
VM:    29372   RSS:    17860 (kB)
VM:    33468   RSS:    19652 (kB)
VM:    33468   RSS:    21316 (kB)
VM:    37564   RSS:    23108 (kB)
VM:    37564   RSS:    24772 (kB)

Related bug reports:

Issue filed by me on oneTBB:
https://github.com/oneapi-src/oneTBB/issues/1533

my repo with demo code, instructions and references should they be needed:
https://github.com/oneapi-src/oneTBB/issues/1533

Pull request on oneDPL which fixes it
https://github.com/oneapi-src/oneDPL/pull/1589/files
(that is what my patch is based on)


Solution
--------
As stated above, the attached patch (59419) fixes the issue