Skip to content

fix(auth): resolve DetachedInstanceError on User.identity during registration#686

Merged
yaojin3616 merged 8 commits into
mainfrom
fix/register-detached-instance-error
Jun 16, 2026
Merged

fix(auth): resolve DetachedInstanceError on User.identity during registration#686
yaojin3616 merged 8 commits into
mainfrom
fix/register-detached-instance-error

Conversation

@yaojin3616

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes a DetachedInstanceError that was raised during user registration flows (both normal /register/init and SSO /register/sso).

Cause of the bug

  1. When a new user is created in registration_service.create_user_with_identity (using user_dao.create), it only writes the identity_id foreign key. The identity relationship attribute is not loaded/populated in Python memory.
  2. The transaction scope ends, committing and closing the database session. The User object becomes detached.
  3. When returning from the endpoint, generating the token response model or serializing UserOut reads user.email and user.username (which are association proxies delegating to user.identity). Since user.identity is not loaded in memory, SQLAlchemy tries to lazy-load it on a detached instance, resulting in:
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <User at ...> is not bound to a Session; lazy load operation of attribute 'identity' cannot proceed

Fix

  • Explicitly assign user.identity = identity in memory when:
    1. Creating a new user in registration_service.create_user_with_identity
    2. Retrieving an existing user in auth.py's register_init flow
      This populates the relationship in Python memory and avoids database lazy loading on serialization.

@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: a7a5529445

ℹ️ 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".

"registration_source": registration_source,
"is_active": is_active or identity.is_platform_admin,
})
user.identity = identity

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reject passwordless identities during registration

When /register/init reuses an Identity whose password_hash is NULL, the check in backend/app/api/auth.py:179 skips password verification entirely; channel/proactive users are created this way with password_hash=None in backend/app/services/channel_user_service.py. This new eager assignment now lets UserOut serialize successfully instead of hitting the DetachedInstanceError, so an attacker who knows such an email can submit any password and receive an access token for the created/reused user. Add an explicit reject/set-password flow before creating or returning a user for passwordless identities.

Useful? React with 👍 / 👎.

Comment thread docker-compose.ci.yml
Comment on lines +55 to +57
environment:
VITE_API_URL: http://localhost:8000
API_UPSTREAM: backend:8000

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 Set MINIO_UPSTREAM for the CI frontend

The CI compose file starts the frontend, but this environment block omits MINIO_UPSTREAM even though frontend/nginx.conf.template:57 and :60 reference ${MINIO_UPSTREAM} for the /minio/ proxy. In CI, where that variable is not otherwise provided, the nginx template is rendered/parsed with an invalid upstream/Host value (unlike the main compose files, which set ${MINIO_UPSTREAM:-minio:9000}), so the frontend container exits while the deploy test only waits on the backend.

Useful? React with 👍 / 👎.

@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: f4df71a04b

ℹ️ 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".

Comment thread .github/drone.yml
branch:
- main
- release
- ci/test-drone # 临时添加,测试完成后删除

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 Remove the temporary Drone branch trigger

When this YAML is merged, pushes to a ci/test-drone branch will run the full pipeline, including save-images and scp-images to /opt/server/clawith-ci/, even though publishing should be limited to main/release. Since the inline note says this was temporary for testing, drop this branch before merging so test branches cannot overwrite CI deployment artifacts.

Useful? React with 👍 / 👎.

@yaojin3616 yaojin3616 merged commit fa8a429 into main Jun 16, 2026
1 check passed
@yaojin3616 yaojin3616 deleted the fix/register-detached-instance-error branch June 16, 2026 09:51
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