C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::thread::detach() can segfault

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Wed, 11 Dec 2024 21:33:28 -0600
Hi,

> Because under high concurrency a call to C++'s detach() can
understandibly segfault.

I don't think this is understandable, a segfault is always a bug and as
others have noted both .detach() and the thread constructor should throw.
More generally, nothing about std::thead's specification is left undefined
so unless you're violating the rules of the standard in some other way it
should be impossible to segfault while using std::thread.

> Impossible because the backtrace lists __pthread_detach() and the thread
uses a reference to a global data structure.

I'd like to briefly note that memory errors leading to segfault often
manifest far from their source.

> You see, under high concurrency I'm creating simple threads followed
immediately by detach(). And very rarely that call crashes.
> I'm really surprised it crashes given I assumed libpthread was the most
tested library of all time.

I'm surprised too.

In the absence of more details or a repro, I tried throwing something silly
together:

#include <thread>
#include <vector>

void worker() {
    volatile int x = 0;
    for(int i = 0; i < 1000; i++) {
        x++;
    }
}

void thread_creater() {
    while(true) {
        std::thread t(worker);
        t.detach();
    }
}

int main() {
    std::vector<std::jthread> creaters;
    for(int i = 0; i < 16; i++) {
        creaters.emplace_back(thread_creater);
    }
}

$ g++ -g test.cpp -std=c++20 -O3 && ./a.out
test.cpp: In function ‘void worker()’:
test.cpp:7:9: warning: ‘++’ expression of ‘volatile’-qualified type is
deprecated [-Wvolatile]
    7 | x++;
      | ^
zsh: segmentation fault (core dumped) ./a.out

I was able to get it to segfault a couple times. So, there does seem to be
something here but it does not seem to be a standards issue.

Cheers,
Jeremy

On Wed, Dec 11, 2024 at 5:19 PM Phil Bouchard via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Impossible because the backtrace lists __pthread_detach() and the thread
> uses a reference to a global data structure.
>
> --
> [image: Logo] <https://www.fornux.com/>
> *Phil Bouchard* [image: facebook icon]
> <https://www.linkedin.com/in/phil-bouchard-5723a910/>
> Founder & CEO
> T: (819) 328-4743
> E: phil_at_[hidden] | www.fornux.com
> 320-345 de la Gauchetière Ouest | Montréal (Qc), H2Z 0A2 Canada
> [image: Banner] <https://static.fornux.com/c-superset/>Le message
> ci-dessus, ainsi que les documents l'accompagnant, sont destinés uniquement
> aux personnes identifiées et peuvent contenir des informations
> privilégiées, confidentielles ou ne pouvant être divulguées. Si vous avez
> reçu ce message par erreur, veuillez le détruire.
> This communication (and/or the attachments) is intended for named
> recipients only and may contain privileged or confidential information
> which is not to be disclosed. If you received this communication by mistake
> please destroy all copies.
>
> On Dec 11, 2024, at 5:48 PM, Thiago Macieira via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
> On Wednesday 11 December 2024 18:30:45 Brasilia Standard Time Phil
> Bouchard
> via Std-Proposals wrote:
>
> You see, under high concurrency I'm creating simple threads followed
>
> immediately by detach(). And very rarely that call crashes.
>
>
>
> I'm really surprised it crashes given I assumed libpthread was the most
>
> tested library of all time.
>
>
> Not the most tested, but one of (assuming we're talking about the glibc
> one,
> not stuff like Android Bionic's).
>
> The most likely scenario is that the "under high concurrency" you're
> talking
> about contains a data race in your own code.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Principal Engineer - Intel DCAI Platform & System Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-12-12 03:33:45