Skip to content

Cygwin: console: fix deadlock in pcon teardown#339

Open
lazka wants to merge 1 commit into
msys2:msys2-3.6.9from
lazka:shutdown-hang
Open

Cygwin: console: fix deadlock in pcon teardown#339
lazka wants to merge 1 commit into
msys2:msys2-3.6.9from
lazka:shutdown-hang

Conversation

@lazka
Copy link
Copy Markdown
Member

@lazka lazka commented May 18, 2026

When a process inside a pseudo console (pcon) exits, the sequence was:

  exit()
    -> atexit handlers (including pcon_hand_over_proc)
         pcon_hand_over_proc closes parent_pty_input_mutex
         -- handle slot now free to be recycled --
    -> _exit() -> do_exit() -> close_all_files() -> fhandler_console::close()
         set_input_mode() waits on input_mutex  <-- hangs

The fix, suggested by Takashi Yano: remove the atexit registration entirely and instead call pcon_hand_over_proc() directly from fhandler_console::close(), after both input_mutex and output_mutex have been closed. By that point close() has already waited for cons_master_thread to exit, so parent_pty_input_mutex is no longer accessed by any other thread and can be safely closed.

Fixes: #338

When a process inside a pseudo console (pcon) exits, the sequence was:

  exit()
    -> atexit handlers (including pcon_hand_over_proc)
         pcon_hand_over_proc closes parent_pty_input_mutex
         -- handle slot now free to be recycled --
    -> _exit() -> do_exit() -> close_all_files() -> fhandler_console::close()
         set_input_mode() waits on input_mutex  <-- hangs

The fix, suggested by Takashi Yano: remove the atexit registration
entirely and instead call pcon_hand_over_proc() directly from
fhandler_console::close(), after both input_mutex and output_mutex
have been closed.  By that point close() has already waited for
cons_master_thread to exit, so parent_pty_input_mutex is no longer
accessed by any other thread and can be safely closed.

Fixes: msys2#338
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Process shutdown may hang due to handle number reuse in winsup/cygwin/fhandler/console.cc

1 participant