Fix sessions race condition during shutdown

This commit is contained in:
Thulinma 2022-10-17 11:51:50 +02:00
parent 8175ad6dd5
commit 0c68bb1530

View file

@ -111,7 +111,6 @@ void userOnDisconnect(Comms::Connections & connections, size_t idx){
}
int main(int argc, char **argv){
Comms::Connections connections;
Comms::Sessions sessions;
uint64_t lastSeen = Util::bootSecs();
Util::redirectLogsIfNeeded();
@ -235,9 +234,6 @@ int main(int argc, char **argv){
if (thisStreamName.size()){streamLastActive[thisStreamName] = now;}
if (memcmp(thisHost.data(), nullAddress, 16)){hostLastActive[thisHost] = now;}
// Open the shared memory page containing statistics for each individual connection in this session
connections.reload(thisSessionId, true);
// Determine session type, since triggers only get run for viewer type sessions
uint64_t thisType = 0;
if (thisSessionId[0] == 'I'){
@ -247,6 +243,13 @@ int main(int argc, char **argv){
} else if (thisSessionId[0] == 'U'){
thisType = 3;
}
bool shouldSleep = false;
//Scope to ensure the connections page is deleted before other cleanup happens
{
// Open the shared memory page containing statistics for each individual connection in this session
Comms::Connections connections;
connections.reload(thisSessionId, true);
// Do a USER_NEW trigger if it is defined for this stream
if (!thisType && Triggers::shouldTrigger("USER_NEW", thisStreamName)){
@ -367,6 +370,8 @@ int main(int argc, char **argv){
}
Util::wait(1000);
}
shouldSleep = connections.getExit();
}//connections scope end
if (Util::bootSecs() - lastSeen > STATS_DELAY){
Util::logExitReason("Session inactive for %d seconds", STATS_DELAY);
}
@ -412,7 +417,7 @@ int main(int argc, char **argv){
Triggers::doTrigger("USER_END", summary.str(), thisStreamName);
}
if (!thisType && connections.getExit()){
if (!thisType && shouldSleep){
uint64_t sleepStart = Util::bootSecs();
// Keep session invalidated for 10 minutes, or until the session stops
while (config.is_active && Util::bootSecs() - sleepStart < SESS_TIMEOUT){