From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id A0F313857C73 for ; Tue, 18 Aug 2020 08:43:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A0F313857C73 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-485-k61a_uG5M2CGt8II7XFksw-1; Tue, 18 Aug 2020 04:43:33 -0400 X-MC-Unique: k61a_uG5M2CGt8II7XFksw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C06C6185E547; Tue, 18 Aug 2020 08:43:32 +0000 (UTC) Received: from localhost (unknown [10.33.36.183]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44F5810098A5; Tue, 18 Aug 2020 08:43:32 +0000 (UTC) Date: Tue, 18 Aug 2020 09:43:31 +0100 From: Jonathan Wakely To: Lewis Hyatt Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH] libstdc++: testsuite: Address random failure in pthread_create() [PR54185] Message-ID: <20200818084331.GD3400@redhat.com> References: <20200813221536.GA51547@ldh-imac.local> MIME-Version: 1.0 In-Reply-To: <20200813221536.GA51547@ldh-imac.local> X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="GJ7e10BhKqIkML+d" Content-Disposition: inline X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Aug 2020 08:43:43 -0000 --GJ7e10BhKqIkML+d Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On 13/08/20 18:15 -0400, Lewis Hyatt via Libstdc++ wrote: >Hello- > >The attached patch was discussed briefly on PR 54185 here: >https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54185#c14 >The test case for this PR sometimes fails due to random failures in >pthread_create() that are not related to the original PR. This patch fixes >it up by ignoring those failures. The test case was designed to repeat the >same test 1000 times to attempt to reproduce a race condition, so I think is >OK if some of those iterations are simply skipped. > >Thanks for taking a look at it; I can commit it if it makes sense. > >-Lewis >libstdc++: testsuite: Address random failure in pthread_create() [PR54185] > >The test for this PR calls pthread_create() many times in a row, which may fail >with EAGAIN sometimes. Avoid generating a test failure in this case. > >libstdc++-v3/ChangeLog: > > PR libstdc++/54185 > * testsuite/30_threads/condition_variable/54185.cc: Make test robust > to random pthread_create() failures. Thanks for the patch. It certainly looks reasonable, but I wonder if the attached version wouldn't be (very slightly) better. The difference is that instead of just giving up at the first EAGAIN we keep trying. This way we might be able to create a few more threads before the loop finishes. If we still keep failing, it works the same. I've also added a check that the failures are due to EAGAIN, and we'll still terminate if there's some other problem. I'm assuming that your failures are EAGAIN. Do you know why that's happening? Does your system a low value for RLIMIT_NPROC or something? The failures for that testcase on AIX appear to be different. It just segfaults after destroying the condition_variable, which probably means there's a POSIX conformance issue in AIX's pthread_cond_t. --GJ7e10BhKqIkML+d Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc index ea0d5bb8740..8ccb79e6de6 100644 --- a/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc +++ b/libstdc++-v3/testsuite/30_threads/condition_variable/54185.cc @@ -31,31 +31,48 @@ std::condition_variable* cond = nullptr; std::mutex mx; int started = 0; +bool notified = false; int constexpr NUM_THREADS = 10; -void do_thread_a() +void do_thread_a(bool wait) { std::unique_lock lock(mx); - if(++started >= NUM_THREADS) + if (++started >= NUM_THREADS) { + notified = true; cond->notify_all(); delete cond; cond = nullptr; } - else - cond->wait(lock); + else if (wait) + cond->wait(lock, [] { return notified; }); } -int main(){ +int main() +{ std::vector vec; - for(int j = 0; j < 1000; ++j) + for (int j = 0; j < 1000; ++j) { started = 0; + notified = false; cond = new std::condition_variable; for (int i = 0; i < NUM_THREADS; ++i) - vec.emplace_back(&do_thread_a); - for (int i = 0; i < NUM_THREADS; ++i) - vec[i].join(); + { + try + { + vec.emplace_back(&do_thread_a, true); + } + catch(const std::system_error& e) + { + if (e.code() == std::errc::resource_unavailable_try_again) + // Thread creation may fail due to resource limits; run serially. + do_thread_a(false); + else + throw; + } + } + for (auto& thread : vec) + thread.join(); vec.clear(); } } --GJ7e10BhKqIkML+d--