Skip to content

Commit

Permalink
Fix race condition for double startRound() during correct guess (#2080)
Browse files Browse the repository at this point in the history
* Fix race condition for double startRound() during correct guess

* Add prevalidation delay for test runner stages
  • Loading branch information
Brainicism authored Apr 14, 2024
1 parent f6085f1 commit fff8e05
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/structures/game_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,29 @@ export default class GameSession extends Session {
* Ends an active GameRound
* @param isError - Whether the round ended due to an error
* @param messageContext - An object containing relevant parts of Eris.Message
* @param gameRound - The round to end
*/
async endRound(
isError: boolean,
messageContext: MessageContext,
gameRound?: GameRound,
): Promise<void> {
// if round ending due to correct song guess, ensure that we are operating on the provided
// game round to ensure we don't end the same round twice (since this.round is modified)
let round = gameRound ?? this.round;
if (!round) {
round = this.round;
}

// wait and accept multiguess results
await delay(
this.multiguessDelayIsActive(this.guildPreference)
? this.guildPreference.getMultiGuessDelay() * 1000
: 0,
);

const round = this.round;

// ensure that only one invocation can proceed
if (round === null || round.finished) {
if (!round || round.finished) {
return;
}

Expand Down Expand Up @@ -394,6 +401,8 @@ export default class GameSession extends Session {
} else if (this.gameType === GameType.SUDDEN_DEATH && !isCorrectGuess) {
await this.endSession("Sudden death game ended", false);
}

await this.startRound(messageContext);
}

/**
Expand Down Expand Up @@ -565,18 +574,15 @@ export default class GameSession extends Session {
return;
}

// mark round as complete, so no more guesses can go through
await this.endRound(false, messageContext);
this.stopGuessTimeout();
this.correctGuesses++;
// mark round as complete, so no more guesses can go through
await this.endRound(false, messageContext, round);

// update game session's lastActive
await this.lastActiveNow();

this.stopGuessTimeout();

await this.incrementGuildSongGuessCount();

await this.startRound(messageContext);
} else if (this.isMultipleChoiceMode() || this.isHiddenMode()) {
// If hidden or multiple choice, everyone guessed and no one was right
if (
Expand Down
4 changes: 4 additions & 0 deletions src/test/end-to-end-tests/test-runner-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ async function evaluateStage(messageResponse?: {

log(`STAGE ${CURRENT_STAGE.stage} | Validating stage`);
const stageOutputValidator = testStage.responseValidator;
if (testStage.prevalidationDelay) {
await delay(testStage.prevalidationDelay);
}

if (
stageOutputValidator(
messageResponse?.title!,
Expand Down
1 change: 1 addition & 0 deletions src/test/end-to-end-tests/test_suites/gameplay_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ const PLAY_TEST_SUITE: TestSuite = {
console.log(`voiceMembers: ${voiceMembers.join(", ")}`);
return !voiceMembers.includes(process.env.BOT_CLIENT_ID!);
},
prevalidationDelay: 3000,
expectedResponseType: KmqResponseType.NONE,
},
],
Expand Down
1 change: 1 addition & 0 deletions src/test/end-to-end-tests/test_suites/test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default interface TestSuite {
client?: Eris.Client,
) => boolean;
expectedResponseType: KmqResponseType;
prevalidationDelay?: number;
preCommandDelay?: number;
}[];
cascadingFailures: boolean;
Expand Down

0 comments on commit fff8e05

Please sign in to comment.