You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

container.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import asyncio
  17. import contextlib
  18. import os
  19. from queue import Empty, Queue
  20. from models.enums import SupportLanguage
  21. from util import env_setting_enabled, is_valid_memory_limit
  22. from utils.common import async_run_command
  23. from core.logger import logger
  24. _CONTAINER_QUEUES: dict[SupportLanguage, Queue] = {}
  25. _CONTAINER_LOCK: asyncio.Lock = asyncio.Lock()
  26. _CONTAINER_EXECUTION_SEMAPHORES: dict[SupportLanguage, asyncio.Semaphore] = {}
  27. async def init_containers(size: int) -> tuple[int, int]:
  28. global _CONTAINER_QUEUES
  29. _CONTAINER_QUEUES = {SupportLanguage.PYTHON: Queue(), SupportLanguage.NODEJS: Queue()}
  30. async with _CONTAINER_LOCK:
  31. while not _CONTAINER_QUEUES[SupportLanguage.PYTHON].empty():
  32. _CONTAINER_QUEUES[SupportLanguage.PYTHON].get_nowait()
  33. while not _CONTAINER_QUEUES[SupportLanguage.NODEJS].empty():
  34. _CONTAINER_QUEUES[SupportLanguage.NODEJS].get_nowait()
  35. for language in SupportLanguage:
  36. _CONTAINER_EXECUTION_SEMAPHORES[language] = asyncio.Semaphore(size)
  37. create_tasks = []
  38. for i in range(size):
  39. name = f"sandbox_python_{i}"
  40. logger.info(f"🛠️ Creating Python container {i + 1}/{size}")
  41. create_tasks.append(_prepare_container(name, SupportLanguage.PYTHON))
  42. name = f"sandbox_nodejs_{i}"
  43. logger.info(f"🛠️ Creating Node.js container {i + 1}/{size}")
  44. create_tasks.append(_prepare_container(name, SupportLanguage.NODEJS))
  45. results = await asyncio.gather(*create_tasks, return_exceptions=True)
  46. success_count = sum(1 for r in results if r is True)
  47. total_task_count = len(create_tasks)
  48. return success_count, total_task_count
  49. async def teardown_containers():
  50. async with _CONTAINER_LOCK:
  51. while not _CONTAINER_QUEUES[SupportLanguage.PYTHON].empty():
  52. name = _CONTAINER_QUEUES[SupportLanguage.PYTHON].get_nowait()
  53. await async_run_command("docker", "rm", "-f", name, timeout=5)
  54. while not _CONTAINER_QUEUES[SupportLanguage.NODEJS].empty():
  55. name = _CONTAINER_QUEUES[SupportLanguage.NODEJS].get_nowait()
  56. await async_run_command("docker", "rm", "-f", name, timeout=5)
  57. async def _prepare_container(name: str, language: SupportLanguage) -> bool:
  58. """Prepare a single container"""
  59. with contextlib.suppress(Exception):
  60. await async_run_command("docker", "rm", "-f", name, timeout=5)
  61. if await create_container(name, language):
  62. _CONTAINER_QUEUES[language].put(name)
  63. return True
  64. return False
  65. async def create_container(name: str, language: SupportLanguage) -> bool:
  66. """Asynchronously create a container"""
  67. create_args = [
  68. "docker",
  69. "run",
  70. "-d",
  71. "--runtime=runsc",
  72. "--name",
  73. name,
  74. "--read-only",
  75. "--tmpfs",
  76. "/workspace:rw,exec,size=100M,uid=65534,gid=65534",
  77. "--tmpfs",
  78. "/tmp:rw,exec,size=50M",
  79. "--user",
  80. "nobody",
  81. "--workdir",
  82. "/workspace",
  83. ]
  84. if os.getenv("SANDBOX_MAX_MEMORY"):
  85. memory_limit = os.getenv("SANDBOX_MAX_MEMORY") or "256m"
  86. if is_valid_memory_limit(memory_limit):
  87. logger.info(f"SANDBOX_MAX_MEMORY: {os.getenv('SANDBOX_MAX_MEMORY')}")
  88. else:
  89. logger.info("Invalid SANDBOX_MAX_MEMORY, using default value: 256m")
  90. memory_limit = "256m"
  91. create_args.extend(["--memory", memory_limit])
  92. else:
  93. logger.info("Set default SANDBOX_MAX_MEMORY: 256m")
  94. create_args.extend(["--memory", "256m"])
  95. if env_setting_enabled("SANDBOX_ENABLE_SECCOMP", "false"):
  96. logger.info(f"SANDBOX_ENABLE_SECCOMP: {os.getenv('SANDBOX_ENABLE_SECCOMP')}")
  97. create_args.extend(["--security-opt", "seccomp=/app/seccomp-profile-default.json"])
  98. if language == SupportLanguage.PYTHON:
  99. create_args.append(os.getenv("SANDBOX_BASE_PYTHON_IMAGE", "sandbox-base-python:latest"))
  100. elif language == SupportLanguage.NODEJS:
  101. create_args.append(os.getenv("SANDBOX_BASE_NODEJS_IMAGE", "sandbox-base-nodejs:latest"))
  102. logger.info(f"Sandbox config:\n\t {create_args}")
  103. try:
  104. returncode, _, stderr = await async_run_command(*create_args, timeout=10)
  105. if returncode != 0:
  106. logger.error(f"❌ Container creation failed {name}: {stderr}")
  107. return False
  108. if language == SupportLanguage.NODEJS:
  109. copy_cmd = ["docker", "exec", name, "bash", "-c", "cp -a /app/node_modules /workspace/"]
  110. returncode, _, stderr = await async_run_command(*copy_cmd, timeout=10)
  111. if returncode != 0:
  112. logger.error(f"❌ Failed to prepare dependencies for {name}: {stderr}")
  113. return False
  114. return await container_is_running(name)
  115. except Exception as e:
  116. logger.error(f"❌ Container creation exception {name}: {str(e)}")
  117. return False
  118. async def recreate_container(name: str, language: SupportLanguage) -> bool:
  119. """Asynchronously recreate a container"""
  120. logger.info(f"🛠️ Recreating container: {name}")
  121. try:
  122. await async_run_command("docker", "rm", "-f", name, timeout=5)
  123. return await create_container(name, language)
  124. except Exception as e:
  125. logger.error(f"❌ Container {name} recreation failed: {str(e)}")
  126. return False
  127. async def release_container(name: str, language: SupportLanguage):
  128. """Asynchronously release a container"""
  129. async with _CONTAINER_LOCK:
  130. if await container_is_running(name):
  131. _CONTAINER_QUEUES[language].put(name)
  132. logger.info(f"🟢 Released container: {name} (remaining available: {_CONTAINER_QUEUES[language].qsize()})")
  133. else:
  134. logger.warning(f"⚠️ Container {name} has crashed, attempting to recreate...")
  135. if await recreate_container(name, language):
  136. _CONTAINER_QUEUES[language].put(name)
  137. logger.info(f"✅ Container {name} successfully recreated and returned to queue")
  138. async def allocate_container_blocking(language: SupportLanguage, timeout=10) -> str:
  139. """Asynchronously allocate an available container"""
  140. start_time = asyncio.get_running_loop().time()
  141. while asyncio.get_running_loop().time() - start_time < timeout:
  142. try:
  143. name = _CONTAINER_QUEUES[language].get_nowait()
  144. async with _CONTAINER_LOCK:
  145. if not await container_is_running(name) and not await recreate_container(name, language):
  146. continue
  147. return name
  148. except Empty:
  149. await asyncio.sleep(0.1)
  150. return ""
  151. async def container_is_running(name: str) -> bool:
  152. """Asynchronously check the container status"""
  153. try:
  154. returncode, stdout, _ = await async_run_command("docker", "inspect", "-f", "{{.State.Running}}", name, timeout=2)
  155. return returncode == 0 and stdout.strip() == "true"
  156. except Exception:
  157. return False