Re: [PATCH] Cygwin: console: Fix deadlock in console teardown that arises from pcon

Johannes Schindelin <[email protected]> Thu, 28 May 2026 15:38:46 +0200 (CEST)
Newsgroups gmane.os.cygwin.patches
Message-ID <[email protected]>
Hi Takashi,

On Fri, 22 May 2026, Takashi Yano wrote:

> When a console process originating from a pseudo console exits, the
> current sequence is as follows:
> 
>   1) atexit handlers (pcon_handover_proc) called. This also closes
>      parent_pty_input_mutex which is introduced by the commit
>      c4fb720afcf1.
>   2) close_all_files() is called via _exit(). This terminates
>      cons_master_thread.
> 
> parent_pty_input_mutex is referenced in cons_master_thread, so
> cons_master_thread may still use the mutex after it has been closed.
> This can lead to undesired behaviour, including a deadlock. Instead
> of registering pcon_hand_over_proc() as an atexit handler, this
> patch calls pcon_handover_proc() at a point in fhandler_console::close
> where cons_master_thread has already terminated, ensuring that no
> other thread accesses the mutex.

Thank you so much for this excellent commit message, which motivates the
patch well and preempts all the questions I would have asked about the
code changes.

The entire patch looks good to me!

Thanks,
Johannes

> 
> Addresses: https://github.com/msys2/msys2-runtime/issues/338
> Fixes: c4fb720afcf1 ("Cygwin: console: Use input_mutex in the parent PTY in master thread")
> Signed-off-by: Takashi Yano <[email protected]>
> Reviewed-by:
> ---
>  winsup/cygwin/fhandler/console.cc | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
> index c76347f6f..6fd4cd965 100644
> --- a/winsup/cygwin/fhandler/console.cc
> +++ b/winsup/cygwin/fhandler/console.cc
> @@ -2018,7 +2018,6 @@ fhandler_console::setup_pcon_hand_over ()
>  	if (get_console_process_id (owner, true, false, false, false))
>  	  {
>  	    inside_pcon = true;
> -	    atexit (fhandler_console::pcon_hand_over_proc);
>  	    parent_pty = i;
>  	    parent_pty_input_mutex =
>  	      cygwin_shared->tty[i]->open_input_mutex (MAXIMUM_ALLOWED);
> @@ -2157,6 +2156,8 @@ fhandler_console::close (int flag)
>    CloseHandle (output_mutex);
>    output_mutex = NULL;
>  
> +  pcon_hand_over_proc ();
> +
>    WaitForSingleObject (shared_info_mutex, INFINITE);
>    if (--shared_info_state[unit] == 0 && shared_console_info[unit])
>      {
> -- 
> 2.51.0
> 
>