Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Terminal Window Does Not Send EVENT_OBJECT_DESTROY On Close #17298

Open
LGUG2Z opened this issue May 22, 2024 · 6 comments
Open

Windows Terminal Window Does Not Send EVENT_OBJECT_DESTROY On Close #17298

LGUG2Z opened this issue May 22, 2024 · 6 comments
Assignees
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting

Comments

@LGUG2Z
Copy link

LGUG2Z commented May 22, 2024

Windows Terminal version

1.19.11213.0

Windows build number

10.0.22631.0

Other Software

No response

Steps to reproduce

Open a single instance of Windows terminal on the desktop. Close that instance of Windows Terminal. EVENT_OBJECT_DESTROY is not emitted.

Expected Behavior

EVENT_OBJECT_DESTROY should be emitted when the Windows Terminal window is closed by the window handle that the user has just interacted with.

Actual Behavior

EVENT_OBJECT_DESTROY is not emitted for the Windows Terminal window that was closed.

When opening Windows Terminal, EVENT_SYSTEM_FOREGROUND is emitted, and the window handle of the actual window that I as the user interact with is the log example below is 658858:

2024-05-22T01:39:16.367526Z  INFO process_event{event=FocusChange(SystemForeground, Window { hwnd: 658858 })}: komorebi::process_event: processed: (hwnd: 658858, title: PowerShell, exe: WindowsTerminal.exe, class: CASCADIA_HOSTING_WINDOW_CLASS)

When I close the window, sometimes an EVENT_OBJECT_HIDE is emitted, but it is emitted for a completely separate window handle (920252):

2024-05-22T01:39:18.051931Z DEBUG process_event{event=Hide(ObjectHide, Window { hwnd: 920252 })}:should_manage{self=Window { hwnd: 920252 } event=Some(Hide(ObjectHide, Window { hwnd: 920252 }))}: komorebi::window: ignoring (exe: WindowsTerminal.exe, title: DesktopWindowXamlSource, event: Hide (WinEvent: ObjectHide, Window: (hwnd: 920252)))
@LGUG2Z LGUG2Z added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels May 22, 2024
LGUG2Z added a commit to LGUG2Z/komorebi that referenced this issue May 22, 2024
Until now the orphan window/container reaper has always run on every
WinEvent. Unfortunately Windows Terminal does not sent a WinEvent when
it is closed.

This is a problem for the new border_manager module which draws and
destroys borders based on notifications sent to it after WinEvents and
SocketMessages have been processed.

Since Windows Terminal is not sending a WinEvent on close, this means
that user interaction is required to remove the ghost border that gets
left behind.

This commit starts a separate thread for the reaper where it runs once
every second in a loop.

This is quite wasteful and definitely not something I wanted to
implement, but a temporary solution is needed given the popularity of
the buggy application in question.

An issue on the Windows Terminal tracker has been opened here:
microsoft/terminal#17298
LGUG2Z added a commit to LGUG2Z/komorebi that referenced this issue May 22, 2024
Until now the orphan window/container reaper has always run on every
WinEvent. Unfortunately Windows Terminal does not sent a WinEvent when
it is closed.

This is a problem for the new border_manager module which draws and
destroys borders based on notifications sent to it after WinEvents and
SocketMessages have been processed.

Since Windows Terminal is not sending a WinEvent on close, this means
that user interaction is required to remove the ghost border that gets
left behind.

This commit starts a separate thread for the reaper where it runs once
every second in a loop.

This is quite wasteful and definitely not something I wanted to
implement, but a temporary solution is needed given the popularity of
the buggy application in question.

An issue on the Windows Terminal tracker has been opened here:
microsoft/terminal#17298
@lhecker
Copy link
Member

lhecker commented May 27, 2024

I'm somewhat surprised that the OS doesn't do that for us. We use TerminateProcess on exit to work around issues in WinUI (in particular on Windows 10). It's a simple and effective solution.

While I can see how we should try to emit these events on exit, I'm not sure whether that would help your project at all. A lot of applications, and not just Windows Terminal, use TerminateProcess and I wonder if the same issue will happen if you kill a process with the task manager. Since TerminateProcess is an entirely valid way to exit an application I'm not sure whether calling Windows Terminal buggy is fair.
But in any case, I suppose you'd need to make your window manager robust against sudden process exits independent of whether we address this issue or not.

