Browse Source

Extend the context rwlock around where we apply optimisations

	This prevents a potential race condition for when the reaper thread reloads the config
Marc Di Luzio 5 years ago
parent
commit
a9e3f866a0
1 changed files with 6 additions and 4 deletions
  1. 6 4
      daemon/gamemode.c

+ 6 - 4
daemon/gamemode.c

@@ -431,7 +431,6 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques
 	/* Update the list */
 	cl->next = self->client;
 	self->client = cl;
-	pthread_rwlock_unlock(&self->rwlock);
 
 	/* First add, init */
 	if (atomic_fetch_add_explicit(&self->refcount, 1, memory_order_seq_cst) == 0) {
@@ -439,6 +438,7 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques
 	}
 
 	game_mode_apply_client_optimisations(self, client);
+	pthread_rwlock_unlock(&self->rwlock);
 
 	game_mode_client_count_changed();
 
@@ -517,7 +517,6 @@ int game_mode_context_unregister(GameModeContext *self, pid_t client, pid_t requ
 	}
 
 	/* Unlock here, potentially yielding */
-	pthread_rwlock_unlock(&self->rwlock);
 
 	if (!found) {
 		LOG_HINTED(
@@ -528,6 +527,7 @@ int game_mode_context_unregister(GameModeContext *self, pid_t client, pid_t requ
 		    "    -- with a nearby 'Removing expired game' which means we cleaned up properly\n"
 		    "    -- (we will log this event). This hint will be displayed only once.\n",
 		    client);
+		pthread_rwlock_unlock(&self->rwlock);
 		return -1;
 	}
 
@@ -536,10 +536,12 @@ int game_mode_context_unregister(GameModeContext *self, pid_t client, pid_t requ
 		game_mode_context_leave(self);
 	}
 
-	game_mode_client_count_changed();
-
 	game_mode_remove_client_optimisations(self, client);
 
+	pthread_rwlock_unlock(&self->rwlock);
+
+	game_mode_client_count_changed();
+
 	return 0;
 }