Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/AppInstallerCLICore/Commands/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace AppInstaller::CLI
HRESULT WaitForShutdown(Execution::Context& context)
{
LogAndReport(context, "Waiting for app shutdown event");
if (!ShutdownMonitoring::TerminationSignalHandler::Instance()->WaitForAppShutdownEvent())
if (!ShutdownMonitoring::ServerShutdownSynchronization::WaitForShutdown(300000))
{
LogAndReport(context, "Failed getting app shutdown event");
return APPINSTALLER_CLI_ERROR_INTERNAL_ERROR;
Expand Down Expand Up @@ -82,6 +82,21 @@ namespace AppInstaller::CLI
return hr;
}

void AppShutdownTestSystemBlockNewWork(CancelReason reason)
{
AICLI_LOG(CLI, Info, << "AppShutdownTestSystemBlockNewWork :: " << reason);
}

void AppShutdownTestSystemBeginShutdown(CancelReason reason)
{
AICLI_LOG(CLI, Info, << "AppShutdownTestSystemBeginShutdown :: " << reason);
}

void AppShutdownTestSystemWait()
{
AICLI_LOG(CLI, Info, << "AppShutdownTestSystemWait");
}

void EnsureDSCv3Processor(Execution::Context& context)
{
auto& configurationSet = context.Get<Execution::Data::ConfigurationContext>().Set();
Expand Down Expand Up @@ -354,6 +369,13 @@ namespace AppInstaller::CLI
{
HRESULT hr = E_FAIL;

ShutdownMonitoring::ServerShutdownSynchronization::ComponentSystem appShutdownTestSystem{};
appShutdownTestSystem.BlockNewWork = AppShutdownTestSystemBlockNewWork;
appShutdownTestSystem.BeginShutdown = AppShutdownTestSystemBeginShutdown;
appShutdownTestSystem.Wait = AppShutdownTestSystemWait;

ShutdownMonitoring::ServerShutdownSynchronization::AddComponent(appShutdownTestSystem);

// Only package context and admin won't create the window message.
if (!Runtime::IsRunningInPackagedContext() || !Runtime::IsRunningAsAdmin())
{
Expand Down
6 changes: 0 additions & 6 deletions src/AppInstallerCLICore/ExecutionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ namespace AppInstaller::CLI::Execution

DEFINE_ENUM_FLAG_OPERATORS(ContextFlag);

#ifndef AICLI_DISABLE_TEST_HOOKS
HWND GetWindowHandle();

bool WaitForAppShutdownEvent();
#endif

// Callback to log data actions.
void ContextEnumBasedVariantMapActionCallback(const void* map, Data data, EnumBasedVariantMapAction action);

Expand Down
13 changes: 2 additions & 11 deletions src/AppInstallerCLICore/Public/ShutdownMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#pragma once
#include <Windows.h>
#include <AppInstallerProgress.h>
#include <winrt/Windows.ApplicationModel.h>
#include <wil/resource.h>
#include <memory>
#include <mutex>
#include <optional>

namespace AppInstaller::ShutdownMonitoring
{
Expand Down Expand Up @@ -39,9 +39,6 @@ namespace AppInstaller::ShutdownMonitoring
#ifndef AICLI_DISABLE_TEST_HOOKS
// Gets the window handle for the message window.
HWND GetWindowHandle() const;

// Waits for the shutdown event.
bool WaitForAppShutdownEvent() const;
#endif

private:
Expand All @@ -59,17 +56,11 @@ namespace AppInstaller::ShutdownMonitoring

void CreateWindowAndStartMessageLoop();

#ifndef AICLI_DISABLE_TEST_HOOKS
wil::unique_event m_appShutdownEvent;
#endif

std::mutex m_listenersLock;
std::vector<ICancellable*> m_listeners;
wil::unique_event m_messageQueueReady;
wil::unique_hwnd m_windowHandle;
std::thread m_windowThread;
winrt::Windows::ApplicationModel::PackageCatalog m_catalog = nullptr;
decltype(winrt::Windows::ApplicationModel::PackageCatalog{ nullptr }.PackageUpdating(winrt::auto_revoke, nullptr)) m_updatingEvent;
};

// Coordinates shutdown across server components
Expand Down Expand Up @@ -98,7 +89,7 @@ namespace AppInstaller::ShutdownMonitoring
static void AddComponent(const ComponentSystem& component);

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

private:
ServerShutdownSynchronization() = default;
Expand Down
49 changes: 18 additions & 31 deletions src/AppInstallerCLICore/ShutdownMonitoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,16 @@ namespace AppInstaller::ShutdownMonitoring
{
return m_windowHandle.get();
}

bool TerminationSignalHandler::WaitForAppShutdownEvent() const
{
return m_appShutdownEvent.wait(60000);
}
#endif

TerminationSignalHandler::TerminationSignalHandler()
{
#ifndef AICLI_DISABLE_TEST_HOOKS
m_appShutdownEvent.create();
#endif

if (!s_TerminationSignalHandlerEnabled)
{
AICLI_LOG(CLI, Info, << "TerminationSignalHandler is disabled, skipping creation of signal listeners");
return;
}

if (Runtime::IsRunningInPackagedContext())
{
// Create package update listener
m_catalog = winrt::Windows::ApplicationModel::PackageCatalog::OpenForCurrentPackage();
m_updatingEvent = m_catalog.PackageUpdating(
winrt::auto_revoke, [this](winrt::Windows::ApplicationModel::PackageCatalog, winrt::Windows::ApplicationModel::PackageUpdatingEventArgs)
{
this->StartAppShutdown();
});
}

// Create message only window.
m_messageQueueReady.create();
m_windowThread = std::thread(&TerminationSignalHandler::CreateWindowAndStartMessageLoop, this);
Expand Down Expand Up @@ -138,10 +118,6 @@ namespace AppInstaller::ShutdownMonitoring
{
AICLI_LOG(CLI, Info, << "Initiating shutdown procedure");

#ifndef AICLI_DISABLE_TEST_HOOKS
m_appShutdownEvent.SetEvent();
#endif

// Lifetime manager sends CTRL-C after the WM_QUERYENDSESSION is processed.
// If we disable the CTRL-C handler, the default handler will kill us.
InformListeners(CancelReason::AppShutdown, true);
Expand Down Expand Up @@ -334,20 +310,27 @@ namespace AppInstaller::ShutdownMonitoring
instance.m_components.push_back(component);
}

void ServerShutdownSynchronization::WaitForShutdown()
bool ServerShutdownSynchronization::WaitForShutdown(std::optional<DWORD> timeout)
{
ServerShutdownSynchronization& instance = Instance();

if (timeout)
{
return instance.m_shutdownComplete.wait(timeout.value());
}
else
{
std::lock_guard<std::mutex> lock{ instance.m_threadLock };
if (!instance.m_shutdownThread.joinable())
{
AICLI_LOG(Core, Warning, << "Attempt to wait for shutdown when shutdown has not been initiated.");
return;
std::lock_guard<std::mutex> lock{ instance.m_threadLock };
if (!instance.m_shutdownThread.joinable())
{
AICLI_LOG(Core, Warning, << "Attempt to wait for shutdown when shutdown has not been initiated.");
return false;
}
}
}

instance.m_shutdownComplete.wait();
return instance.m_shutdownComplete.wait();
}
}

void ServerShutdownSynchronization::Signal(CancelReason reason)
Expand Down Expand Up @@ -393,6 +376,7 @@ namespace AppInstaller::ShutdownMonitoring
components = m_components;
}

AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: BlockNewWork");
for (const auto& component : components)
{
if (component.BlockNewWork)
Expand All @@ -401,6 +385,7 @@ namespace AppInstaller::ShutdownMonitoring
}
}

AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: BeginShutdown");
for (const auto& component : components)
{
if (component.BeginShutdown)
Expand All @@ -409,6 +394,7 @@ namespace AppInstaller::ShutdownMonitoring
}
}

AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: Wait");
for (const auto& component : components)
{
if (component.Wait)
Expand All @@ -417,6 +403,7 @@ namespace AppInstaller::ShutdownMonitoring
}
}

