Skip to content

Commit d7598b6

Browse files
committed
Catch exception from background thread
1 parent bdcf921 commit d7598b6

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/applicationinfo.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ APPLICATION_INFO::ShutDownApplication(const bool fServerInitiated)
245245

246246
LOG_INFOF(L"Stopping application '%ls'", QueryApplicationInfoKey().c_str());
247247
app->Stop(fServerInitiated);
248-
LOG_INFO(L"Setting app to null");
249248

250249
SRWExclusiveLock lock(m_applicationLock);
251-
LOG_INFO(L"lock acquired");
252250

253251
m_pApplication = nullptr;
254252
m_pApplicationFactory = nullptr;

src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "Environment.h"
1515
#include "HostFxr.h"
1616

17-
IN_PROCESS_APPLICATION* IN_PROCESS_APPLICATION::s_Application = NULL;
17+
IN_PROCESS_APPLICATION* IN_PROCESS_APPLICATION::s_Application = NULL;
1818

1919
IN_PROCESS_APPLICATION::IN_PROCESS_APPLICATION(
2020
IHttpServer& pHttpServer,
@@ -184,18 +184,26 @@ IN_PROCESS_APPLICATION::LoadManagedApplication(ErrorContext& errorContext)
184184
{
185185
if (p.path() != shadowCopyDir)
186186
{
187-
std::filesystem::remove_all(p.path());
187+
try
188+
{
189+
std::filesystem::remove_all(p.path());
190+
}
191+
catch (...)
192+
{
193+
OBSERVE_CAUGHT_EXCEPTION();
194+
}
188195
}
189196
}
197+
190198
}, m_shadowCopyDirectory);
191199
}
192200

193201
m_workerThread = std::thread([](std::unique_ptr<IN_PROCESS_APPLICATION, IAPPLICATION_DELETER> application)
194-
{
195-
LOG_INFO(L"Starting in-process worker thread");
196-
application->ExecuteApplication();
197-
LOG_INFO(L"Stopping in-process worker thread");
198-
}, ::ReferenceApplication(this));
202+
{
203+
LOG_INFO(L"Starting in-process worker thread");
204+
application->ExecuteApplication();
205+
LOG_INFO(L"Stopping in-process worker thread");
206+
}, ::ReferenceApplication(this));
199207

200208
const HANDLE waitHandles[2] = { m_pInitializeEvent, m_workerThread.native_handle() };
201209

@@ -255,7 +263,7 @@ IN_PROCESS_APPLICATION::ExecuteApplication()
255263
m_pConfig->QueryArguments(),
256264
errorContext,
257265
hostFxrResolutionResult
258-
));
266+
));
259267

260268
hostFxrResolutionResult->GetArguments(context->m_argc, context->m_argv);
261269
THROW_IF_FAILED(SetEnvironmentVariablesOnWorkerProcess());
@@ -440,11 +448,11 @@ void IN_PROCESS_APPLICATION::QueueStop()
440448
LOG_INFO(L"Queueing in-process stop thread");
441449

442450
std::thread stoppingThread([](std::unique_ptr<IN_PROCESS_APPLICATION, IAPPLICATION_DELETER> application)
443-
{
444-
LOG_INFO(L"Starting in-process stop thread");
445-
application->Stop(false);
446-
LOG_INFO(L"Stopping in-process stop thread");
447-
}, ::ReferenceApplication(this));
451+
{
452+
LOG_INFO(L"Starting in-process stop thread");
453+
application->Stop(false);
454+
LOG_INFO(L"Stopping in-process stop thread");
455+
}, ::ReferenceApplication(this));
448456

449457
stoppingThread.detach();
450458
}
@@ -505,7 +513,7 @@ IN_PROCESS_APPLICATION::ExecuteClr(const std::shared_ptr<ExecuteClrContext>& con
505513
context->m_exitCode = exitCode;
506514
context->m_hostFxr.Close();
507515
}
508-
__except(GetExceptionCode() != 0)
516+
__except (GetExceptionCode() != 0)
509517
{
510518
LOG_INFOF(L"Managed threw an exception %d", GetExceptionCode());
511519

@@ -519,7 +527,7 @@ IN_PROCESS_APPLICATION::ExecuteClr(const std::shared_ptr<ExecuteClrContext>& con
519527
// in case of startup timeout
520528
//
521529
VOID
522-
IN_PROCESS_APPLICATION::ClrThreadEntryPoint(const std::shared_ptr<ExecuteClrContext> &context)
530+
IN_PROCESS_APPLICATION::ClrThreadEntryPoint(const std::shared_ptr<ExecuteClrContext>& context)
523531
{
524532
HandleWrapper<ModuleHandleTraits> moduleHandle;
525533

@@ -550,7 +558,7 @@ IN_PROCESS_APPLICATION::SetEnvironmentVariablesOnWorkerProcess()
550558
QueryApplicationPhysicalPath().c_str(),
551559
nullptr);
552560

553-
for (const auto & variable : variables)
561+
for (const auto& variable : variables)
554562
{
555563
LOG_INFOF(L"Setting environment variable %ls=%ls", variable.first.c_str(), variable.second.c_str());
556564
SetEnvironmentVariable(variable.first.c_str(), variable.second.c_str());
@@ -584,7 +592,7 @@ IN_PROCESS_APPLICATION::UnexpectedThreadExit(const ExecuteClrContext& context) c
584592
QueryApplicationId().c_str(),
585593
QueryApplicationPhysicalPath().c_str(),
586594
context.m_exceptionCode
587-
);
595+
);
588596
}
589597
return;
590598
}
@@ -618,8 +626,8 @@ IN_PROCESS_APPLICATION::UnexpectedThreadExit(const ExecuteClrContext& context) c
618626

619627
HRESULT
620628
IN_PROCESS_APPLICATION::CreateHandler(
621-
_In_ IHttpContext *pHttpContext,
622-
_Out_ IREQUEST_HANDLER **pRequestHandler)
629+
_In_ IHttpContext* pHttpContext,
630+
_Out_ IREQUEST_HANDLER** pRequestHandler)
623631
{
624632
try
625633
{

0 commit comments

Comments
 (0)