Skip to content

Fix A2A messaging DB session handling#681

Merged
yaojin3616 merged 3 commits into
mainfrom
refactor/a2a-messaging
Jun 15, 2026
Merged

Fix A2A messaging DB session handling#681
yaojin3616 merged 3 commits into
mainfrom
refactor/a2a-messaging

Conversation

@yaojin3616

Copy link
Copy Markdown
Collaborator

Summary

  • modularize A2A message routing and move consult flow onto the shared LLM failover path
  • propagate request DB sessions through the DAO context and make standalone DAO sessions commit/rollback safely
  • fix channel identity/session isolation paths that could violate Identity foreign keys

Tests

  • python -m pytest tests/test_base_dao.py tests/test_a2a_msg_type.py -q

yaojin3616 and others added 3 commits June 15, 2026 13:36
## 问题1:ForeignKeyViolationError(users.identity_id → identities.id)

根因:channel_user_service._create_channel_user() 调用
registration_service.find_or_create_identity(),后者通过
identity_dao.create_identity() 开启独立的 async with self.session()。
由于后台channel handler 直接使用 async_session()(未经 Depends(get_db)),
_session_ctx 未设置,DAO 打开新 session 执行 flush 后退出时被 rollback,
Identity 行消失,随后 User INSERT 的 FK 引用失效。

修复:_create_channel_user 和 get_platform_user_by_org_member 改为
直接在传入的 db session 上创建 Identity(db.add + flush),
确保 Identity 和 User 在同一事务内,彻底解决 FK 违反。

## 问题2:LLM调用期间长期占用DB连接

所有channel在调用LLM时仍持有数据库连接(30~180s),耗尽连接池。

修复:所有channel统一采用三段式事务模式:
- Phase 1:短事务加载数据、保存用户消息 → commit + close
- Phase 2:调用LLM(_call_llm_with_config,无DB session)
- Phase 3:新短事务(async_session)保存回复

## 修复文件清单

- database.py:get_db 正确设置/重置 _session_ctx ContextVar
- tenants.py:修复 bind_org_member 调用签名
- enterprise.py:修复 get_email_templates 调用签名
- feishu.py:修复图片处理路径的会话泄漏
- teams.py:修复webhook持有Depends(get_db)连接跨LLM调用
- whatsapp.py:修复LLM调用期间持有连接
- wechat_channel.py:修复LLM调用期间持有连接
- wecom_stream.py:修复LLM调用期间持有连接
- discord_gateway.py:修复LLM调用期间持有连接
- channel_user_service.py:根治Identity FK违反,同session创建Identity+User
@yaojin3616 yaojin3616 merged commit 538ec5e into main Jun 15, 2026
1 check was pending
@yaojin3616 yaojin3616 deleted the refactor/a2a-messaging branch June 15, 2026 08:19

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2c76cc8712

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

identity = Identity(
email=email,
phone=normalized_phone,
username=username,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep channel identity usernames unique

When the generated channel username already belongs to another Identity with a different email/phone (for example a pre-existing web username or the same sanitized channel ID from another provider), this direct insert bypasses registration_service.find_or_create_identity's is_username_taken suffix logic and the db.flush() will hit the unique identities.username constraint, breaking lazy channel user creation. Keep the username de-duplication while still creating the identity in the caller's session.

Useful? React with 👍 / 👎.

Comment on lines 331 to +332
await _send_whatsapp_messages(config, sender_phone, reply_text)
config.is_connected = True
db.add(ChatMessage(agent_id=agent_id, user_id=platform_user_id, role="assistant", content=reply_text, conversation_id=session_conv_id))
sess.last_message_at = datetime.now(timezone.utc)
await db.commit()
async with _async_session() as _save_db:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve WhatsApp connection status updates

When WhatsApp delivery succeeds or fails, this path now only sends the message and records the assistant reply; it no longer updates ChannelConfig.is_connected. The previous code set it to True after a successful _send_whatsapp_messages call and False in the exception branch, so the admin/UI connection indicator can remain stale for WhatsApp agents despite the latest delivery result.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant