This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Commit 29d5555
authored
Fix Console.CancelKeyPress deadlock (#28286)
If the CancelKeyPress handler tries to unregister itself, it can deadlock on Windows. This is because there's a lock that's held by Windows while invoking the callback, and the .NET logic for handling that callback is queueing the actual user callback to run on a ThreadPool thread and is then blocking waiting for that queued work to complete. So the calling thread that holds a lock is blocked waiting for another thread to run the user callback. If that user callback then tries to unregister the callback, it ends up trying to take the same lock that's already held by the other thread, and we deadlock.
The simple fix is to stop queueing the work item and to just run it on the same thread. The code that does the queueing goes back over a decade, and was added during a phase where we were trying to ensure there was a ton of stack space available, but a) even then there were hundreds of kb of stack space available, which is plenty, and b) that effort was abandoned.
(There is a small risk here for a corner case. The code currently has a timeout, where if the queued work item hasn't completed within 30 seconds, it unblocks the calling thread (but leaves the queued work item running indefinitely). With this change, we no longer have the work item and thus no longer have the timeout, but it is theoretically conceivable that someone somewhere was relying on the 30 second timeout.)1 parent 2977973 commit 29d5555
1 file changed
+5
-56
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
688 | 688 | | |
689 | 689 | | |
690 | 690 | | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
695 | | - | |
696 | | - | |
697 | | - | |
698 | | - | |
699 | | - | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
706 | | - | |
707 | | - | |
708 | | - | |
709 | | - | |
710 | | - | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
716 | | - | |
717 | 691 | | |
718 | 692 | | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
| 693 | + | |
| 694 | + | |
746 | 695 | | |
747 | | - | |
748 | 696 | | |
749 | 697 | | |
750 | 698 | | |
751 | | - | |
752 | | - | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
753 | 702 | | |
754 | 703 | | |
755 | 704 | | |
0 commit comments