@LGUG2Z
Copy link
Author

LGUG2Z commented May 27, 2024

Updates have already been made on our side to protect against any other applications which might exhibit this kind of non-standard WinEvent behaviour in the future (referenced commit above).

I wonder if the same issue will happen if you kill a process with the task manager

I can confirm this is not the case; applications killed from the Task Manager send EVENT_OBJECT_DESTROY.

A lot of applications, and not just Windows Terminal, use TerminateProcess

komorebi users are pretty quick and vocal to report issues like these, and with tens of thousands of users across a variety of professions using a breadth of both modern and legacy applications, Windows Terminal is the only application which has been reported to exhibit this behaviour.

If and when there are more examples from other applications I will be happy to share them, but for now, if 99% of other applications windows are doing the right thing (sending the appropriate WinEvent when they are destroyed), I think it's worth trying to bring WT into conformance with the expected application behaviour in the Windows ecosystem.

@lhecker
Copy link
Member

lhecker commented May 27, 2024

I've hacked this C++ snippet together as a test case for this issue: https://gist.github.com/lhecker/82b029643e9f9ccf15304a623093880b
For any given application name, it records a set of HWNDs. They're added to the set on EVENT_OBJECT_CREATE and removed on EVENT_OBJECT_DESTROY. Currently, when opening/closing the window the counter for WindowsTerminal.exe will constantly increase.

However, I'm doing something wrong in that code, because the same thing happens for VS Code and other applications:

AW5Sm4xuRzrM4pUi.mp4

Since it happens with pretty much all applications, I must be doing something wrong but I can't figure out what. @LGUG2Z I don't mean to bother you unnecessarily, but if you can spot any flaws with what I described above, or any flaws in my code, please let me know. 🙂

@lhecker
Copy link
Member

lhecker commented May 27, 2024

Just after writing that comment, I finally figured out the first mistake I made! Apparently, when a EVENT_OBJECT_DESTROY is received, OpenProcess may not work anymore. In my first version I did an early return in that case.

I've now changed the code to instead search through all windows and that gets VS Code down to just 0 to 1 HWND leaks per launch. But that's the same amount as for Windows Terminal. Weird. Am I checking some of the callback parameters incorrectly? 🤔

@LGUG2Z
Copy link
Author

LGUG2Z commented May 27, 2024

Unfortunately I'm not well versed enough with CPP to offer much meaningful insight, but I created something similar using windows-rs here: https://gist.github.com/LGUG2Z/da57d4f1fb294266e3793348b149fb79

[src\main.rs:126:5] state = {
    "C:\\Windows\\explorer.exe": 3,
    "C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminal_1.19.11213.0_x64__8wekyb3d8bbwe\\WindowsTerminal.exe": 2,
    "C:\\Windows\\SystemApps\\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\\StartMenuExperienceHost.exe": 5,
}

I'm tracking the state in a hashmap keyed by the exe path, incrementing by 1 whenever a create event comes through, decrementing by 1 whenever a destroy event comes through, and removing from the map when we drop to 0.

For when OpenProcess calls might fail, there is another hashmap keyed with the HWND to look up the exe path if the API call fails.

With this I can see that there are a few lingering create events between opening and closing instances of WT that don't have corresponding destroy events, and I'm betting these are for the visible window that interaction happens with 🤔

@carlos-zamora carlos-zamora added the Needs-Attention The core contributors need to come back around and look at this ASAP. label May 29, 2024
@carlos-zamora carlos-zamora added this to the Terminal v1.22 milestone Jun 5, 2024
@zadjii-msft
Copy link
Member

Thoughts:

  • Is this just a XAML islands thing? I think PerceptionSimulationInput (or something like that) in system32 is also XAML islands, but this might be gone in certain win11 builds.
  • Is this a RuntimeBroker thing? maybe if we're the defterm, the monarch process is still running?
  • Maybe the tray icon is still present (but not cleaned up cause {{bug number here}}, and that's why we're not having that event emitted?
  • Does this repro with the default terminal settings? allowHeadless, minimizeToNotificationArea, and _quake mode come to mind

we honestly don't know! We should find someone who does

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting
Projects
None yet
Development

No branches or pull requests

4 participants