Skip to content

Commit

Permalink
fix(steam): race condition leading to duplicate SteamChild
Browse files Browse the repository at this point in the history
In some cases, needRefresh would be set to true before the existing
child process would've picked up on it and exited. Instead, we use a PID
match to make sure only the latest process remains.
  • Loading branch information
blattersturm committed Dec 23, 2023
1 parent 52e4e6a commit 1b0320e
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions code/components/steam/src/SteamComponent.cpp
Expand Up @@ -22,10 +22,9 @@
struct CfxPresenceState
{
char gameName[512];
volatile bool needRefresh;
int childPid = 0;

CfxPresenceState()
: needRefresh(0)
{
memset(gameName, 0, sizeof(gameName));
}
Expand Down Expand Up @@ -398,7 +397,6 @@ void SteamComponent::InitializePresence()
}

static HostSharedData<CfxPresenceState> gameData("PresenceState");
gameData->needRefresh = false;

if (gameData->gameName[0])
{
Expand Down Expand Up @@ -492,6 +490,11 @@ bool SteamComponent::RunPresenceDummy()

trace("game parent PID: %d\n", parentPid);

HostSharedData<CfxPresenceState> gameData("PresenceState");

auto currentPid = GetCurrentProcessId();
gameData->childPid = currentPid;

// open a handle to the parent process with SYNCHRONIZE rights
HANDLE processHandle = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, parentPid);

Expand All @@ -512,9 +515,7 @@ bool SteamComponent::RunPresenceDummy()
break;
}

static HostSharedData<CfxPresenceState> gameData("PresenceState");

if (gameData->needRefresh)
if (gameData->childPid != currentPid)
{
return true;
}
Expand Down Expand Up @@ -651,9 +652,11 @@ void SteamComponent::SetRichPresenceValue(int idx, const std::string& value)
if (updateGameName)
{
static HostSharedData<CfxPresenceState> gameData("PresenceState");
gameData->needRefresh = true;
strcpy_s(gameData->gameName, value.c_str());

// reset the child PID to make the current process exit early if it can
gameData->childPid = 0;

RunChildLauncher();
}
}
Expand Down

0 comments on commit 1b0320e

Please sign in to comment.