Fix deadlock when closing on Windows using Conpty

Fixes #3042.
This commit is contained in:
David Hewitt 2019-12-12 15:01:23 +00:00 committed by Christian Duerr
parent 4d3f6de41a
commit 6deb274b82
4 changed files with 20 additions and 0 deletions

View File

@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Minimize on windows causing layout issues
- Performance bottleneck when clearing colored rows
- Vague startup crash messages on Windows with WinPTY backend
- Deadlock on Windows when closing Alacritty using the title bar "X" button (ConPTY backend)
## 0.4.0

View File

@ -222,6 +222,19 @@ fn run(window_event_loop: GlutinEventLoop<Event>, config: Config) -> Result<(),
// Start event loop and block until shutdown
processor.run(terminal, window_event_loop);
// This explicit drop is needed for Windows, ConPTY backend. Otherwise a deadlock can occur.
// The cause:
// - Drop for Conpty will deadlock if the conout pipe has already been dropped.
// - The conout pipe is dropped when the io_thread is joined below (io_thread owns pty).
// - Conpty is dropped when the last of processor and io_thread are dropped, because both of
// them own an Arc<Conpty>.
//
// The fix is to ensure that processor is dropped first. That way, when io_thread (i.e. pty)
// is dropped, it can ensure Conpty is dropped before the conout pipe in the pty drop order.
//
// FIXME: Change PTY API to enforce the correct drop order with the typesystem.
drop(processor);
// Shutdown PTY parser event loop
loop_tx.send(Msg::Shutdown).expect("Error sending shutdown to pty event loop");
io_thread.join().expect("join io thread");

View File

@ -90,6 +90,10 @@ pub type ConptyHandle = Arc<Conpty>;
impl Drop for Conpty {
fn drop(&mut self) {
// XXX: This will block until the conout pipe is drained. Will cause a deadlock if the
// conout pipe has already been dropped by this point.
//
// See PR #3084 and https://docs.microsoft.com/en-us/windows/console/closepseudoconsole
unsafe { (self.api.ClosePseudoConsole)(self.handle) }
}
}

View File

@ -45,6 +45,8 @@ pub enum PtyHandle {
}
pub struct Pty {
// XXX: Handle is required to be the first field, to ensure correct drop order. Dropping
// `conout` before `handle` will cause a deadlock.
handle: PtyHandle,
// TODO: It's on the roadmap for the Conpty API to support Overlapped I/O.
// See https://github.com/Microsoft/console/issues/262