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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. function isPromise(value) {
  79. return Boolean(value && typeof value.then === 'function');
  80. }
  81. if (fs.existsSync(mainPath)) {
  82. const mod = require(mainPath);
  83. const main = typeof mod === 'function' ? mod : mod.main;
  84. if (typeof main !== 'function') {
  85. console.error('Error: main is not a function');
  86. process.exit(1);
  87. }
  88. if (typeof args === 'object' && args !== null) {
  89. try {
  90. const result = main(args);
  91. if (isPromise(result)) {
  92. result.then(output => {
  93. if (output !== null) {
  94. console.log(output);
  95. }
  96. }).catch(err => {
  97. console.error('Error in async main function:', err);
  98. });
  99. } else {
  100. if (result !== null) {
  101. console.log(result);
  102. }
  103. }
  104. } catch (err) {
  105. console.error('Error when executing main:', err);
  106. }
  107. } else {
  108. console.error('Error: args is not a valid object:', args);
  109. }
  110. } else {
  111. console.error('main.js not found in the current directory');
  112. }
  113. """)
  114. # dirs
  115. returncode, _, stderr = await async_run_command("docker", "exec", container, "mkdir", "-p", f"/workspace/{task_id}", timeout=5)
  116. if returncode != 0:
  117. raise RuntimeError(f"Directory creation failed: {stderr}")
  118. # archive
  119. tar_proc = await asyncio.create_subprocess_exec("tar", "czf", "-", "-C", workdir, code_name, runner_name, stdout=asyncio.subprocess.PIPE)
  120. tar_stdout, _ = await tar_proc.communicate()
  121. # unarchive
  122. docker_proc = await asyncio.create_subprocess_exec(
  123. "docker", "exec", "-i", container, "tar", "xzf", "-", "-C", f"/workspace/{task_id}", stdin=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
  124. )
  125. stdout, stderr = await docker_proc.communicate(input=tar_stdout)
  126. if docker_proc.returncode != 0:
  127. raise RuntimeError(stderr.decode())
  128. # exec
  129. start_time = time.time()
  130. try:
  131. logger.info(f"Passed in args: {req.arguments}")
  132. args_json = json.dumps(req.arguments or {})
  133. run_args = [
  134. "docker",
  135. "exec",
  136. "--workdir",
  137. f"/workspace/{task_id}",
  138. container,
  139. "timeout",
  140. str(TIMEOUT),
  141. language,
  142. ]
  143. # flags
  144. if language == SupportLanguage.PYTHON:
  145. run_args.extend(["-I", "-B"])
  146. elif language == SupportLanguage.NODEJS:
  147. run_args.extend([])
  148. else:
  149. assert True, "Will never reach here"
  150. run_args.extend([runner_name, args_json])
  151. returncode, stdout, stderr = await async_run_command(
  152. *run_args,
  153. timeout=TIMEOUT + 5,
  154. )
  155. time_used_ms = (time.time() - start_time) * 1000
  156. logger.info("----------------------------------------------")
  157. logger.info(f"Code: {str(base64.b64decode(req.code_b64))}")
  158. logger.info(f"{returncode=}")
  159. logger.info(f"{stdout=}")
  160. logger.info(f"{stderr=}")
  161. logger.info(f"{args_json=}")
  162. if returncode == 0:
  163. return CodeExecutionResult(
  164. status=ResultStatus.SUCCESS,
  165. stdout=str(stdout),
  166. stderr=stderr,
  167. exit_code=0,
  168. time_used_ms=time_used_ms,
  169. )
  170. elif returncode == 124:
  171. return CodeExecutionResult(
  172. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  173. stdout="",
  174. stderr="Execution timeout",
  175. exit_code=-124,
  176. resource_limit_type=ResourceLimitType.TIME,
  177. time_used_ms=time_used_ms,
  178. )
  179. elif returncode == 137:
  180. return CodeExecutionResult(
  181. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  182. stdout="",
  183. stderr="Memory limit exceeded (killed by OOM)",
  184. exit_code=-137,
  185. resource_limit_type=ResourceLimitType.MEMORY,
  186. time_used_ms=time_used_ms,
  187. )
  188. return analyze_error_result(stderr, returncode)
  189. except asyncio.TimeoutError:
  190. await async_run_command("docker", "exec", container, "pkill", "-9", language)
  191. return CodeExecutionResult(
  192. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  193. stdout="",
  194. stderr="Execution timeout",
  195. exit_code=-1,
  196. resource_limit_type=ResourceLimitType.TIME,
  197. time_used_ms=(time.time() - start_time) * 1000,
  198. )
  199. except Exception as e:
  200. logger.error(f"Execution exception: {str(e)}")
  201. return CodeExecutionResult(status=ResultStatus.PROGRAM_RUNNER_ERROR, stdout="", stderr=str(e), exit_code=-3, detail="internal_error")
  202. finally:
  203. # cleanup
  204. cleanup_tasks = [async_run_command("docker", "exec", container, "rm", "-rf", f"/workspace/{task_id}"), async_run_command("rm", "-rf", workdir)]
  205. await asyncio.gather(*cleanup_tasks, return_exceptions=True)
  206. await release_container(container, language)
  207. def analyze_error_result(stderr: str, exit_code: int) -> CodeExecutionResult:
  208. """Analyze the error result and classify it"""
  209. if "Permission denied" in stderr:
  210. return CodeExecutionResult(
  211. status=ResultStatus.UNAUTHORIZED_ACCESS,
  212. stdout="",
  213. stderr=stderr,
  214. exit_code=exit_code,
  215. unauthorized_access_type=UnauthorizedAccessType.FILE_ACCESS,
  216. )
  217. elif "Operation not permitted" in stderr:
  218. return CodeExecutionResult(
  219. status=ResultStatus.UNAUTHORIZED_ACCESS,
  220. stdout="",
  221. stderr=stderr,
  222. exit_code=exit_code,
  223. unauthorized_access_type=UnauthorizedAccessType.DISALLOWED_SYSCALL,
  224. )
  225. elif "MemoryError" in stderr:
  226. return CodeExecutionResult(
  227. status=ResultStatus.RESOURCE_LIMIT_EXCEEDED,
  228. stdout="",
  229. stderr=stderr,
  230. exit_code=exit_code,
  231. resource_limit_type=ResourceLimitType.MEMORY,
  232. )
  233. else:
  234. return CodeExecutionResult(
  235. status=ResultStatus.PROGRAM_ERROR,
  236. stdout="",
  237. stderr=stderr,
  238. exit_code=exit_code,
  239. runtime_error_type=RuntimeErrorType.NONZERO_EXIT,
  240. )