Selaa lähdekoodia

use RPC rundown to do the server cleanup

instead of polling all the client processes exit code on every call to determin client is alive or not, implement the context handles which will be cleaned up in case the client disconnect or terminated abnormally.
Lei Shi 9 vuotta sitten
vanhempi
sitoutus
1cec7380ea

+ 2 - 2
lib/Backend/InterpreterThunkEmitter.cpp

@@ -706,7 +706,7 @@ InterpreterThunkEmitter::IsInHeap(void* address)
 #ifdef ENABLE_OOP_NATIVE_CODEGEN
     if (JITManager::GetJITManager()->IsOOPJITEnabled())
     {
-        intptr_t remoteScript = this->scriptContext->GetRemoteScriptAddr(false);
+        PSCRIPTCONTEXT_HANDLE remoteScript = this->scriptContext->GetRemoteScriptAddr(false);
         if (!remoteScript)
         {
             return false;
@@ -742,7 +742,7 @@ void InterpreterThunkEmitter::Close()
 #ifdef ENABLE_OOP_NATIVE_CODEGEN
     if (JITManager::GetJITManager()->IsOOPJITEnabled())
     {
-        intptr_t remoteScript = this->scriptContext->GetRemoteScriptAddr(false);
+        PSCRIPTCONTEXT_HANDLE remoteScript = this->scriptContext->GetRemoteScriptAddr(false);
         if (remoteScript)
         {
             JITManager::GetJITManager()->DecommitInterpreterBufferManager(remoteScript, this->isAsmInterpreterThunk);

+ 3 - 4
lib/Backend/PageAllocatorPool.cpp

@@ -21,7 +21,7 @@ PageAllocatorPool::PageAllocatorPool()
 
 PageAllocatorPool::~PageAllocatorPool()
 {
-    RemoveAll();
+    Shutdown();
 }
 
 void PageAllocatorPool::Initialize()
@@ -38,13 +38,13 @@ void PageAllocatorPool::Shutdown()
     if (Instance)
     {
         CloseHandle(Instance->idleCleanupTimer);
+        Instance->RemoveAll();
         HeapDelete(Instance);
         Instance = nullptr;
     }
 }
 void PageAllocatorPool::RemoveAll()
 {
-    AutoCriticalSection autoCS(&cs);
     while (!pageAllocators.Empty())
     {
         HeapDelete(pageAllocators.Pop());
@@ -116,14 +116,13 @@ VOID CALLBACK PageAllocatorPool::IdleCleanupRoutine(
     AUTO_NESTED_HANDLED_EXCEPTION_TYPE(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
     try
     {
+        AutoCriticalSection autoCS(&cs);
         if (Instance)
         {
             // TODO: OOP JIT, use better stragtegy to do the cleanup, like do not remove all,
             // instead keep couple inactivate page allocator for next calls
             Instance->RemoveAll();
         }
-
-        ServerContextManager::IdleCleanup();
     }
     catch (Js::OutOfMemoryException&)
     {

+ 17 - 17
lib/JITClient/JITManager.cpp

@@ -302,7 +302,7 @@ JITManager::Shutdown()
 HRESULT
 JITManager::InitializeThreadContext(
     __in ThreadContextDataIDL * data,
-    __out intptr_t * threadContextInfoAddress,
+    __out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
     __out intptr_t * prereservedRegionAddr)
 {
     Assert(IsOOPJITEnabled());
@@ -323,7 +323,7 @@ JITManager::InitializeThreadContext(
 
 HRESULT
 JITManager::CleanupThreadContext(
-    __in intptr_t threadContextInfoAddress)
+    __inout PPTHREADCONTEXT_HANDLE threadContextInfoAddress)
 {
     Assert(IsOOPJITEnabled());
 
@@ -343,7 +343,7 @@ JITManager::CleanupThreadContext(
 
 HRESULT
 JITManager::AddDOMFastPathHelper(
-    __in intptr_t scriptContextInfoAddress,
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     __in intptr_t funcInfoAddr,
     __in int helper)
 {
@@ -365,7 +365,7 @@ JITManager::AddDOMFastPathHelper(
 
 HRESULT
 JITManager::SetIsPRNGSeeded(
-    __in intptr_t scriptContextInfoAddress,
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     __in boolean value)
 {
     HRESULT hr = E_FAIL;
@@ -385,7 +385,7 @@ JITManager::SetIsPRNGSeeded(
 
 HRESULT
 JITManager::DecommitInterpreterBufferManager(
-    __in intptr_t scriptContextInfoAddress,
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     __in boolean asmJsThunk)
 {
     Assert(IsOOPJITEnabled());
@@ -406,7 +406,7 @@ JITManager::DecommitInterpreterBufferManager(
 
 HRESULT
 JITManager::NewInterpreterThunkBlock(
-    __in intptr_t scriptContextInfoAddress,
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     __in boolean asmJsThunk,
     __out InterpreterThunkInfoIDL * thunkInfo)
 {
@@ -428,7 +428,7 @@ JITManager::NewInterpreterThunkBlock(
 
 HRESULT 
 JITManager::AddModuleRecordInfo(
-    /* [in] */ intptr_t scriptContextInfoAddress,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ unsigned int moduleId,
     /* [in] */ intptr_t localExportSlotsAddr)
 {
@@ -451,7 +451,7 @@ JITManager::AddModuleRecordInfo(
 
 HRESULT
 JITManager::SetWellKnownHostTypeId(
-    __in  intptr_t threadContextRoot,
+    __in  PTHREADCONTEXT_HANDLE threadContextRoot,
     __in  int typeId)
 {
 
@@ -474,7 +474,7 @@ JITManager::SetWellKnownHostTypeId(
 
 HRESULT
 JITManager::UpdatePropertyRecordMap(
-    __in intptr_t threadContextInfoAddress,
+    __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
     __in UpdatedPropertysIDL * updatedProps)
 {
     Assert(IsOOPJITEnabled());
@@ -496,8 +496,8 @@ JITManager::UpdatePropertyRecordMap(
 HRESULT
 JITManager::InitializeScriptContext(
     __in ScriptContextDataIDL * data,
-    __in intptr_t threadContextInfoAddress,
-    __out intptr_t * scriptContextInfoAddress)
+    __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
+    __out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
 {
     Assert(IsOOPJITEnabled());
 
@@ -517,7 +517,7 @@ JITManager::InitializeScriptContext(
 
 HRESULT
 JITManager::CleanupScriptContext(
-    __in intptr_t scriptContextInfoAddress)
+    __inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
 {
     Assert(IsOOPJITEnabled());
 
@@ -537,7 +537,7 @@ JITManager::CleanupScriptContext(
 
 HRESULT
 JITManager::CloseScriptContext(
-    __in intptr_t scriptContextInfoAddress)
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
 {
     Assert(IsOOPJITEnabled());
 
@@ -557,7 +557,7 @@ JITManager::CloseScriptContext(
 
 HRESULT
 JITManager::FreeAllocation(
-    __in intptr_t threadContextInfoAddress,
+    __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
     __in intptr_t address)
 {
     Assert(IsOOPJITEnabled());
@@ -579,7 +579,7 @@ JITManager::FreeAllocation(
 #if DBG
 HRESULT
 JITManager::IsInterpreterThunkAddr(
-    __in intptr_t scriptContextInfoAddress,
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     __in intptr_t address,
     __in boolean asmjsThunk,
     __out boolean * result)
@@ -603,7 +603,7 @@ JITManager::IsInterpreterThunkAddr(
 
 HRESULT
 JITManager::IsNativeAddr(
-    __in intptr_t threadContextInfoAddress,
+    __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
     __in intptr_t address,
     __out boolean * result)
 {
@@ -626,7 +626,7 @@ JITManager::IsNativeAddr(
 HRESULT
 JITManager::RemoteCodeGenCall(
     __in CodeGenWorkItemIDL *workItemData,
-    __in intptr_t scriptContextInfoAddress,
+    __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     __out JITOutputIDL *jitData)
 {
     Assert(IsOOPJITEnabled());

+ 31 - 31
lib/JITClient/JITManager.h

@@ -23,73 +23,73 @@ public:
 
     HRESULT InitializeThreadContext(
         __in ThreadContextDataIDL * data,
-        __out intptr_t *threadContextInfoAddress,
+        __out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __out intptr_t *prereservedRegionAddr);
 
     HRESULT CleanupThreadContext(
-        __in intptr_t threadContextInfoAddress);
+        __inout PPTHREADCONTEXT_HANDLE threadContextInfoAddress);
 
     HRESULT UpdatePropertyRecordMap(
-        __in intptr_t threadContextInfoAddress,
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __in UpdatedPropertysIDL * updatedProps);
 
     HRESULT DecommitInterpreterBufferManager(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in boolean asmJsThunk);
 
     HRESULT NewInterpreterThunkBlock(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in boolean asmJsThunk,
         __out InterpreterThunkInfoIDL * thunkInfo);
 
     HRESULT AddDOMFastPathHelper(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in intptr_t funcInfoAddr,
         __in int helper);
 
     HRESULT AddModuleRecordInfo(
-            /* [in] */ intptr_t scriptContextInfoAddress,
+            /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
             /* [in] */ unsigned int moduleId,
             /* [in] */ intptr_t localExportSlotsAddr);
 
     HRESULT SetWellKnownHostTypeId(
-        __in  intptr_t threadContextRoot,
+        __in  PTHREADCONTEXT_HANDLE threadContextRoot,
         __in  int typeId);
 
     HRESULT InitializeScriptContext(
         __in ScriptContextDataIDL * data,
-        __in  intptr_t threadContextInfoAddress,
-        __out intptr_t *scriptContextInfoAddress);
+        __in  PTHREADCONTEXT_HANDLE threadContextInfoAddress,
+        __out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
 
     HRESULT CleanupProcess();
 
     HRESULT CleanupScriptContext(
-        __in intptr_t scriptContextInfoAddress);
+        __inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
 
     HRESULT CloseScriptContext(
-        __in intptr_t scriptContextInfoAddress);
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
 
     HRESULT FreeAllocation(
-        __in intptr_t threadContextInfoAddress,
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __in intptr_t address);
 
     HRESULT SetIsPRNGSeeded(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in boolean value);
 
     HRESULT IsNativeAddr(
-        __in intptr_t threadContextInfoAddress,
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __in intptr_t address,
         __out boolean * result);
 
     HRESULT RemoteCodeGenCall(
         __in CodeGenWorkItemIDL *workItemData,
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __out JITOutputIDL *jitData);
 
 #if DBG
     HRESULT IsInterpreterThunkAddr(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in intptr_t address,
         __in boolean asmjsThunk,
         __out boolean * result);
@@ -138,72 +138,72 @@ public:
 
     HRESULT InitializeThreadContext(
         __in ThreadContextDataIDL * data,
-        __out intptr_t *threadContextInfoAddress,
+        __out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __out intptr_t *prereservedRegionAddr)
         { Assert(false); return E_FAIL; }
 
     HRESULT CleanupThreadContext(
-        __in intptr_t threadContextInfoAddress)
+        __inout PPTHREADCONTEXT_HANDLE threadContextInfoAddress)
         { Assert(false); return E_FAIL; }
 
     HRESULT UpdatePropertyRecordMap(
-        __in intptr_t threadContextInfoAddress,
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __in UpdatedPropertysIDL * updatedProps)
         { Assert(false); return E_FAIL; }
 
     HRESULT AddDOMFastPathHelper(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in intptr_t funcInfoAddr,
         __in int helper)
         { Assert(false); return E_FAIL; }
 
     HRESULT AddModuleRecordInfo(
-            /* [in] */ intptr_t scriptContextInfoAddress,
+            /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
             /* [in] */ unsigned int moduleId,
             /* [in] */ intptr_t localExportSlotsAddr)
         { Assert(false); return E_FAIL; }
 
     HRESULT SetWellKnownHostTypeId(
-        __in  intptr_t threadContextRoot,
+        __in  PTHREADCONTEXT_HANDLE threadContextRoot,
         __in  int typeId)
         { Assert(false); return E_FAIL; }
 
     HRESULT InitializeScriptContext(
         __in ScriptContextDataIDL * data,
-        __in intptr_t threadContextInfoAddress,
-        __out intptr_t *scriptContextInfoAddress)
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
+        __out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
         { Assert(false); return E_FAIL; }
 
     HRESULT CleanupProcess()
         { Assert(false); return E_FAIL; }
 
     HRESULT CleanupScriptContext(
-        __in intptr_t scriptContextInfoAddress)
+        __inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
         { Assert(false); return E_FAIL; }
 
     HRESULT CloseScriptContext(
-        __in intptr_t scriptContextInfoAddress)
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
         { Assert(false); return E_FAIL; }
 
     HRESULT FreeAllocation(
-        __in intptr_t threadContextInfoAddress,
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __in intptr_t address)
         { Assert(false); return E_FAIL; }
 
     HRESULT SetIsPRNGSeeded(
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __in boolean value)
         { Assert(false); return E_FAIL; }
 
     HRESULT IsNativeAddr(
-        __in intptr_t threadContextInfoAddress,
+        __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         __in intptr_t address,
         __out boolean * result)
         { Assert(false); return E_FAIL; }
 
     HRESULT RemoteCodeGenCall(
         __in CodeGenWorkItemIDL *workItemData,
-        __in intptr_t scriptContextInfoAddress,
+        __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         __out JITOutputIDL *jitData)
         { Assert(false); return E_FAIL; }
 

+ 3 - 0
lib/JITIDL/Chakra.JITIDL.vcxproj

@@ -37,6 +37,9 @@
   <ItemGroup>
     <ClInclude Include="JITTypes.h" />
   </ItemGroup>
+  <ItemGroup>
+    <None Include="ChakraJIT.acf" />
+  </ItemGroup>
   <Import Project="$(BuildConfigPropsPath)Chakra.Build.targets" Condition="exists('$(BuildConfigPropsPath)Chakra.Build.targets')" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
 </Project>

+ 7 - 0
lib/JITIDL/ChakraJIT.acf

@@ -0,0 +1,7 @@
+//-------------------------------------------------------------------------------------------------------
+// Copyright (C) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
+//-------------------------------------------------------------------------------------------------------
+
+typedef [context_handle_noserialize] PTHREADCONTEXT_HANDLE;
+typedef [context_handle_noserialize] PSCRIPTCONTEXT_HANDLE;

+ 17 - 17
lib/JITIDL/ChakraJIT.idl

@@ -16,7 +16,7 @@ interface IChakraJIT
     HRESULT InitializeThreadContext(
         [in] handle_t binding,
         [in] ThreadContextDataIDL * threadData,
-        [out] CHAKRA_PTR * threadContextInfoAddress,
+        [out] PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
         [out] CHAKRA_PTR * prereservedRegionAddr);
 
     HRESULT CleanupProcess(
@@ -25,81 +25,81 @@ interface IChakraJIT
 
     HRESULT CleanupThreadContext(
         [in] handle_t binding,
-        [in] CHAKRA_PTR threadContextInfoAddress);
+        [in, out] PPTHREADCONTEXT_HANDLE threadContextInfoAddress);
 
     HRESULT UpdatePropertyRecordMap(
         [in] handle_t binding,
-        [in] CHAKRA_PTR threadContextInfoAddress,
+        [in] PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         [in] UpdatedPropertysIDL * updatedProps);
 
     HRESULT AddDOMFastPathHelper(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] CHAKRA_PTR funcInfoAddr,
         [in] int helper);
 
     HRESULT AddModuleRecordInfo(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] unsigned int moduleId,
         [in] CHAKRA_PTR localExportSlotsAddr);
 
     HRESULT SetWellKnownHostTypeId(
         [in] handle_t binding,
-        [in] CHAKRA_PTR threadContextInfoAddress,
+        [in] PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         [in] int typeId);
 
     HRESULT InitializeScriptContext(
         [in] handle_t binding,
         [in] ScriptContextDataIDL * scriptContextData,
-        [in] CHAKRA_PTR threadContextInfoAddress,
-        [out] CHAKRA_PTR * scriptContextInfoAddress);
+        [in] PTHREADCONTEXT_HANDLE threadContextInfoAddress,
+        [out] PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
 
     HRESULT CloseScriptContext(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress);
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
 
     HRESULT CleanupScriptContext(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress);
+        [in, out] PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
 
     HRESULT FreeAllocation(
         [in] handle_t binding,
-        [in] CHAKRA_PTR threadContextInfoAddress,
+        [in] PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         [in] CHAKRA_PTR address);
 
     HRESULT NewInterpreterThunkBlock(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] boolean asmJsThunk,
         [out] InterpreterThunkInfoIDL * thunkInfo);
 
     HRESULT DecommitInterpreterBufferManager(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] boolean asmJsManager);
 
     HRESULT IsNativeAddr(
         [in] handle_t binding,
-        [in] CHAKRA_PTR threadContextInfoAddress,
+        [in] PTHREADCONTEXT_HANDLE threadContextInfoAddress,
         [in] CHAKRA_PTR address,
         [out] boolean * result);
 
     HRESULT SetIsPRNGSeeded(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] boolean value);
 
     HRESULT RemoteCodeGen(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] CodeGenWorkItemIDL * workItemData,
         [out] JITOutputIDL * jitData);
 
 #if DBG
     HRESULT IsInterpreterThunkAddr(
         [in] handle_t binding,
-        [in] CHAKRA_PTR scriptContextInfoAddress,
+        [in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
         [in] CHAKRA_PTR address,
         [in] boolean asmjsThunk,
         [out] boolean * result);

+ 6 - 0
lib/JITIDL/JITTypes.h

@@ -55,6 +55,12 @@ typedef unsigned char boolean;
 const int VTABLE_COUNT = 47;
 const int EQUIVALENT_TYPE_CACHE_SIZE = 8;
 
+typedef IDL_DEF([context_handle]) void * PTHREADCONTEXT_HANDLE;
+typedef IDL_DEF([ref]) PTHREADCONTEXT_HANDLE * PPTHREADCONTEXT_HANDLE;
+
+typedef IDL_DEF([context_handle]) void * PSCRIPTCONTEXT_HANDLE;
+typedef IDL_DEF([ref]) PSCRIPTCONTEXT_HANDLE * PPSCRIPTCONTEXT_HANDLE;
+
 typedef struct TypeHandlerIDL
 {
     boolean isObjectHeaderInlinedTypeHandler;

+ 61 - 75
lib/JITServer/JITServer.cpp

@@ -128,22 +128,37 @@ ServerCleanupProcess(
     /* [in] */ handle_t binding,
     /* [in] */ intptr_t processHandle)
 {
+    ServerContextManager::CleanUpForProcess((HANDLE)processHandle);
     CloseHandle((HANDLE)processHandle);
     return S_OK;
 }
 
+
+void
+__RPC_USER PTHREADCONTEXT_HANDLE_rundown(__RPC__in PTHREADCONTEXT_HANDLE phContext)
+{
+    ServerCleanupThreadContext(nullptr, &phContext);
+}
+
+void
+__RPC_USER PSCRIPTCONTEXT_HANDLE_rundown(__RPC__in PSCRIPTCONTEXT_HANDLE phContext)
+{
+    ServerCloseScriptContext(nullptr, phContext);
+    ServerCleanupScriptContext(nullptr, &phContext);
+}
+
 HRESULT
 ServerInitializeThreadContext(
     /* [in] */ handle_t binding,
     /* [in] */ __RPC__in ThreadContextDataIDL * threadContextData,
-    /* [out] */ __RPC__out intptr_t *threadContextRoot,
+    /* [out] */ __RPC__out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
     /* [out] */ __RPC__out intptr_t *prereservedRegionAddr)
 {
     ServerThreadContext * contextInfo = nullptr;
     try
     {
         AUTO_NESTED_HANDLED_EXCEPTION_TYPE(static_cast<ExceptionType>(ExceptionType_OutOfMemory));
-        contextInfo = HeapNew(ServerThreadContext, threadContextData);
+        contextInfo = (ServerThreadContext*)HeapNew(ServerThreadContext, threadContextData);
         ServerContextManager::RegisterThreadContext(contextInfo);
     }
     catch (Js::OutOfMemoryException)
@@ -153,7 +168,7 @@ ServerInitializeThreadContext(
 
     return ServerCallWrapper(contextInfo, [&]()->HRESULT
     {
-        *threadContextRoot = (intptr_t)EncodePointer(contextInfo);
+        *threadContextInfoAddress = (PTHREADCONTEXT_HANDLE)EncodePointer(contextInfo);
         *prereservedRegionAddr = (intptr_t)contextInfo->GetPreReservedVirtualAllocator()->EnsurePreReservedRegion();
 
         return S_OK;
@@ -163,15 +178,19 @@ ServerInitializeThreadContext(
 HRESULT
 ServerCleanupThreadContext(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t threadContextRoot)
+    /* [in] */ PPTHREADCONTEXT_HANDLE threadContextInfoAddress)
 {
-    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer((void*)threadContextRoot);
+    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer(*threadContextInfoAddress);
     if (threadContextInfo == nullptr)
     {
         Assert(false);
         return RPC_S_INVALID_ARG;
     }
 
+    // This tells the run-time, when it is marshalling the out
+    // parameters, that the context handle has been closed normally.
+    *threadContextInfoAddress = nullptr;
+
     return ServerCallWrapper(threadContextInfo, [&]()->HRESULT
     {
         threadContextInfo->Close();
@@ -183,10 +202,10 @@ ServerCleanupThreadContext(
 HRESULT
 ServerUpdatePropertyRecordMap(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t threadContextInfoAddress,
+    /* [in] */ PTHREADCONTEXT_HANDLE threadContextInfoAddress,
     /* [in] */ __RPC__in UpdatedPropertysIDL * updatedProps)
 {
-    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer((void*)threadContextInfoAddress);
+    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer(threadContextInfoAddress);
 
     if (threadContextInfo == nullptr)
     {
@@ -213,11 +232,11 @@ ServerUpdatePropertyRecordMap(
 HRESULT
 ServerAddDOMFastPathHelper(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextRoot,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ intptr_t funcInfoAddr,
     /* [in] */ int helper)
 {
-    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer((void*)scriptContextRoot);
+    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer(scriptContextInfoAddress);
 
     if (scriptContextInfo == nullptr)
     {
@@ -235,11 +254,11 @@ ServerAddDOMFastPathHelper(
 HRESULT
 ServerAddModuleRecordInfo(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextInfoAddress,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ unsigned int moduleId,
     /* [in] */ intptr_t localExportSlotsAddr)
 {
-    ServerScriptContext * serverScriptContext = (ServerScriptContext*)DecodePointer((void*)scriptContextInfoAddress);
+    ServerScriptContext * serverScriptContext = (ServerScriptContext*)DecodePointer(scriptContextInfoAddress);
     if (serverScriptContext == nullptr)
     {
         Assert(false);
@@ -254,13 +273,13 @@ ServerAddModuleRecordInfo(
 
 }
 
-HRESULT 
+HRESULT
 ServerSetWellKnownHostTypeId(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t threadContextRoot,
+    /* [in] */ PTHREADCONTEXT_HANDLE threadContextInfoAddress,
     /* [in] */ int typeId)
 {
-    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer((void*)threadContextRoot);
+    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer(threadContextInfoAddress);
 
     if (threadContextInfo == nullptr)
     {
@@ -279,10 +298,10 @@ HRESULT
 ServerInitializeScriptContext(
     /* [in] */ handle_t binding,
     /* [in] */ __RPC__in ScriptContextDataIDL * scriptContextData,
-    /* [in] */ intptr_t threadContextInfoAddress,
-    /* [out] */ __RPC__out intptr_t * scriptContextInfoAddress)
+    /* [in] */ PTHREADCONTEXT_HANDLE threadContextInfoAddress,
+    /* [out] */ __RPC__out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
 {
-    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer((void*)threadContextInfoAddress);
+    ServerThreadContext * threadContextInfo = (ServerThreadContext*)DecodePointer(threadContextInfoAddress);
 
     *scriptContextInfoAddress = 0;
     if (threadContextInfo == nullptr)
@@ -295,7 +314,7 @@ ServerInitializeScriptContext(
     {
         ServerScriptContext * contextInfo = HeapNew(ServerScriptContext, scriptContextData, threadContextInfo);
         ServerContextManager::RegisterScriptContext(contextInfo);
-        *scriptContextInfoAddress = (intptr_t)EncodePointer(contextInfo);
+        *scriptContextInfoAddress = (PSCRIPTCONTEXT_HANDLE)EncodePointer(contextInfo);
 
 #if !FLOATVAR
         // TODO: should move this to ServerInitializeThreadContext, also for the fields in IDL
@@ -308,9 +327,9 @@ ServerInitializeScriptContext(
 HRESULT
 ServerCleanupScriptContext(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextRoot)
+    /* [in] */ PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
 {
-    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer((void*)scriptContextRoot);
+    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer(*scriptContextInfoAddress);
 
     if (scriptContextInfo == nullptr)
     {
@@ -318,6 +337,10 @@ ServerCleanupScriptContext(
         return RPC_S_INVALID_ARG;
     }
 
+    // This tells the run-time, when it is marshalling the out
+    // parameters, that the context handle has been closed normally.
+    *scriptContextInfoAddress = nullptr;
+
     Assert(scriptContextInfo->IsClosed());
     HeapDelete(scriptContextInfo);
 
@@ -327,9 +350,9 @@ ServerCleanupScriptContext(
 HRESULT
 ServerCloseScriptContext(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextRoot)
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
 {
-    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer((void*)scriptContextRoot);
+    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer(scriptContextInfoAddress);
 
     if (scriptContextInfo == nullptr)
     {
@@ -356,10 +379,10 @@ ServerCloseScriptContext(
 HRESULT
 ServerDecommitInterpreterBufferManager(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextInfoAddress,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ boolean asmJsManager)
 {
-    ServerScriptContext * scriptContext = (ServerScriptContext *)DecodePointer((void*)scriptContextInfoAddress);
+    ServerScriptContext * scriptContext = (ServerScriptContext *)DecodePointer(scriptContextInfoAddress);
 
     if (scriptContext == nullptr)
     {
@@ -377,11 +400,11 @@ ServerDecommitInterpreterBufferManager(
 HRESULT
 ServerNewInterpreterThunkBlock(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextInfo,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfo,
     /* [in] */ boolean asmJsThunk,
     /* [out] */ __RPC__out InterpreterThunkInfoIDL * thunkInfo)
 {
-    ServerScriptContext * scriptContext = (ServerScriptContext *)DecodePointer((void*)scriptContextInfo);
+    ServerScriptContext * scriptContext = (ServerScriptContext *)DecodePointer(scriptContextInfo);
 
     memset(thunkInfo, 0, sizeof(InterpreterThunkInfoIDL));
 
@@ -455,10 +478,10 @@ ServerNewInterpreterThunkBlock(
 HRESULT
 ServerFreeAllocation(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t threadContextInfo,
+    /* [in] */ PTHREADCONTEXT_HANDLE threadContextInfo,
     /* [in] */ intptr_t address)
 {
-    ServerThreadContext * context = (ServerThreadContext*)DecodePointer((void*)threadContextInfo);
+    ServerThreadContext * context = (ServerThreadContext*)DecodePointer(threadContextInfo);
 
     if (context == nullptr)
     {
@@ -466,7 +489,7 @@ ServerFreeAllocation(
         return RPC_S_INVALID_ARG;
     }
 
-    return ServerCallWrapper(context, [&]()->HRESULT 
+    return ServerCallWrapper(context, [&]()->HRESULT
     {
         if (CONFIG_FLAG(OOPCFGRegistration))
         {
@@ -481,12 +504,12 @@ ServerFreeAllocation(
 HRESULT
 ServerIsInterpreterThunkAddr(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextInfoAddress,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ intptr_t address,
     /* [in] */ boolean asmjsThunk,
     /* [out] */ __RPC__out boolean * result)
 {
-    ServerScriptContext * context = (ServerScriptContext*)DecodePointer((void*)scriptContextInfoAddress);
+    ServerScriptContext * context = (ServerScriptContext*)DecodePointer(scriptContextInfoAddress);
 
     if (context == nullptr)
     {
@@ -509,11 +532,11 @@ ServerIsInterpreterThunkAddr(
 HRESULT
 ServerIsNativeAddr(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t threadContextInfo,
+    /* [in] */ PTHREADCONTEXT_HANDLE threadContextInfo,
     /* [in] */ intptr_t address,
     /* [out] */ __RPC__out boolean * result)
 {
-    ServerThreadContext * context = (ServerThreadContext*)DecodePointer((void*)threadContextInfo);
+    ServerThreadContext * context = (ServerThreadContext*)DecodePointer(threadContextInfo);
 
     *result = false;
     if (context == nullptr)
@@ -546,10 +569,10 @@ ServerIsNativeAddr(
 HRESULT
 ServerSetIsPRNGSeeded(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextInfoAddress,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ boolean value)
 {
-    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer((void*)scriptContextInfoAddress);
+    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer(scriptContextInfoAddress);
 
     if (scriptContextInfo == nullptr)
     {
@@ -567,13 +590,13 @@ ServerSetIsPRNGSeeded(
 HRESULT
 ServerRemoteCodeGen(
     /* [in] */ handle_t binding,
-    /* [in] */ intptr_t scriptContextInfoAddress,
+    /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
     /* [in] */ __RPC__in CodeGenWorkItemIDL *workItemData,
     /* [out] */ __RPC__out JITOutputIDL *jitData)
 {
     memset(jitData, 0, sizeof(JITOutputIDL));
 
-    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer((void*)scriptContextInfoAddress);
+    ServerScriptContext * scriptContextInfo = (ServerScriptContext*)DecodePointer(scriptContextInfoAddress);
 
     if (scriptContextInfo == nullptr)
     {
@@ -707,7 +730,7 @@ void ServerContextManager::UnRegisterThreadContext(ServerThreadContext* threadCo
     {
         ServerScriptContext* scriptContext = iter.Current().Key();
         if (scriptContext->GetThreadContext() == threadContext)
-        {   
+        {
             if (!scriptContext->IsClosed())
             {
                 scriptContext->Close();
@@ -794,30 +817,6 @@ bool ServerContextManager::CheckLivenessAndAddref(ServerThreadContext* context)
     return false;
 }
 
-void ServerContextManager::IdleCleanup()
-{
-    JsUtil::BaseHashSet<HANDLE, HeapAllocator> clientProcesses(&HeapAllocator::Instance);
-    {
-        AutoCriticalSection autoCS(&cs);
-        threadContexts.Map([&clientProcesses](ServerThreadContext* threadContext)
-        {
-            if (!clientProcesses.ContainsKey(threadContext->GetProcessHandle()))
-            {
-                clientProcesses.Add(threadContext->GetProcessHandle());
-            }
-        });
-    }
-
-    clientProcesses.Map([](HANDLE hProcess)
-    {
-        DWORD exitCode = STILL_ACTIVE;
-        if (!GetExitCodeProcess(hProcess, &exitCode) && exitCode != STILL_ACTIVE)
-        {
-            CleanUpForProcess(hProcess);
-        }
-    });
-}
-
 template<typename Fn>
 HRESULT ServerCallWrapper(ServerThreadContext* threadContextInfo, Fn fn)
 {
@@ -828,19 +827,6 @@ HRESULT ServerCallWrapper(ServerThreadContext* threadContextInfo, Fn fn)
         AUTO_NESTED_HANDLED_EXCEPTION_TYPE(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
         AutoReleaseThreadContext autoThreadContext(threadContextInfo);
         hr = fn();
-
-        DWORD exitCode = STILL_ACTIVE;
-        if (!GetExitCodeProcess(threadContextInfo->GetProcessHandle(), &exitCode))
-        {
-            Assert(false); // fail to check target process
-            hr = E_FAIL;
-        }
-
-        if (exitCode != STILL_ACTIVE)
-        {
-            ServerContextManager::CleanUpForProcess(threadContextInfo->GetProcessHandle());
-            hr = E_FAIL;
-        }
     }
     catch (ContextClosedException&)
     {

+ 0 - 1
lib/JITServer/JITServer.h

@@ -17,7 +17,6 @@ public:
     static bool CheckLivenessAndAddref(ServerScriptContext* context);
     static bool CheckLivenessAndAddref(ServerThreadContext* context);
 
-    static void IdleCleanup();
 
 private:
     static JsUtil::BaseHashSet<ServerThreadContext*, HeapAllocator> threadContexts;

+ 4 - 4
lib/Runtime/Base/ScriptContext.cpp

@@ -129,7 +129,7 @@ namespace Js
         cache(nullptr),
         firstInterpreterFrameReturnAddress(nullptr),
         builtInLibraryFunctions(nullptr),
-        m_remoteScriptContextAddr(0),
+        m_remoteScriptContextAddr(nullptr),
         isWeakReferenceDictionaryListCleared(false)
 #if ENABLE_PROFILE_INFO
         , referencesSharedDynamicSourceContextInfo(false)
@@ -500,11 +500,11 @@ namespace Js
         this->weakReferenceDictionaryList.Reset();
 
 #if ENABLE_NATIVE_CODEGEN
-        if (m_remoteScriptContextAddr != 0)
+        if (m_remoteScriptContextAddr)
         {
             Assert(JITManager::GetJITManager()->IsOOPJITEnabled());
-            JITManager::GetJITManager()->CleanupScriptContext(m_remoteScriptContextAddr);
-            m_remoteScriptContextAddr = 0;
+            JITManager::GetJITManager()->CleanupScriptContext(&m_remoteScriptContextAddr);
+            Assert(m_remoteScriptContextAddr == nullptr);
         }
 #endif
 

+ 2 - 2
lib/Runtime/Base/ScriptContext.h

@@ -524,7 +524,7 @@ namespace Js
 
         ArenaAllocator* diagnosticArena;
 
-        intptr_t m_remoteScriptContextAddr;
+        PSCRIPTCONTEXT_HANDLE m_remoteScriptContextAddr;
 
         bool startupComplete; // Indicates if the heuristic startup phase for this script context is complete
         bool isInvalidatedForHostObjects;  // Indicates that we've invalidate all objects in the host so stop calling them.
@@ -912,7 +912,7 @@ private:
         void SetDirectHostTypeId(TypeId typeId) {directHostTypeId = typeId; }
         TypeId GetDirectHostTypeId() const { return directHostTypeId; }
 
-        intptr_t GetRemoteScriptAddr(bool allowInitialize = true) 
+        PSCRIPTCONTEXT_HANDLE GetRemoteScriptAddr(bool allowInitialize = true)
         {
 #if ENABLE_OOP_NATIVE_CODEGEN
             if (!m_remoteScriptContextAddr && allowInitialize)

+ 2 - 2
lib/Runtime/Base/ThreadContext.cpp

@@ -189,7 +189,7 @@ ThreadContext::ThreadContext(AllocationPolicyManager * allocationPolicyManager,
     isProfilingUserCode(true),
     loopDepth(0),
     tridentLoadAddress(nullptr),
-    m_remoteThreadContextInfo(0),
+    m_remoteThreadContextInfo(nullptr),
     debugManager(nullptr)
 #if ENABLE_TTD
     , IsTTRecordRequested(false)
@@ -2242,7 +2242,7 @@ void ThreadContext::SetWellKnownHostTypeId(WellKnownHostType wellKnownType, Js::
     {
         this->wellKnownHostTypeHTMLAllCollectionTypeId = typeId;
 #if ENABLE_NATIVE_CODEGEN
-        if (this->m_remoteThreadContextInfo != 0)
+        if (this->m_remoteThreadContextInfo)
         {
             HRESULT hr = JITManager::GetJITManager()->SetWellKnownHostTypeId(this->m_remoteThreadContextInfo, (int)typeId);
             JITManager::HandleServerCallResult(hr);

+ 7 - 4
lib/Runtime/Base/ThreadContext.h

@@ -482,7 +482,7 @@ public:
     typedef JsUtil::WeaklyReferencedKeyDictionary<const Js::PropertyRecord, PropertyGuardEntry*, Js::PropertyRecordPointerComparer> PropertyGuardDictionary;
 
 private:
-    intptr_t m_remoteThreadContextInfo;
+    PTHREADCONTEXT_HANDLE m_remoteThreadContextInfo;
     intptr_t m_prereservedRegionAddr;
 
 #if ENABLE_NATIVE_CODEGEN
@@ -503,7 +503,7 @@ public:
     static void SetJITConnectionInfo(HANDLE processHandle, void* serverSecurityDescriptor, UUID connectionId);
     void EnsureJITThreadContext(bool allowPrereserveAlloc);
 
-    intptr_t GetRemoteThreadContextAddr()
+    PTHREADCONTEXT_HANDLE GetRemoteThreadContextAddr()
     {
         Assert(m_remoteThreadContextInfo);
         return m_remoteThreadContextInfo;
@@ -996,8 +996,11 @@ public:
 
         if (JITManager::GetJITManager()->IsOOPJITEnabled() && m_remoteThreadContextInfo)
         {
-            JITManager::GetJITManager()->CleanupThreadContext(m_remoteThreadContextInfo);
-            m_remoteThreadContextInfo = 0;
+            if (JITManager::GetJITManager()->CleanupThreadContext(&m_remoteThreadContextInfo) == S_OK)
+            {
+                Assert(m_remoteThreadContextInfo == nullptr);
+            }
+            m_remoteThreadContextInfo = nullptr;
         }
 #endif
 #if ENABLE_CONCURRENT_GC