Skip to content

Commit 2c7a2fc

Browse files
authored
Fixes for updating winget from winget (1.12) (#5978)
CP of #5972 onto 1.12 ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/microsoft/winget-cli/pull/5978)
1 parent f805d9b commit 2c7a2fc

File tree

17 files changed

+482
-178
lines changed

17 files changed

+482
-178
lines changed

src/AppInstallerCLICore/Commands/TestCommand.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace AppInstaller::CLI
3131
HRESULT WaitForShutdown(Execution::Context& context)
3232
{
3333
LogAndReport(context, "Waiting for app shutdown event");
34-
if (!ShutdownMonitoring::TerminationSignalHandler::Instance()->WaitForAppShutdownEvent())
34+
if (!ShutdownMonitoring::ServerShutdownSynchronization::WaitForShutdown(300000))
3535
{
3636
LogAndReport(context, "Failed getting app shutdown event");
3737
return APPINSTALLER_CLI_ERROR_INTERNAL_ERROR;
@@ -82,6 +82,21 @@ namespace AppInstaller::CLI
8282
return hr;
8383
}
8484

85+
void AppShutdownTestSystemBlockNewWork(CancelReason reason)
86+
{
87+
AICLI_LOG(CLI, Info, << "AppShutdownTestSystemBlockNewWork :: " << reason);
88+
}
89+
90+
void AppShutdownTestSystemBeginShutdown(CancelReason reason)
91+
{
92+
AICLI_LOG(CLI, Info, << "AppShutdownTestSystemBeginShutdown :: " << reason);
93+
}
94+
95+
void AppShutdownTestSystemWait()
96+
{
97+
AICLI_LOG(CLI, Info, << "AppShutdownTestSystemWait");
98+
}
99+
85100
void EnsureDSCv3Processor(Execution::Context& context)
86101
{
87102
auto& configurationSet = context.Get<Execution::Data::ConfigurationContext>().Set();
@@ -354,6 +369,13 @@ namespace AppInstaller::CLI
354369
{
355370
HRESULT hr = E_FAIL;
356371

372+
ShutdownMonitoring::ServerShutdownSynchronization::ComponentSystem appShutdownTestSystem{};
373+
appShutdownTestSystem.BlockNewWork = AppShutdownTestSystemBlockNewWork;
374+
appShutdownTestSystem.BeginShutdown = AppShutdownTestSystemBeginShutdown;
375+
appShutdownTestSystem.Wait = AppShutdownTestSystemWait;
376+
377+
ShutdownMonitoring::ServerShutdownSynchronization::AddComponent(appShutdownTestSystem);
378+
357379
// Only package context and admin won't create the window message.
358380
if (!Runtime::IsRunningInPackagedContext() || !Runtime::IsRunningAsAdmin())
359381
{

src/AppInstallerCLICore/ExecutionContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ namespace AppInstaller::CLI::Execution
8080

8181
DEFINE_ENUM_FLAG_OPERATORS(ContextFlag);
8282

83-
#ifndef AICLI_DISABLE_TEST_HOOKS
84-
HWND GetWindowHandle();
85-
86-
bool WaitForAppShutdownEvent();
87-
#endif
88-
8983
// Callback to log data actions.
9084
void ContextEnumBasedVariantMapActionCallback(const void* map, Data data, EnumBasedVariantMapAction action);
9185

src/AppInstallerCLICore/Public/ShutdownMonitoring.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#pragma once
44
#include <Windows.h>
55
#include <AppInstallerProgress.h>
6-
#include <winrt/Windows.ApplicationModel.h>
76
#include <wil/resource.h>
87
#include <memory>
98
#include <mutex>
9+
#include <optional>
1010

1111
namespace AppInstaller::ShutdownMonitoring
1212
{
@@ -32,9 +32,6 @@ namespace AppInstaller::ShutdownMonitoring
3232
#ifndef AICLI_DISABLE_TEST_HOOKS
3333
// Gets the window handle for the message window.
3434
HWND GetWindowHandle() const;
35-
36-
// Waits for the shutdown event.
37-
bool WaitForAppShutdownEvent() const;
3835
#endif
3936

4037
private:
@@ -52,17 +49,11 @@ namespace AppInstaller::ShutdownMonitoring
5249

5350
void CreateWindowAndStartMessageLoop();
5451

55-
#ifndef AICLI_DISABLE_TEST_HOOKS
56-
wil::unique_event m_appShutdownEvent;
57-
#endif
58-
5952
std::mutex m_listenersLock;
6053
std::vector<ICancellable*> m_listeners;
6154
wil::unique_event m_messageQueueReady;
6255
wil::unique_hwnd m_windowHandle;
6356
std::thread m_windowThread;
64-
winrt::Windows::ApplicationModel::PackageCatalog m_catalog = nullptr;
65-
decltype(winrt::Windows::ApplicationModel::PackageCatalog{ nullptr }.PackageUpdating(winrt::auto_revoke, nullptr)) m_updatingEvent;
6657
};
6758

6859
// Coordinates shutdown across server components
@@ -91,7 +82,7 @@ namespace AppInstaller::ShutdownMonitoring
9182
static void AddComponent(const ComponentSystem& component);
9283

9384
// Waits for the shutdown to complete.
94-
static void WaitForShutdown();
85+
static bool WaitForShutdown(std::optional<DWORD> timeout = std::nullopt);
9586

9687
// Listens for a termination signal.
9788
void Cancel(CancelReason reason, bool force) override;

src/AppInstallerCLICore/ShutdownMonitoring.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,10 @@ namespace AppInstaller::ShutdownMonitoring
6363
{
6464
return m_windowHandle.get();
6565
}
66-
67-
bool TerminationSignalHandler::WaitForAppShutdownEvent() const
68-
{
69-
return m_appShutdownEvent.wait(60000);
70-
}
7166
#endif
7267

7368
TerminationSignalHandler::TerminationSignalHandler()
7469
{
75-
#ifndef AICLI_DISABLE_TEST_HOOKS
76-
m_appShutdownEvent.create();
77-
#endif
78-
79-
if (Runtime::IsRunningInPackagedContext())
80-
{
81-
// Create package update listener
82-
m_catalog = winrt::Windows::ApplicationModel::PackageCatalog::OpenForCurrentPackage();
83-
m_updatingEvent = m_catalog.PackageUpdating(
84-
winrt::auto_revoke, [this](winrt::Windows::ApplicationModel::PackageCatalog, winrt::Windows::ApplicationModel::PackageUpdatingEventArgs)
85-
{
86-
this->StartAppShutdown();
87-
});
88-
}
89-
9070
// Create message only window.
9171
m_messageQueueReady.create();
9272
m_windowThread = std::thread(&TerminationSignalHandler::CreateWindowAndStartMessageLoop, this);
@@ -120,10 +100,6 @@ namespace AppInstaller::ShutdownMonitoring
120100
{
121101
AICLI_LOG(CLI, Info, << "Initiating shutdown procedure");
122102

123-
#ifndef AICLI_DISABLE_TEST_HOOKS
124-
m_appShutdownEvent.SetEvent();
125-
#endif
126-
127103
// Lifetime manager sends CTRL-C after the WM_QUERYENDSESSION is processed.
128104
// If we disable the CTRL-C handler, the default handler will kill us.
129105
InformListeners(CancelReason::AppShutdown, true);
@@ -306,20 +282,27 @@ namespace AppInstaller::ShutdownMonitoring
306282
instance.m_components.push_back(component);
307283
}
308284

309-
void ServerShutdownSynchronization::WaitForShutdown()
285+
bool ServerShutdownSynchronization::WaitForShutdown(std::optional<DWORD> timeout)
310286
{
311287
ServerShutdownSynchronization& instance = Instance();
312288

289+
if (timeout)
290+
{
291+
return instance.m_shutdownComplete.wait(timeout.value());
292+
}
293+
else
313294
{
314-
std::lock_guard<std::mutex> lock{ instance.m_threadLock };
315-
if (!instance.m_shutdownThread.joinable())
316295
{
317-
AICLI_LOG(Core, Warning, << "Attempt to wait for shutdown when shutdown has not been initiated.");
318-
return;
296+
std::lock_guard<std::mutex> lock{ instance.m_threadLock };
297+
if (!instance.m_shutdownThread.joinable())
298+
{
299+
AICLI_LOG(Core, Warning, << "Attempt to wait for shutdown when shutdown has not been initiated.");
300+
return false;
301+
}
319302
}
320-
}
321303

322-
instance.m_shutdownComplete.wait();
304+
return instance.m_shutdownComplete.wait();
305+
}
323306
}
324307

325308
void ServerShutdownSynchronization::Cancel(CancelReason reason, bool)
@@ -362,6 +345,7 @@ namespace AppInstaller::ShutdownMonitoring
362345
components = m_components;
363346
}
364347

348+
AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: BlockNewWork");
365349
for (const auto& component : components)
366350
{
367351
if (component.BlockNewWork)
@@ -370,6 +354,7 @@ namespace AppInstaller::ShutdownMonitoring
370354
}
371355
}
372356

357+
AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: BeginShutdown");
373358
for (const auto& component : components)
374359
{
375360
if (component.BeginShutdown)
@@ -378,6 +363,7 @@ namespace AppInstaller::ShutdownMonitoring
378363
}
379364
}
380365

366+
AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: Wait");
381367
for (const auto& component : components)
382368
{
383369
if (component.Wait)
@@ -386,6 +372,7 @@ namespace AppInstaller::ShutdownMonitoring
386372
}
387373
}
388374

375+
AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: ShutdownCompleteCallback");
389376
ShutdownCompleteCallback callback = m_callback;
390377
if (callback)
391378
{

src/AppInstallerCLIE2ETests/AppShutdownTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class AppShutdownTests
2424
/// Runs winget test appshutdown and register the application to force a WM_QUERYENDSESSION message.
2525
/// </summary>
2626
[Test]
27+
[Ignore("This test relied on a signal to terminate that was determined to be problematic. We may need OS fixes to test it when elevated.")]
2728
public void RegisterApplicationTest()
2829
{
2930
if (!TestSetup.Parameters.PackagedContext)
@@ -95,9 +96,6 @@ public void RegisterApplicationTest()
9596

9697
Task.WaitAll(new Task[] { testCmdTask, registerTask }, 360000);
9798

98-
// Assert.True(registerTask.Result);
99-
TestContext.Out.Write(testCmdTask.Result.StdOut);
100-
10199
// The ctrl-c command terminates the batch file before the exit code file gets created.
102100
// Look for the output.
103101
Assert.True(testCmdTask.Result.StdOut.Contains("Succeeded waiting for app shutdown event"));

src/AppInstallerCLITests/AppInstallerCLITests.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@
10531053
<CopyFileToFolders Include="TestData\TestFont.ttf">
10541054
<DeploymentContent>true</DeploymentContent>
10551055
</CopyFileToFolders>
1056+
<CopyFileToFolders Include="TestData\Manifest-MSIX-in-AppsAndFeatures.yaml">
1057+
<DeploymentContent>true</DeploymentContent>
1058+
</CopyFileToFolders>
1059+
<CopyFileToFolders Include="TestData\Manifest-MSIX-in-Archive.yaml">
1060+
<DeploymentContent>true</DeploymentContent>
1061+
</CopyFileToFolders>
10561062
</ItemGroup>
10571063
<ItemGroup>
10581064
<ProjectReference Include="..\AppInstallerCLICore\AppInstallerCLICore.vcxproj">

0 commit comments

Comments
 (0)