X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87CC3386F80F
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1593804150;
	bh=1loJtPtINUUewVkyaPzfGPlJi6PzWxBQ9zVi0Vr5o2g=;
	h=Date:From:To:Subject:References:In-Reply-To:List-Id:
	 List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe:
	 Reply-To:From;
	b=ycMhQADUxgUv+1oFhceAwc+UiCD+gE/KNHPNLlY9bj11/5BMOOgxWIMc9kp0k2apX
	 dYTNG9NMsj6qStAgqaufREwhNrL/PFVBupHaFpURHn9Q77RemiYawkNw3gs/BAkGO6
	 H3Rbtpk1yQU6iY2sXOSIk80JrtL6kxlTY9YC6QtE=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C37A63844079
Authentication-Results: sourceware.org;
 dmarc=none (p=none dis=none) header.from=cygwin.com
Authentication-Results: sourceware.org;
 spf=fail smtp.mailfrom=corinna-cygwin@cygwin.com
Date: Fri, 3 Jul 2020 21:22:25 +0200
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: SIGINT lost while program calls PeekMessage
Message-ID: <20200703192225.GB3499@calimero.vinschen.de>
Mail-Followup-To: cygwin@cygwin.com
References: <01ea01d6508c$1a340a70$4e9c1f50$@beniston.com>
MIME-Version: 1.0
Content-Disposition: inline
In-Reply-To: <01ea01d6508c$1a340a70$4e9c1f50$@beniston.com>
X-Provags-ID: V03:K1:0whFys+1EQIxQDkB6NvRje/vf24M2LG1TFHlEs55XtYrS3CSEs2
 KdmWfsPZkIkQuuq0ajy+xAbwJ2xpRfIL+gBV1ZnNLhT/hheFhe39FLH9YdOdXhpWKiRrct0
 /Iz+3mEgM2/e5PGIa6lWgAPy5OWN7R+/oxSm0nr1JApUstbxNQmPNl7P4VR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US, RCVD_IN_DNSWL_NONE,
 RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL,
 TXREP autolearn=ham autolearn_force=no version=3.4.2
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on
 server2.sourceware.org
X-BeenThere: cygwin@cygwin.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <http://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: <http://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
Reply-To: cygwin@cygwin.com
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: cygwin-bounces@cygwin.com
Sender: "Cygwin" <cygwin-bounces@cygwin.com>

On Jul  2 17:16, Jon Beniston wrote:
> Hi,
> 
> I have a Cygwin program that:
> 
> - registers a SIGINT handler, via signal(SIGINT,h)
> - creates a window, using Win32 CreateWindow
> - and is then calling PeekMessage for that window in a loop
> 
> It appears that while PeekMessage is being called, any SIGINTs sent to the
> program are lost. Is this to be expected as it's not really supported or a
> bug?
> 
> To reproduce:
> 
> - Compile and run the attached example program, and repeatedly press CTRL-C
> in the console. 
> - Only maybe 1 of every 10 or 20 times CTRL-C is pressed, is the signal
> handler called.
> - Similarly, running 'kill -SIGINT PID' results in most of the signals being
> ignored.
> - Remove the call to PeekMessage, and the signals are received. 

The cause of the lost signals is the fact that PeekMessage is called in
a very tight busy loop(*).

As soon as you ease the pain by adding a usleep(1) practically all
signals will arrive(**).

The problem is this: Every process has a so-called "wait_sig" thread.
This thread waits for signals and when it gets one, it tries to process
it.  To be able to call a user-space signal handler, the thread supposed
to handle this signal (here: the main thread) has to be interruptible.

Threads are not interruptible while they are running in a Windows system
DLL (like, e.g., kernel32.dll).

Since your example code is running very tightly most of its time inside
some Windows system DLL function, there's next to no chance to interrupt
the thread to inject the signal handler call.  Cygwin tries this 100
times per signal.  If that fails, the signal gets lost.

One way to solve this problem is adding a Cygwin call into the loop,
like the aforementioned usleep.  When a Cygwin call is performed, the
signal handler will be called as soon as the Cygwin call returns to
user-space.

The bottom line is, Cygwin user space signal handlers and lots of
Windows-only calls in a tight loop don't work nicely together.


Corinna

(*)  Which is a no-no.  Better use a thread calling GetMessage or
     MsgWaitForMultipleObjectsEx in a loop.

(**) "Practically", because every signal is only queued once yet.
     That is, if a SIGINT is queued and another SIGINT arrives before
     the first SIGINT got dequeued, the second signal is ignored.

-- 
Corinna Vinschen
Cygwin Maintainer
--
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
