Bug 117276 - [13 Regression] std::sort(par_unseq ,...) leaks memory when called repeatedly
Summary: [13 Regression] std::sort(par_unseq ,...) leaks memory when called repeatedly
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: 13.5
Assignee: Jonathan Wakely
URL: https://github.com/oneapi-src/oneTBB/...
Keywords:
: 113504 (view as bug list)
Depends on:
Blocks:
 
Reported: 2024-10-23 17:00 UTC by Oliver Schönrock
Modified: 2026-01-23 11:24 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.5.0
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
Comment 3 Oliver Schönrock 2024-11-04 15:40:59 UTC
Realised the link to my repo with demo code was incorrect. Here is the correct one:

https://github.com/oschonrock/tbbleak
Comment 4 Oliver Schönrock 2024-11-11 15:38:34 UTC
Is there anything I can do to help move this forward?
Comment 5 Sam James 2024-11-11 15:42:05 UTC
Jonathan already self-assigned it which indicates he's planning to work on it soon, FWIW (that doesn't happen for all libstdc++ bugs automatically).

But if you're interested in trying a patch, he might be able to give you advice if you want to learn (which is welcome).
Comment 6 Sam James 2024-11-11 15:42:47 UTC
(In reply to Sam James from comment #5)
> But if you're interested in trying a patch, he might be able to give you
> advice if you want to learn (which is welcome).

Ah, sorry, I see you already did -- maybe submit it formally?
Comment 7 Oliver Schönrock 2024-11-17 01:08:39 UTC
I did some additional testing on older distributions. On debian 11, bullseye, oldstable, we have 

gcc version 10.2.1 20210110 (Debian 10.2.1-6) 

I noticed that my patch DID NOT apply

and the leak is NOT present with libstc++ as is. 

$ ./build/gcc/release/sort_leak 
Single threaded sort:
VM:    13900   RSS:    11012 (kB)
VM:    13968   RSS:    11288 (kB)
VM:    13968   RSS:    11288 (kB)
VM:    13968   RSS:    11288 (kB)
VM:    13968   RSS:    11288 (kB)

par_unseq sort:
VM:    94268   RSS:    15684 (kB)
VM:    94268   RSS:    15684 (kB)
VM:    94268   RSS:    15684 (kB)
VM:    94268   RSS:    15684 (kB)
VM:    94268   RSS:    15684 (kB)
Comment 8 Jonathan Wakely 2024-11-17 12:25:30 UTC
GCC 10 is not supported upstream, so we don't care about it anyway.
Comment 9 Sam James 2024-11-17 12:30:00 UTC
I suppose the point is that it makes it a regression.
Comment 10 Oliver Schönrock 2024-11-17 12:31:16 UTC
Sorry, should have made that clear.. 

yes, appears to be a regression between gcc-10 and gcc-12
Comment 11 Benjamin Buch 2025-09-12 11:37:45 UTC
I can reproduce this with GCC 15.2 on Ubuntu 25.10 via:

https://github.com/oschonrock/tbbleak

./build.sh
-- The CXX compiler identification is GNU 15.2.0
...

./build/gcc/relwithdebinfo/sort_leak
Single threaded sort:
VM:    17492   RSS:    12140 (kB)
VM:    17496   RSS:    12176 (kB)
VM:    17496   RSS:    12176 (kB)
VM:    17496   RSS:    12176 (kB)
VM:    17496   RSS:    12176 (kB)

par_unseq sort:
VM:   332764   RSS:    17820 (kB)
VM:   332764   RSS:    19612 (kB)
VM:   332764   RSS:    21148 (kB)
VM:   345052   RSS:    23068 (kB)
VM:   345052   RSS:    24860 (kB)
Comment 12 GCC Commits 2025-09-12 13:10:03 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:d8f1655a781a76f5c86b3545b181b2005e585d29

commit r16-3824-gd8f1655a781a76f5c86b3545b181b2005e585d29
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 12 12:49:39 2025 +0100

    libstdc++: Fix memory leak in PSTL TBB backend [PR117276]
    
    Backport of upstream patch:
    https://github.com/uxlfoundation/oneDPL/pull/1589
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/117276
            * include/pstl/parallel_backend_tbb.h (__func_task::finalize):
            Make deallocation unconditional.
Comment 13 Jonathan Wakely 2025-09-12 13:11:10 UTC
Thanks for the reminder, fixed on trunk only so far.
Comment 14 Oliver Schönrock 2025-09-12 15:08:26 UTC
(In reply to Jonathan Wakely from comment #13)
> Thanks for the reminder, fixed on trunk only so far.

Great to see this get fixed. Thanks Jonathan.
Comment 15 GCC Commits 2025-09-12 20:41:08 UTC
The releases/gcc-15 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:210b7a0d8cae48268b69a331249e702495cf2639

commit r15-10320-g210b7a0d8cae48268b69a331249e702495cf2639
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 12 12:49:39 2025 +0100

    libstdc++: Fix memory leak in PSTL TBB backend [PR117276]
    
    Backport of upstream patch:
    https://github.com/uxlfoundation/oneDPL/pull/1589
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/117276
            * include/pstl/parallel_backend_tbb.h (__func_task::finalize):
            Make deallocation unconditional.
    
    (cherry picked from commit d8f1655a781a76f5c86b3545b181b2005e585d29)
Comment 16 GCC Commits 2025-09-15 21:07:10 UTC
The releases/gcc-14 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:83926770efa15d5e0a1e265d26012bdd623fd75e

commit r14-12026-g83926770efa15d5e0a1e265d26012bdd623fd75e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 12 12:49:39 2025 +0100

    libstdc++: Fix memory leak in PSTL TBB backend [PR117276]
    
    Backport of upstream patch:
    https://github.com/uxlfoundation/oneDPL/pull/1589
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/117276
            * include/pstl/parallel_backend_tbb.h (__func_task::finalize):
            Make deallocation unconditional.
    
    (cherry picked from commit d8f1655a781a76f5c86b3545b181b2005e585d29)
Comment 17 Jonathan Wakely 2025-12-19 14:54:17 UTC
*** Bug 113504 has been marked as a duplicate of this bug. ***
Comment 18 GCC Commits 2026-01-23 11:23:59 UTC
The releases/gcc-13 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:0caeca8cd77e23c8155e0f7c14de3aabac53b941

commit r13-10050-g0caeca8cd77e23c8155e0f7c14de3aabac53b941
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 12 12:49:39 2025 +0100

    libstdc++: Fix memory leak in PSTL TBB backend [PR117276]
    
    Backport of upstream patch:
    https://github.com/uxlfoundation/oneDPL/pull/1589
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/117276
            * include/pstl/parallel_backend_tbb.h (__func_task::finalize):
            Make deallocation unconditional.
    
    (cherry picked from commit d8f1655a781a76f5c86b3545b181b2005e585d29)
Comment 19 Jonathan Wakely 2026-01-23 11:24:24 UTC
Fixed for 13.5, 14.4, 15.3