AICLI_LOG(CLI, Verbose, << "ServerShutdownSynchronization :: ShutdownCompleteCallback");
ShutdownCompleteCallback callback = m_callback;
if (callback)
{
Expand Down
4 changes: 1 addition & 3 deletions src/AppInstallerCLIE2ETests/AppShutdownTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class AppShutdownTests
/// Runs winget test appshutdown and register the application to force a WM_QUERYENDSESSION message.
/// </summary>
[Test]
[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.")]
public void RegisterApplicationTest()
{
if (!TestSetup.Parameters.PackagedContext)
Expand Down Expand Up @@ -95,9 +96,6 @@ public void RegisterApplicationTest()

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

// Assert.True(registerTask.Result);
TestContext.Out.Write(testCmdTask.Result.StdOut);

// The ctrl-c command terminates the batch file before the exit code file gets created.
// Look for the output.
Assert.True(testCmdTask.Result.StdOut.Contains("Succeeded waiting for app shutdown event"));
Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerCLIE2ETests/InprocTestbedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void OneTimeSetup()
[Test]
public void DefaultTest()
{
this.RunInprocTestbed(new TestbedParameters());
this.RunInprocTestbed(new TestbedParameters() { WorkTestSleepInterval = 1000 });
}

/// <summary>
Expand Down
9 changes: 7 additions & 2 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,12 @@
<CopyFileToFolders Include="TestData\TestFont.ttf">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Manifest-MSIX-in-AppsAndFeatures.yaml">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
<CopyFileToFolders Include="TestData\Manifest-MSIX-in-Archive.yaml">
<DeploymentContent>true</DeploymentContent>
</CopyFileToFolders>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AppInstallerCLICore\AppInstallerCLICore.vcxproj">
Expand Down Expand Up @@ -1080,5 +1086,4 @@
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.250303.1\build\native\Microsoft.Windows.CppWinRT.targets'))" />
</Target>
</Project>

</Project>
Loading
Loading