Skip to content

RuntimeError: can't start new thread #447

@sky-climber

Description

@sky-climber

我在x86 服务器(openEuler 22.03 (LTS-SP3))上通过以下命令启动opensandbox-server,经验证8080端口可用。

docker run  -d \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /data/opensandbox/sandbox.toml:/etc/opensandbox/config.toml \
-p 8080:8080  \
sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/server:v0.1.1    \
--name opensandbox-server

然后在Java代码中引用代码解释器Kotlin SDK,使用示例代码执行。

public static void main(String[] args) {        
       // 1. 配置连接信息
        ConnectionConfig config = ConnectionConfig.builder()
                .domain("127.0.0.1:8080")
                .apiKey("c36f32be8c464a50a96bd2e94ce4cb7e")
                .build();

        // 2. 创建 Sandbox 实例
        // 注意: 必须使用 code-interpreter 专用镜像
        // 使用 try-with-resources 确保资源正确关闭
        try (Sandbox sandbox = Sandbox.builder()
                .connectionConfig(config)
                .image("sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.1")
                .entrypoint("/opt/opensandbox/code-interpreter.sh")
                .env("PYTHON_VERSION", "3.11") // 指定语言版本
                .build()) {

            // 3. 创建 CodeInterpreter 包装器
            CodeInterpreter interpreter = CodeInterpreter.builder()
                    .fromSandbox(sandbox)
                    .build();

            // 4. 创建执行上下文 (Python)
            CodeContext context = interpreter.codes().createContext(SupportedLanguage.PYTHON);

            // 5. 运行代码
            Execution result = interpreter.codes().run(
                    RunCodeRequest.builder()
                            .code("import sys; print(f'Running on Python {sys.version}')")
                            .context(context)
                            .build()
            );

            // 6. 打印输出
            if (!result.getLogs().getStdout().isEmpty()) {
                System.out.println(result.getLogs().getStdout().get(0).getText());
            }

            // 7. 清理资源
            // 注意: kill() 会立即终止远程沙箱实例;try-with-resources 会自动调用 close() 清理本地资源
            sandbox.kill();
        } catch (SandboxException e) {
            // 处理 Sandbox 特定异常
            System.err.println("沙箱错误: [" + e.getError().getCode() + "] " + e.getError().getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

报错信息如下:

  1. opensandbox-server容器内报错
INFO:     2026-03-14 09:18:54+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=inspect image sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.1 | duration=7.90
INFO:     2026-03-14 09:18:54+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=create sandbox container | duration=49.37
INFO:     2026-03-14 09:18:54+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=ensure directory /opt/opensandbox | duration=46.96
INFO:     2026-03-14 09:18:54+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=copy execd archive to sandbox | duration=128.04
INFO:     2026-03-14 09:18:54+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=ensure directory /opt/opensandbox | duration=54.44
INFO:     2026-03-14 09:18:54+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=install bootstrap script | duration=72.00
INFO:     2026-03-14 09:18:55+0000 src.services.docker: sandbox=72fd389c-030c-40ab-843c-e6471f1cfd8a | action=start sandbox container | duration=622.79
INFO:     2026-03-14 09:18:55+0000 uvicorn.access: 182.115.0.1:46888 - "POST /v1/sandboxes HTTP/1.1" 500
ERROR:    2026-03-14 09:18:55+0000 uvicorn.error: Exception in ASGI application
Traceback (most recent call last):
  File "/app/.venv/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/app/.venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/fastapi/applications.py", line 1134, in __call__
    await super().__call__(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/applications.py", line 107, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 191, in __call__
    with recv_stream, send_stream, collapse_excgroups():
  File "/usr/local/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/app/.venv/lib/python3.10/site-packages/starlette/_utils.py", line 85, in collapse_excgroups
    raise exc
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 193, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/app/src/middleware/auth.py", line 115, in dispatch
    response = await call_next(request)
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 168, in call_next
    raise app_exc from app_exc.__cause__ or app_exc.__context__
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 144, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/cors.py", line 85, in __call__
    await self.app(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/app/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/app/.venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/routing.py", line 716, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/routing.py", line 736, in app
    await route.handle(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/routing.py", line 290, in handle
    await self.app(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 125, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/app/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/app/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/app/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 111, in app
    response = await f(request)
  File "/app/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 391, in app
    raw_response = await run_endpoint_function(
  File "/app/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 290, in run_endpoint_function
    return await dependant.call(**values)
  File "/app/src/api/lifecycle.py", line 108, in create_sandbox
    return sandbox_service.create_sandbox(request)
  File "/app/src/services/docker.py", line 632, in create_sandbox
    return self._provision_sandbox(sandbox_id, request, created_at, expires_at)
  File "/app/src/services/docker.py", line 853, in _provision_sandbox
    self._schedule_expiration(sandbox_id, expires_at)
  File "/app/src/services/docker.py", line 249, in _schedule_expiration
    timer.start()
  File "/usr/local/lib/python3.10/threading.py", line 935, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread
  1. code-interpreter容器内报错
[I 2026-03-14 08:39:30.994 ServerApp] Writing Jupyter server cookie secret to /root/.local/share/jupyter/runtime/jupyter_cookie_secret
[I 2026-03-14 08:39:31.321 ServerApp] notebook_shim | extension was successfully linked.
[I 2026-03-14 08:39:31.342 ServerApp] notebook_shim | extension was successfully loaded.
[I 2026-03-14 08:39:31.345 ServerApp] jupyter_lsp | extension was successfully loaded.
[I 2026-03-14 08:39:31.346 ServerApp] jupyter_server_terminals | extension was successfully loaded.
[I 2026-03-14 08:39:31.349 LabApp] JupyterLab extension loaded from /opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/site-packages/jupyterlab
[I 2026-03-14 08:39:31.349 LabApp] JupyterLab application directory is /opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/share/jupyter/lab
[I 2026-03-14 08:39:31.349 LabApp] Extension Manager is 'pypi'.
[I 2026-03-14 08:39:31.386 ServerApp] jupyterlab | extension was successfully loaded.
[I 2026-03-14 08:39:31.390 ServerApp] notebook | extension was successfully loaded.
[I 2026-03-14 08:39:31.391 ServerApp] Serving notebooks from local directory: /workspace
[I 2026-03-14 08:39:31.391 ServerApp] Jupyter Server 2.17.0 is running at:
[I 2026-03-14 08:39:31.391 ServerApp] http://127.0.0.1:44771/tree?token=...
[I 2026-03-14 08:39:31.391 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[E 2026-03-14 08:39:31.396 ServerApp] Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x7f6e26dff9d0>>, <Task finished name='Task-2' coro=<initialize() done, defined at /opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/site-packages/jupyter_lsp/serverextension.py:15> exception=RuntimeError("can't start new thread")>)
    Traceback (most recent call last):
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/site-packages/tornado/ioloop.py", line 758, in _run_callback
        ret = callback()
              ^^^^^^^^^^
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/site-packages/tornado/ioloop.py", line 782, in _discard_future_result
        future.result()
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/site-packages/jupyter_lsp/serverextension.py", line 24, in initialize
        await nbapp.io_loop.run_in_executor(pool, manager.initialize)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/site-packages/tornado/platform/asyncio.py", line 272, in run_in_executor
        return self.asyncio_loop.run_in_executor(executor, func, *args)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/asyncio/base_events.py", line 830, in run_in_executor
        executor.submit(func, *args), loop=self)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/concurrent/futures/thread.py", line 176, in submit
        self._adjust_thread_count()
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/concurrent/futures/thread.py", line 199, in _adjust_thread_count
        t.start()
      File "/opt/python/versions/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/threading.py", line 964, in start
        _start_new_thread(self._bootstrap, ())
    RuntimeError: can't start new thread

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions