Pull Request has been successfully merged by openeuler-ci-bot.
(Thank you for xhmlja's contribution.)变更摘要
该 PR 修复 Python SDK(py-sdk)中共享 HTTP transport 初始化时的并发问题。原 get_transport 在 e2b.api.client_sync 模块中创建 TransportWithLogger 单例时未加同步保护,多个并发客户端可能各自构建连接池。本次改动引入 threading.Lock(_transport_lock),对首次构建过程加锁串行化,并通过双检锁(fast path 无锁、加锁后二次检查)保证全局只发布一个共享 transport;同时新增针对该初始化逻辑的单元测试。
主要改动
- 串行化共享 transport 的首次初始化:在
py-sdk/e2b/api/client_sync/__init__.py中新增_transport_lock,get_transport改为持有锁后再检查并构建TransportWithLogger.singleton,使并发客户端复用同一个连接池,锁内二次检查保证快速路径保持无锁。 - 初始化失败时不发布单例、支持重试:单例赋值移入锁内且发生在构造成功之后,构造抛出异常时
TransportWithLogger.singleton仍为None,后续调用可重新初始化。 - 新增并发初始化单元测试:新增
py-sdk/tests/unit/test_http_transport.py,通过 16 个 worker 加threading.Barrier并发调用get_transport,并放慢httpx.HTTPTransport.__init__,断言初始化只发生一次、所有线程拿到同一实例。 - 新增失败重试测试用例:
test_failed_initialization_does_not_publish_and_can_retry验证初始化抛错后单例未被发布,随后再次调用可成功构建并发布 transport。


Welcome To openEuler Community
Hey @xhmlja , thanks for your contribution to the community.
Bot Usage Manual
I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands. You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
Contact Guide
If you have any questions, please contact the SIG: sig-CloudNative ,
and any of the maintainers: @duguhaotian, @gaodaweiky, @jimmieme, @lu-wei-army, @weibaohui, @wonleing, @yangzhao_kl, @zhaoyifan28, @zmr_2020 ,
and any of the committers: @feijianqiang, @zhourenjianz .


门禁正在运行, 您可以通过以下链接查看实时门禁检查结果.
若您对门禁结果含义不清晰或者遇到问题不知如何解决,可参考门禁指导手册
门禁入口及编码规范检查: multiarch/openeuler/trigger/KASandbox/341/console


/lgtm
/approve


当前PR是否有AI参与:
[x] 否
[ ] 是
__1. AI Agent 平台:
__2. AI 模型:
__3. Prompt上下文 :
参考:openEuler社区《生成式AI工具使用与开源贡献政策》
PR功能描述 / 为什么需要这个合入**:
该PR关联的issue
(格式为fixes #<issue号>, 或者resolves #<issue号>): fixes #
希望检视人员了解: