Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

execution.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 base64
  18. import json
  19. import os
  20. import time
  21. import uuid
  22. from core.config import TIMEOUT
  23. from core.container import allocate_container_blocking, release_container
  24. from core.logger import logger
  25. from models.enums import ResourceLimitType, ResultStatus, RuntimeErrorType, SupportLanguage, UnauthorizedAccessType
  26. from models.schemas import CodeExecutionRequest, CodeExecutionResult
  27. from utils.common import async_run_command
  28. async def execute_code(req: CodeExecutionRequest):
  29. """Fully asynchronous execution logic"""
  30. language = req.language
  31. container = await allocate_container_blocking(language)
  32. if not container:
  33. return CodeExecutionResult(
  34. status=ResultStatus.PROGRAM_RUNNER_ERROR,
  35. stdout="",
  36. stderr="Container pool is busy",
  37. exit_code=-10,
  38. detail="no_available_container",
  39. )
  40. task_id = str(uuid.uuid4())
  41. workdir = f"/tmp/sandbox_{task_id}"
  42. os.makedirs(workdir, mode=0o700, exist_ok=True)
  43. try:
  44. if language == SupportLanguage.PYTHON:
  45. code_name = "main.py"
  46. # code
  47. code_path = os.path.join(workdir, code_name)
  48. with open(code_path, "wb") as f:
  49. f.write(base64.b64decode(req.code_b64))
  50. # runner
  51. runner_name = "runner.py"
  52. runner_path = os.path.join(workdir, runner_name)
  53. with open(runner_path, "w") as f:
  54. f.write("""import json
  55. import os
  56. import sys
  57. sys.path.insert(0, os.path.dirname(__file__))
  58. from main import main
  59. if __name__ == "__main__":
  60. args = json.loads(sys.argv[1])
  61. result = main(**args)
  62. if result is not None:
  63. print(result)
  64. """)
  65. elif language == SupportLanguage.NODEJS:
  66. code_name = "main.js"
  67. code_path = os.path.join(workdir, "main.js")
  68. with open(code_path, "wb") as f:
  69. f.write(base64.b64decode(req.code_b64))
  70. runner_name = "runner.js"
  71. runner_path = os.path.join(workdir, "runner.js")
  72. with open(runner_path, "w") as f:
  73. f.write("""
  74. const fs = require('fs');
  75. const path = require('path');
  76. const args = JSON.parse(process.argv[2]);
  77. const mainPath = path.join(__dirname, 'main.js');
  78. if (fs.existsSync(mainPath)) {
  79. const { main } = require(mainPath);
  80. if (typeof args === 'object' && args !== null) {
  81. main(args).then(result => {
  82. if (result !== null) {
  83. console.log(result);
  84. }
  85. }).catch(err => {
  86. console.error('Error in main function:', err);
  87. });
  88. } else {
  89. console.error('Error: args is not a valid object:', args);
  90. }
  91. } else {
  92. console.error('main.js not found in the current directory');
  93. }
  94. """)
  95. # dirs
  96. returncode, _, stderr = await async_run_command("docker", "exec", container, "mkdir", "-p", f"/workspace/{task_id}", timeout=5)
  97. if returncode != 0:
  98. raise RuntimeError(f"Directory creation failed: {stderr}")
  99. # archive
  100. tar_proc = await asyncio.create_subprocess_exec("tar", "czf", "-", "-C", workdir, code_name, runner_name, stdout=asyncio.subprocess.PIPE)
  101. tar_stdout, _ = await tar_proc.communicate()
  102. # unarchive
  103. docker_proc = await asyncio.create_subprocess_exec(
  104. "docker", "exec", "-i", container, "tar", "xzf", "-", "-C", f"/workspace/{task_id}", stdin=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
  105. )
  106. stdout, stderr = await docker_proc.communicate(input=tar_stdout)
  107. if docker_proc.returncode != 0:
  108. raise RuntimeError(stderr.decode())
  109. # exec
  110. start_time = time.time()
  111. try:
  112. logger.info(f"Passed in args: {req.arguments}")
  113. args_json = json.dumps(req.arguments or {})
  114. run_args = [
  115. "docker",
  116. "exec",
  117. "--workdir",
  118. f"/workspace/{task_id}",
  119. container,
  120. "timeout",
  121. str(TIMEOUT),
  122. language,
  123. ]
  124. # flags
  125. if language == SupportLanguage.PYTHON:
  126. run_args.extend(["-I", "-B"])
  127. elif language == SupportLanguage.NODEJS:
  128. run_args.extend([])
  129. else:
  130. assert True, "Will never reach here"
  131. run_args.extend([runner_name, args_json])
  132. returncode, stdout, stderr = await async_run_command(
  133. *run_args,
  134. timeout=TIMEOUT + 5,
  135. )
  136. time_used_ms = (time.time() - start_time) * 1000
  137. logger.info("----------------------------------------------")
  138. logger.info(f"Code: {str(base64.b64decode(req.code_b64))}")
  139. logger.info(f"{returncode=}")
  140. logger.info(f"{stdout=}")
  141. logger.info(f"{stderr=}")
  142. logger.info(f"{args_json=}")
  143. if returncode == 0:
  144. return CodeExecutionResult(
  145. status=ResultStatus.SUCCESS,
  146. stdout=str(stdout),
  147. stderr=stderr,
  148. exit_code=0,
  149. time_used_ms=time_used_ms,
  150. )
  151. elif returncode == 124:
  152. return CodeExecutionResult(
  153. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  154. stdout="",
  155. stderr="Execution timeout",
  156. exit_code=-124,
  157. resource_limit_type=ResourceLimitType.TIME,
  158. time_used_ms=time_used_ms,
  159. )
  160. elif returncode == 137:
  161. return CodeExecutionResult(
  162. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  163. stdout="",
  164. stderr="Memory limit exceeded (killed by OOM)",
  165. exit_code=-137,
  166. resource_limit_type=ResourceLimitType.MEMORY,
  167. time_used_ms=time_used_ms,
  168. )
  169. return analyze_error_result(stderr, returncode)
  170. except asyncio.TimeoutError:
  171. await async_run_command("docker", "exec", container, "pkill", "-9", language)
  172. return CodeExecutionResult(
  173. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  174. stdout="",
  175. stderr="Execution timeout",
  176. exit_code=-1,
  177. resource_limit_type=ResourceLimitType.TIME,
  178. time_used_ms=(time.time() - start_time) * 1000,
  179. )
  180. except Exception as e:
  181. logger.error(f"Execution exception: {str(e)}")
  182. return CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR, stdout="", stderr=str(e), exit_code=-3, detail="internal_error")
  183. finally:
  184. # cleanup
  185. cleanup_tasks = [async_run_command("docker", "exec", container, "rm", "-rf", f"/workspace/{task_id}"), async_run_command("rm", "-rf", workdir)]
  186. await asyncio.gather(*cleanup_tasks, return_exceptions=True)
  187. await release_container(container, language)
  188. def analyze_error_result(stderr: str, exit_code: int) -> CodeExecutionResult:
  189. """Analyze the error result and classify it"""
  190. if "Permission denied" in stderr:
  191. return CodeExecutionResult(
  192. status=ResultStatus.UNAUTHORIZED_ACCESS,
  193. stdout="",
  194. stderr=stderr,
  195. exit_code=exit_code,
  196. unauthorized_access_type=UnauthorizedAccessType.FILE_ACCESS,
  197. )
  198. elif "Operation not permitted" in stderr:
  199. return CodeExecutionResult(
  200. status=ResultStatus.UNAUTHORIZED_ACCESS,
  201. stdout="",
  202. stderr=stderr,
  203. exit_code=exit_code,
  204. unauthorized_access_type=UnauthorizedAccessType.DISALLOWED_SYSCALL,
  205. )
  206. elif "MemoryError" in stderr:
  207. return CodeExecutionResult(
  208. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  209. stdout="",
  210. stderr=stderr,
  211. exit_code=exit_code,
  212. resource_limit_type=ResourceLimitType.MEMORY,
  213. )
  214. else:
  215. return CodeExecutionResult(
  216. status=ResultStatus.PROGRAM_ERROR,
  217. stdout="",
  218. stderr=stderr,
  219. exit_code=exit_code,
  220. runtime_error_type=RuntimeErrorType.NONZERO_EXIT,
  221. )