This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix debugger launch race hitting breakpoints in startup code. #8951
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -387,25 +387,66 @@ class RuntimeStartupHelper | |
return hr; | ||
} | ||
|
||
bool AreAllHandlesValid(HANDLE *handleArray, DWORD arrayLength) | ||
{ | ||
for (int i = 0; i < (int)arrayLength; i++) | ||
{ | ||
HANDLE h = handleArray[i]; | ||
if (h == INVALID_HANDLE_VALUE) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
HRESULT InternalEnumerateCLRs(HANDLE** ppHandleArray, _In_reads_(*pdwArrayLength) LPWSTR** ppStringArray, DWORD* pdwArrayLength) | ||
{ | ||
int numTries = 0; | ||
HRESULT hr; | ||
|
||
while (numTries < 25) | ||
{ | ||
hr = EnumerateCLRs(m_processId, ppHandleArray, ppStringArray, pdwArrayLength); | ||
|
||
// EnumerateCLRs uses the OS API CreateToolhelp32Snapshot which can return ERROR_BAD_LENGTH or | ||
// ERROR_PARTIAL_COPY. If we get either of those, we try wait 1/10th of a second try again (that | ||
// is the recommendation of the OS API owners) | ||
hr = EnumerateCLRs(m_processId, ppHandleArray, ppStringArray, pdwArrayLength); | ||
// is the recommendation of the OS API owners). | ||
if ((hr != HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY)) && (hr != HRESULT_FROM_WIN32(ERROR_BAD_LENGTH))) | ||
{ | ||
break; | ||
// Just return any other error or if no handles were found (which means the coreclr module wasn't found yet). | ||
if (FAILED(hr) || *ppHandleArray == NULL || *pdwArrayLength <= 0) | ||
{ | ||
return hr; | ||
} | ||
// If EnumerateCLRs succeeded but any of the handles are INVALID_HANDLE_VALUE, then sleep and retry | ||
// also. This fixes a race condition where dbgshim catches the coreclr module just being loaded but | ||
// before g_hContinueStartupEvent has been initialized. | ||
if (AreAllHandlesValid(*ppHandleArray, *pdwArrayLength)) | ||
{ | ||
return hr; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't find anything, would it make more sense to wait for the startup event to get set? (ex:
If we do decide to retry, we need to free the memory before doing so or we will leak. |
||
// Clean up memory allocated in EnumerateCLRs since this path it succeeded | ||
CloseCLREnumeration(*ppHandleArray, *ppStringArray, *pdwArrayLength); | ||
|
||
*ppHandleArray = NULL; | ||
*ppStringArray = NULL; | ||
*pdwArrayLength = 0; | ||
} | ||
|
||
// Sleep and retry enumerating the runtimes | ||
Sleep(100); | ||
numTries++; | ||
|
||
if (m_canceled) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
// Indicate a timeout | ||
hr = HRESULT_FROM_WIN32(ERROR_TIMEOUT); | ||
|
||
return hr; | ||
} | ||
|
||
|
@@ -517,9 +558,8 @@ class RuntimeStartupHelper | |
bool coreclrExists = false; | ||
|
||
HRESULT hr = InvokeStartupCallback(&coreclrExists); | ||
// Because the target process is suspended on create, the toolhelp apis fail with the below errors even | ||
// with the retry logic in InternalEnumerateCLRs. | ||
if (SUCCEEDED(hr) || (hr == HRESULT_FROM_WIN32(ERROR_PARTIAL_COPY)) || (hr == HRESULT_FROM_WIN32(ERROR_BAD_LENGTH))) | ||
// The retry logic in InternalEnumerateCLRs failed if ERROR_TIMEOUT was returned. | ||
if (SUCCEEDED(hr) || (hr == HRESULT_FROM_WIN32(ERROR_TIMEOUT))) | ||
{ | ||
if (!coreclrExists && !m_canceled) | ||
{ | ||
|
@@ -531,8 +571,11 @@ class RuntimeStartupHelper | |
hr = InvokeStartupCallback(&coreclrExists); | ||
if (SUCCEEDED(hr)) | ||
{ | ||
// We should always find a coreclr module | ||
_ASSERTE(coreclrExists); | ||
// We should always find a coreclr module so fail if we don't | ||
if (!coreclrExists) | ||
{ | ||
hr = E_FAIL; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -1120,8 +1163,6 @@ EnumerateCLRs( | |
|
||
HANDLE hContinueStartupEvent = INVALID_HANDLE_VALUE; | ||
HRESULT hr = GetContinueStartupEvent(debuggeePID, pStringArray[idx], &hContinueStartupEvent); | ||
_ASSERTE(SUCCEEDED(hr) == (hContinueStartupEvent != INVALID_HANDLE_VALUE)); | ||
|
||
pEventArray[idx] = hContinueStartupEvent; | ||
#else | ||
pEventArray[idx] = NULL; | ||
|
@@ -1729,7 +1770,7 @@ GetContinueStartupEvent( | |
ThrowHR(E_FAIL); | ||
} | ||
|
||
if (NULL != continueEvent) | ||
if (NULL != continueEvent && INVALID_HANDLE_VALUE != continueEvent) | ||
{ | ||
if (!DuplicateHandle(hProcess, continueEvent, GetCurrentProcess(), &continueEvent, | ||
EVENT_MODIFY_STATE, FALSE, 0)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of older CoreCLR's, this will still be NULL, so should we check both?