DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 5BME5EER670383
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 5BME5EER670383
Authentication-Results: delorie.com;
	dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=YWS1FlSu
X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BCA44BA2E2A
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1766412314;
	bh=1MKU080ADDwz45/D2W7237TV3yORz0xzaf3aOVxghUU=;
	h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe:
	 List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
	 From;
	b=YWS1FlSuqyLaB2zG0Nu7ny4zUNtQzzjNnvJvx6Ry+pODp2S0O+xPvVy0wRIhcesgU
	 Ey7plWAOuMrAUQfAWmeGpLj3JL1jeK7P+yf9I69VKZ9ViN5EZ2mtsfBctAJil7umCL
	 30tvetqtRdoOiCcEOqNgxaCe+PCLKWcP/sFohud0=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC3264BA2E05
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BC3264BA2E05
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1766412292; cv=none;
 b=gTiyWjQYvGTa9N6pGQtm8BRGpaw6LJLqZWqcNiumcandvPhP5V+hfnNN5kHWQbmo3DC/ei3VW+/kAFN3uo1nV6dktPiQrF1O+M3G9l2yzfcUi7f7W+xHp6UEaB4fP4Ztsn0lM7xBjUwmkKnSfTkBpL8Ypk7qd1VoQY6JQf6wr78=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
 t=1766412292; c=relaxed/simple;
 bh=JF76IKNgEQaXtc496CNaD/8wVXu69bMY06K5KX18WXA=;
 h=Date:From:To:Subject:Message-Id:Mime-Version:DKIM-Signature;
 b=meP5lz2XfInuDU9AnA1p4vKEkHOf4b8otBDjx0QUko+T5Q/0f2mmIh3Ij6gPC51W3BZJ+lQUyHLJzHvSBbVG3HyuD2B/BfxvWEaDY+aFIpxtxWQfpfbGxkzSFRDhepVSinvNPRAubCf7JmlLpwby/HwHLaL57d/bZOoKb+wvldI=
ARC-Authentication-Results: i=1; server2.sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC3264BA2E05
Date: Mon, 22 Dec 2025 23:04:50 +0900
To: cygwin@cygwin.com
Subject: Re: Recent testsuite/winsup.api/pthread/cancel2 failure
Message-Id: <20251222230450.a2e308861cebfab9cdb5dcc1@nifty.ne.jp>
In-Reply-To: <20251217193737.3d3d686ebe3865c183315dc5@nifty.ne.jp>
References: <20251214162637.1ee05b084788ba073fe94670@nifty.ne.jp>
 <20251214163936.6841fc62145d8f54cfa31fe7@nifty.ne.jp>
 <9d058204-fb3f-43b4-9e32-6adc7af12ff4@dronecode.org.uk>
 <20251217193737.3d3d686ebe3865c183315dc5@nifty.ne.jp>
X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Mime-Version: 1.0
X-BeenThere: cygwin@cygwin.com
X-Mailman-Version: 2.1.30
Precedence: list
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-request@cygwin.com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
From: Takashi Yano via Cygwin <cygwin@cygwin.com>
Reply-To: Takashi Yano <takashi.yano@nifty.ne.jp>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: cygwin-bounces~archive-cygwin=delorie.com@cygwin.com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie.com@cygwin.com>

On Wed, 17 Dec 2025 19:37:37 +0900
Takashi Yano wrote:
> Hi Jon,
> 
> Thanks for the reply.
> 
> On Tue, 16 Dec 2025 13:11:15 +0000
> Jon Turney wrote:
> > On 14/12/2025 07:39, Takashi Yano via Cygwin wrote:
> > > On Sun, 14 Dec 2025 16:26:37 +0900
> > > Takashi Yano via Cygwin <cygwin@cygwin.com> wrote:
> > > 
> > >> Recently, I have concerned that testsuite winsup.api/pthread/cancel2 fails
> > >> consistently.
> > >>
> > >> https://github.com/cygwin/cygwin/actions/runs/19926408142/job/57127200619
> > 
> > Thanks very much for looking into this!
> > 
> > I have the vague idea that this problem started showing up (more?) when 
> > the CI VM was upgraded from Windows Server 2022 to Windows Server 2025, 
> > but I guess that's maybe just timings...
> 
> IIRC, this did not happen when I uses Win10. Now, I'm using Win11.
> 
> > >> I'm not sure why this happens, but it also falis in my local environment.
> > >> I looked into this issue a bit, and found that access violation happnes
> > >> in CloseHandle() in _cygtls::remove().
> > >>
> > >> And I am also not sure why at all, cancel2 works if CloseHandle()'s are
> > >> replaced with NtClose() as follows.
> > 
> > I think this is just the difference between the two calls: CloseHandle 
> > generates an exception whereas NtClose returns an error code if the 
> > handle is invalid.
> > 
> > Doesn't really explain whats wrong with the handle, though.
> 
> I checked the return code of NtClose() and found the it is STATUS_SUCCESS.
> If the handle is invalid, NtClose() returns error code, I think...

I worked on this issue, and found the following code solves the issue,
without "CloseHandle()/NtClose()" patch.

diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 86a00e76e..82d52f44f 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -630,6 +630,8 @@ pthread::cancel ()
       threadlist_t *tl_entry = cygheap->find_tls (cygtls);
       if (!cygtls->inside_kernel (&context))
 	{
+	  *(ULONG_PTR *) context._CX_stackPtr = context._CX_instPtr;
+	  context._CX_stackPtr -= sizeof (ULONG_PTR);
 	  context._CX_instPtr = (ULONG_PTR) pthread::static_cancel_self;
 	  SetThreadContext (win32_obj_id, &context);
 	}

But, I'm not sure why at all.

pthread::static_cancel_self() never returns, so stack should not affect,
I think.

Any idea?

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple
