From 5b6e4912b97573925c9c68e75082256083558009 Mon Sep 17 00:00:00 2001 From: Bastien Chatelard Date: Tue, 17 Nov 2026 13:36:45 +0100 Subject: [PATCH] fix(sandbox): unique app name for same-name creates in the same second The app holding a sandbox was named sandbox-app--. App names are unique per organization, so two creates with the same name in the same second (parallel creates, or two processes in one organization) failed with 400 "name already exists". Use the time in base 36 plus 4 random characters: the same 10 characters as before, so every name that fit the 64-character limit still fits. --- koyeb/sandbox/spec.py | 20 +++++++++++++++++++- koyeb/sandbox/test_spec.py | 13 +++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/koyeb/sandbox/spec.py b/koyeb/sandbox/spec.py index f980e57e..ff133fa8 100644 --- a/koyeb/sandbox/spec.py +++ b/koyeb/sandbox/spec.py @@ -13,6 +13,7 @@ import os import secrets +import string import time from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, Dict, List, Optional @@ -353,6 +354,23 @@ def create_deployment_definition( ) +_BASE36 = string.digits + string.ascii_lowercase + + +def _app_name_suffix() -> str: + """Base-36 seconds plus 4 random characters. + + App names are unique per organization, so two same-name creates in the + same second must still differ. Until 2038 it is 10 characters, like the + decimal seconds it replaced, so every name that fit the 64-character app + name limit still fits.""" + seconds, digits = int(time.time()), "" + while seconds: + seconds, digit = divmod(seconds, 36) + digits = _BASE36[digit] + digits + return digits + "".join(secrets.choice(_BASE36) for _ in range(4)) + + @dataclass class SandboxSpec: """The single definition of a sandbox deployment. @@ -413,7 +431,7 @@ def apply_sandbox_secret(self, sandbox_secret: Optional[str] = None) -> str: def app_payload(self) -> Dict[str, Any]: """CreateApp payload for the app a create call owns.""" return { - "name": f"sandbox-app-{self.name}-{int(time.time())}", + "name": f"sandbox-app-{self.name}-{_app_name_suffix()}", "life_cycle": {"delete_when_empty": True}, } diff --git a/koyeb/sandbox/test_spec.py b/koyeb/sandbox/test_spec.py index 59155dd5..05e7ab10 100644 --- a/koyeb/sandbox/test_spec.py +++ b/koyeb/sandbox/test_spec.py @@ -2,6 +2,7 @@ consumed by every create flow (sync and async, Sandbox and ServicePool).""" import unittest +from unittest import mock from koyeb.api.models.deployment_definition_type import DeploymentDefinitionType from koyeb.api.models.deployment_mesh import DeploymentMesh @@ -82,6 +83,18 @@ def test_app_payload_coerces_into_both_model_flavors(self): self.assertTrue(app.life_cycle.delete_when_empty) self.assertTrue(async_app.life_cycle.delete_when_empty) + def test_app_name_differs_for_same_name_creates_in_the_same_second(self): + # the longest name that fit with the 10-digit seconds suffix + spec = _spec(name="n" * 41) + + with mock.patch("koyeb.sandbox.spec.time.time", return_value=1_794_916_800.0): + names = {spec.app_payload()["name"] for _ in range(20)} + + self.assertEqual(len(names), 20) + for name in names: + self.assertLessEqual(len(name), 64) + self.assertRegex(name, r"^[a-z0-9]+([.-][a-z0-9]+)*$") + def test_full_snapshot_omits_definition(self): payload = _spec( snapshot_id="snap-1", snapshot_type=SnapshotType.FULL