Skip to content

Commit

Permalink
Race condition in Mock reference tracker runtime with GC. (#50804)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT authored Apr 7, 2021
1 parent f6b344b commit 57bbdee
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ComHelpers.h>
#include <unordered_map>
#include <list>
#include <mutex>
#include <inspectable.h>

namespace API
Expand Down Expand Up @@ -315,12 +316,16 @@ namespace
class TrackerRuntimeManagerImpl : public API::IReferenceTrackerManager
{
ComSmartPtr<API::IReferenceTrackerHost> _runtimeServices;
std::mutex _objectsLock;
std::list<ComSmartPtr<TrackerObject>> _objects;

public:
ITrackerObject* RecordObject(_In_ TrackerObject* obj, _Outptr_ IUnknown** inner)
{
_objects.push_back(ComSmartPtr<TrackerObject>{ obj });
{
std::lock_guard<std::mutex> guard{ _objectsLock };
_objects.push_back(ComSmartPtr<TrackerObject>{ obj });
}

if (_runtimeServices != nullptr)
_runtimeServices->AddMemoryPressure(sizeof(TrackerObject));
Expand All @@ -337,12 +342,17 @@ namespace

void ReleaseObjects()
{
std::list<ComSmartPtr<TrackerObject>> objectsLocal;
{
std::lock_guard<std::mutex> guard{ _objectsLock };
objectsLocal = std::move(_objects);
}

// Unpeg all instances
for (auto& i : _objects)
for (auto& i : objectsLocal)
(void)i->DisconnectFromReferenceTrackerRuntime();

size_t count = _objects.size();
_objects.clear();
size_t count = objectsLocal.size();
if (_runtimeServices != nullptr)
_runtimeServices->RemoveMemoryPressure(sizeof(TrackerObject) * count);
}
Expand All @@ -358,6 +368,8 @@ namespace
public: // IReferenceTrackerManager
STDMETHOD(ReferenceTrackingStarted)()
{
std::lock_guard<std::mutex> guard{ _objectsLock };

// Unpeg all instances
for (auto& i : _objects)
i->TogglePeg(/* should peg */ false);
Expand All @@ -367,6 +379,8 @@ namespace

STDMETHOD(FindTrackerTargetsCompleted)(_In_ BOOL bWalkFailed)
{
std::lock_guard<std::mutex> guard{ _objectsLock };

// Verify and ensure all connected types are pegged
for (auto& i : _objects)
i->TogglePeg(/* should peg */ true);
Expand Down

0 comments on commit 57bbdee

Please sign in to comment.