Skip to content

Commit

Permalink
Fire and forget Hyperlink handling to break deadlock (#8087)
Browse files Browse the repository at this point in the history
Fire and forget on the hyperlink handler inside the TermControl. 

## PR Checklist
* [x] Closes #7994 
* [x] Tested manually
* [x] Hi, I work here.

## Detailed Description of the Pull Request / Additional comments
In `TermControl`, `_HyperlinkHandler` is called by
`_PointerPressedHandler` which has taken a write lock for all its
friends. However, `_HyperlinkHandler` downstreams to `ShellExecute`
which can pump the message queue looking for something. That pumping of
the queue can trigger messages that also want the write lock to update
state. They get stuck. Everything hangs. 

`_HyperlinkHandler` really only needs read lock and really only for as
long as it takes to fill up its parameters before it's invoked... but
the simpler and more contained solution is to just fire and forget the
rest of the method that causes the deadlock to a continuation at the
tail of the dispatcher queue so `_PointerPressedHandler` can complete
and naturally drop the write lock.

## Validation Steps Performed
- Launched `main` manually on my box and clicked the hyperlink that is
  detected when Powershell starts and it froze.
- Launched this change manually on my box and clicked the hyperlink that
  is detected when Powershell starts and it did not freeze.
  • Loading branch information
miniksa authored Oct 29, 2020
1 parent 4daed9d commit 2ea4742
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3012,10 +3012,20 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - Checks if the uri is valid and sends an event if so
// Arguments:
// - The uri
void TermControl::_HyperlinkHandler(const std::wstring_view uri)
winrt::fire_and_forget TermControl::_HyperlinkHandler(const std::wstring_view uri)
{
auto hyperlinkArgs = winrt::make_self<OpenHyperlinkEventArgs>(winrt::hstring{ uri });
_openHyperlinkHandlers(*this, *hyperlinkArgs);
// Save things we need to resume later.
winrt::hstring heldUri{ uri };
auto strongThis{ get_strong() };

// Pop the rest of this function to the tail of the UI thread
// Just in case someone was holding a lock when they called us and
// the handlers decide to do something that take another lock
// (like ShellExecute pumping our messaging thread...GH#7994)
co_await Dispatcher();

auto hyperlinkArgs = winrt::make_self<OpenHyperlinkEventArgs>(heldUri);
_openHyperlinkHandlers(*strongThis, *hyperlinkArgs);
}

// Method Description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void _LostFocusHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);
winrt::fire_and_forget _DragDropHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::DragEventArgs const e);
void _DragOverHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::DragEventArgs const& e);
void _HyperlinkHandler(const std::wstring_view uri);
winrt::fire_and_forget _HyperlinkHandler(const std::wstring_view uri);

void _CursorTimerTick(Windows::Foundation::IInspectable const& sender, Windows::Foundation::IInspectable const& e);
void _BlinkTimerTick(Windows::Foundation::IInspectable const& sender, Windows::Foundation::IInspectable const& e);
Expand Down

0 comments on commit 2ea4742

Please sign in to comment.