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

ColorPicker and PT Run take 10 seconds to exit #5860

Closed
enricogior opened this issue Aug 11, 2020 · 17 comments
Closed

ColorPicker and PT Run take 10 seconds to exit #5860

enricogior opened this issue Aug 11, 2020 · 17 comments
Assignees
Labels
Issue-Bug Something isn't working Priority-0 Bugs that we consider release-blocking/recall-class (P0) Product-Color Picker All things around the Color Picker utility Product-PowerToys Run Improved app launch PT Run (Win+R) Window Resolution-Fix Committed Fix is checked in, but it might be 3-4 weeks until a release.
Milestone

Comments

@enricogior
Copy link
Contributor

enricogior commented Aug 11, 2020

ℹ Computer information

  • PowerToys version: 0.20.1
  • PowerToy module: ColorPicker, PT Run

📝 Provide detailed reproduction steps (if any)

  1. Turn off ColorPicker or PT Run in the Settings

✔️ Expected result

ColorPIcker.exe and PowerLauncher.exe should quit immediately

❌ Actual result

It takes 10 seconds for ColorPIcker.exe or PowerLauncher.exe to quit

Note: the terminateProcess() function:

void terminateProcess()
{
DWORD processID = GetProcessId(m_hProcess);
EnumWindows(&requestMainWindowClose, processID);
const DWORD result = WaitForSingleObject(m_hProcess, MAX_WAIT_MILLISEC);
if (result == WAIT_TIMEOUT)
{
TerminateProcess(m_hProcess, 1);
}
}

always ends up killing the process:
TerminateProcess(m_hProcess, 1);

@enricogior enricogior added the Issue-Bug Something isn't working label Aug 11, 2020
@ghost ghost added the Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams label Aug 11, 2020
@enricogior enricogior added Product-Color Picker All things around the Color Picker utility Product-PowerToys Run Improved app launch PT Run (Win+R) Window and removed Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams labels Aug 11, 2020
@dsrivastavv
Copy link
Contributor

@enricogior The issue here seems to be because pt run isn't able to cleanly terminate itself. This codepath was added in #4472 because we were earlier using TerminateProcess to stop powerlauncher.exe which caused immediate termination of the process and no exit callbacks were being invoked. To fix this, we replaced it with sending WM_CLOSE signal to powerlauncher.exe process and give it 10 sec to cleanly terminate itself, otherwise, we force kill it using TerminateProcess.

@arjunbalgovind
Copy link
Contributor

