Merged
fix(py-sdk): serialize shared HTTP transport initialization #146
fix(py-sdk): serialize shared HTTP transport initialization #146
Merged
xhmljaAdd on Sep 14
xhmlja
xhmlja
Sep 14

当前PR是否有AI参与:

[x] 否

[ ] 是
__1. AI Agent 平台:
__2. AI 模型:
__3. Prompt上下文 :

参考:openEuler社区《生成式AI工具使用与开源贡献政策》

PR功能描述 / 为什么需要这个合入**:

该PR关联的issue

(格式为fixes #<issue号>, 或者resolves #<issue号>): fixes #

希望检视人员了解:

likedislike
Pull Request has been successfully merged by openeuler-ci-bot.
(Thank you for xhmlja's contribution.)
xhmljaxhmlja
Sep 14 Created pull request, commit d06ac44b
atomgit-bot
atomgit-bot
Sep 14 Comments:

变更摘要

该 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。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 14 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 14 Set weibaohui,yangzhao_kl,jimmieme,duguhaotian,wonleing,zmr_2020,gaodaweiky,lu-wei-army,zhaoyifan28 as approver
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 14 Added label: sig/sig-CloudNative
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 14 Comments:

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 .

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 14 Added label: openeuler-cla/yes
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 14 Comments:

CLA Signature Pass

xhmlja, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 14 Added label: ci_processing
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 14 Comments:

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

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 14 Removed label: ci_processing
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 14 Comments:
Check Name Build Result 详情 Build Details
check_sca ✅SUCCESS #341
check_package_license ✅SUCCESS
x86_64 check_build ✅SUCCESS #344
aarch64 check_build ✅SUCCESS #360
DT check_build ✅SUCCESS #357
likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 14 Added label: ci_successful
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 14 Comments:
likedislike
zhourenjian
zhourenjianCollaborators
Sep 18 Comments:

/lgtm
/approve

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 18 Added label: approvedlgtm
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 18 Comments:

Review Code Feedback

  • The label lgtm, approved was added to this pull request. It means that zhourenjianz reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄
likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 18 Merged pull request, merge node SHA: b4a23b13e7764f50bf73af7291317225006b1610