diff --git a/src/cascadia/TerminalApp/Pane.cpp b/src/cascadia/TerminalApp/Pane.cpp index e875622dea7..cb8f4278a76 100644 --- a/src/cascadia/TerminalApp/Pane.cpp +++ b/src/cascadia/TerminalApp/Pane.cpp @@ -329,6 +329,7 @@ void Pane::_ControlConnectionStateChangedHandler(const winrt::Windows::Foundatio } const auto newConnectionState = _control.ConnectionState(); + const auto previousConnectionState = std::exchange(_connectionState, newConnectionState); if (newConnectionState < ConnectionState::Closed) { @@ -336,6 +337,14 @@ void Pane::_ControlConnectionStateChangedHandler(const winrt::Windows::Foundatio return; } + if (previousConnectionState < ConnectionState::Connected && newConnectionState >= ConnectionState::Failed) + { + // A failure to complete the connection (before it has _connected_) is not covered by "closeOnExit". + // This is to prevent a misconfiguration (closeOnExit: always, startingDirectory: garbage) resulting + // in Terminal flashing open and immediately closed. + return; + } + const auto settings{ winrt::TerminalApp::implementation::AppLogic::CurrentAppSettings() }; auto paneProfile = settings.FindProfile(_profile.value()); if (paneProfile) @@ -709,6 +718,7 @@ void Pane::_CloseChild(const bool closeFirst) // take the control, profile and id of the pane that _wasn't_ closed. _control = remainingChild->_control; + _connectionState = remainingChild->_connectionState; _profile = remainingChild->_profile; _id = remainingChild->Id(); @@ -1476,6 +1486,7 @@ std::pair, std::shared_ptr> Pane::_Split(SplitState // Move our control, guid into the first one. // Move the new guid, control into the second. _firstChild = std::make_shared(_profile.value(), _control); + _firstChild->_connectionState = std::exchange(_connectionState, ConnectionState::NotConnected); _profile = std::nullopt; _control = { nullptr }; _secondChild = std::make_shared(profile, control); diff --git a/src/cascadia/TerminalApp/Pane.h b/src/cascadia/TerminalApp/Pane.h index e8344fef893..6e7f801b787 100644 --- a/src/cascadia/TerminalApp/Pane.h +++ b/src/cascadia/TerminalApp/Pane.h @@ -95,6 +95,7 @@ class Pane : public std::enable_shared_from_this winrt::Windows::UI::Xaml::Controls::Grid _root{}; winrt::Windows::UI::Xaml::Controls::Border _border{}; winrt::Microsoft::Terminal::Control::TermControl _control{ nullptr }; + winrt::Microsoft::Terminal::TerminalConnection::ConnectionState _connectionState{ winrt::Microsoft::Terminal::TerminalConnection::ConnectionState::NotConnected }; static winrt::Windows::UI::Xaml::Media::SolidColorBrush s_focusedBorderBrush; static winrt::Windows::UI::Xaml::Media::SolidColorBrush s_unfocusedBorderBrush; diff --git a/src/cascadia/TerminalConnection/ConptyConnection.cpp b/src/cascadia/TerminalConnection/ConptyConnection.cpp index 036dccb4957..a5666f782f9 100644 --- a/src/cascadia/TerminalConnection/ConptyConnection.cpp +++ b/src/cascadia/TerminalConnection/ConptyConnection.cpp @@ -251,6 +251,8 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation void ConptyConnection::Start() try { + _transitionToState(ConnectionState::Connecting); + if (!_inPipe) { const COORD dimensions{ gsl::narrow_cast(_initialCols), gsl::narrow_cast(_initialRows) };