@enricogior I will be looking into running the PowerLauncher.exe process as admin (for #4427) and launching apps from within that non-elevated (unless the RunAsAdmin context menu button is pressed). Would this delay to exit still be problematic for Restart as admin if we make that change?

@enricogior
Copy link
Contributor Author

@somil55
the problem here is that PowerLauncher.exe never exits by itself, and 100% of the times it gets killed, so we either fix the code to do a clean exit or we simply go back to kill it right away.

@arjunbalgovind
yes, the delay is still problematic and needs to be fixed. If it can't be fixed in PowerLauncher.exe, we need to revisit the "restart as admin" code since it will have to wait until all modules quits before starting the new process.

@arjunbalgovind
Copy link
Contributor

arjunbalgovind commented Aug 11, 2020

@enricogior can you point me to the code where we terminate and relaunch PowerLauncher.exe when Restart as admin is pressed? If it is the same logic within the Launcher::enabled() method, we could look into either force terminating in that scenario or adding a second thread to wait until termination and then relaunch the process, so that we don't block the runner thread.

@enricogior
Copy link
Contributor Author

@arjunbalgovind
the logic is in Launcher::enabled(), but the first problem to fix is that the current disable logic is not working and PT Run gets killed after the timeout expires.
There are two scenarios here:
1 - the WM_CLOSE event is not received/processed by PT Run
2 - PT Run processes the event but it takes forever to quit

Also the event should not be sent to all PT Run windows

if (windowPid == (DWORD)closePid)
::PostMessage(nextWindow, WM_CLOSE, 0, 0);

but only to the main one and then return false.
And we need to verify if that code does actually work for applications that don't have a visible window.

@yuyoyuppe
Copy link
Contributor

yuyoyuppe commented Aug 26, 2020

Launcher is always terminated for me when its window is hidden. EnumWindows isn't able to communicate with the launcher process while its window is hidden. I've commented out the hiding logic when the window loses focus and couldn't reproduce the issue anymore. Otherwise, launcher never terminates for me (tried with INFINITE timout before terminate call) when disabling/exiting PT.

I suggest we use named event for that instead of relying on WM_CLOSE.

Another thing, we already react to the process powertoys.exe exit, and rely solely on that technique for ColorPicker/FZE/.../, so I wonder if we should remove the terminateProcess call there, and send event only when we disable the module.

@arjunbalgovind
Copy link
Contributor

We should also make sure the approach we go for is non-blocking in runner to avoid the issue described in #6676. (Runner was getting blocked on the WaitForSingleObject call)

@enricogior
Copy link
Contributor Author

@arjunbalgovind
thanks for info.

@crutkas crutkas added the Priority-0 Bugs that we consider release-blocking/recall-class (P0) label Sep 25, 2020
@crutkas crutkas added this to the InVEST-2010 milestone Sep 25, 2020
@enricogior enricogior added the Status-In progress This issue or work-item is under development label Sep 30, 2020
@crutkas
Copy link
Member

crutkas commented Oct 21, 2020

@enricogior / @arjunbalgovind / @ivan100sic / @yuyoyuppe are we viewing this work item as done?

@yuyoyuppe
Copy link
Contributor

can't repro on a recent master

@enricogior
Copy link
Contributor Author

enricogior commented Oct 21, 2020

@crutkas
for now it's fixed with a workaround: we kill the process right away instead of waiting 10 seconds and then kill it anyway.
Clean exit in PT Run wasn't working for unknown reason and a brief investigation wasn't enough. @ivan100sic may have more details.

@crutkas
Copy link
Member

crutkas commented Oct 26, 2020

@ivan100sic thoughts?

@ivan100sic
Copy link
Contributor

It definitely has to do with C# code of PT Run, not the PowerToy interface or dllmain. When we have better understanding of PT Run itself, I guess then we'll be able to do a clean exit.

@crutkas
Copy link
Member

crutkas commented Oct 27, 2020

so based on @yuyoyuppe the current workarounds in place help and can't be repo'ed against master. I think this can be migrated to a 0.29 December task then

@enricogior
Copy link
Contributor Author

@crutkas
given that since day one we always killed PT Run and never exited gracefully, it exiting gracefully a must have? What are the possible problems of killing the process?

@enricogior enricogior removed the Status-In progress This issue or work-item is under development label Oct 28, 2020
@arjunbalgovind
Copy link
Contributor

@enricogior Launcher has logic for saving some caches (for example Image cache) when the process disposes

API?.SaveAppAllSettings();
PluginManager.Dispose();
_mainWindow?.Dispose();
API?.Dispose();
_mainVM?.Dispose();
_themeManager?.Dispose();
. This is also explicitly called when the "exit when PowerToys.exe terminates" handler is called
RunnerHelper.WaitForPowerToysRunner(powerToysPid, () =>
{
try
{
Dispose();
}
finally
{
Environment.Exit(0);
}
});
}
.
@somil55 and @alekhyareddy28 can add more context on this.

@crutkas crutkas added the Resolution-Fix Committed Fix is checked in, but it might be 3-4 weeks until a release. label Oct 29, 2020
@crutkas
Copy link
Member

crutkas commented Oct 29, 2020

@crutkas crutkas closed this as completed Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Something isn't working Priority-0 Bugs that we consider release-blocking/recall-class (P0) Product-Color Picker All things around the Color Picker utility Product-PowerToys Run Improved app launch PT Run (Win+R) Window Resolution-Fix Committed Fix is checked in, but it might be 3-4 weeks until a release.
Projects
None yet
Development

No branches or pull requests

6 participants