https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97339

            Bug ID: 97339
           Summary: std::async sometimes prevents std::current_exception
                    from working properly in the terminate handler
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m101010a at gmail dot com
  Target Milestone: ---

If std::async is called with a noexcept function that throws an exception and
contains a catch block which catches exceptions of a different type,
std::current_exception returns an empty exception pointer when called from the
terminate handler.

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC) 
$ cat y.cpp
#include <iostream>
#include <future>

void term_handler() {
        auto e = std::current_exception();
        if (e)
                std::cout << "Died with exception" << std::endl;
        else
                std::cout << "Died without exception" << std::endl;
        std::abort();
}

void bar() noexcept {
        try {
                throw "";
        }
        catch (int) { }
}

int main() {
        std::set_terminate(&term_handler);
        std::async(std::launch::async, &bar).get();
}

$ g++ y.cpp -pthread
$ ./a.out
Died without exception
Aborted (core dumped)
$ 


This happens regardless of whether launch::async or launch::deferred is used,
but does not happen if the function is called directly or is run by a
std::thread.

Reply via email to