Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ describe('scaleUp with GHES', () => {
expect(mockOctokit.actions.createRegistrationTokenForRepo).not.toBeCalled();
});

it('does not create runners when current runners exceed maximum (race condition)', async () => {
process.env.RUNNERS_MAXIMUM_COUNT = '5';
process.env.ENABLE_EPHEMERAL_RUNNERS = 'false';
// Simulate race condition where pool lambda created more runners than max
mockListRunners.mockImplementation(async () =>
Array.from({ length: 10 }, (_, i) => ({
instanceId: `i-${i}`,
launchTime: new Date(),
type: 'Org',
owner: TEST_DATA_SINGLE.repositoryOwner,
})),
);
await scaleUpModule.scaleUp(TEST_DATA);
// Should not attempt to create runners (would be negative without fix)
expect(createRunner).not.toBeCalled();
expect(mockOctokit.actions.createRegistrationTokenForOrg).not.toBeCalled();
});

it('does create a runner if maximum is set to -1', async () => {
process.env.RUNNERS_MAXIMUM_COUNT = '-1';
process.env.ENABLE_EPHEMERAL_RUNNERS = 'false';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,14 @@ export async function scaleUp(payloads: ActionRequestMessageSQS[]): Promise<stri
});

// Calculate how many runners we want to create.
// Use Math.max(0, ...) to ensure we never attempt to create a negative number of runners,
// which can happen when currentRunners exceeds maximumRunners due to pool/scale-up race conditions.
const newRunners =
maximumRunners === -1
? // If we don't have an upper limit, scale up by the number of new jobs.
scaleUp
: // Otherwise, we do have a limit, so work out if `scaleUp` would exceed it.
Math.min(scaleUp, maximumRunners - currentRunners);
Math.max(0, Math.min(scaleUp, maximumRunners - currentRunners));

const missingInstanceCount = Math.max(0, scaleUp - newRunners);

Expand Down
Loading