dify icon indicating copy to clipboard operation
dify copied to clipboard

Concurrent first requests with the same user value create duplicate end_users rows

Open Theysua opened this issue 3 weeks ago • 1 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing issues, including closed ones.
  • [x] I confirm that I am using English to submit this report, otherwise it will be closed.
  • [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • [x] Please do not modify this template :) and fill in all the required fields.

Dify version

1.19.0 (the affected code is unchanged on main at 4833948da)

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. Publish any Workflow app and create an API key.
  2. Pick a user value that has never been used with this app, for example "user": "new-user-1".
  3. Send N requests at the same time to POST /v1/workflows/run, all with that same user value.
  4. Count the rows:
select count(*) from end_users where session_id = 'new-user-1';

✔️ Expected Behavior

One end_users row for one user value in one app, however many requests arrive together.

❌ Actual Behavior

Requests sent together with the same new user end_users rows created
10 sequential 1
5 concurrent 1
10 concurrent 10
20 concurrent (two trials) 20, 20
30 concurrent 20

The duplicates are permanent. In a follow-up test, 20 concurrent first requests created 20 rows and the 20 runs they started are attributed to 20 different end user ids. 20 further requests sent one after another with the same user all resolved to one of those rows, so 19 of the 40 runs stay attached to end users that are never used again.

Cause. AppScopedEndUserService.get_or_create_end_user_by_type (services/app_scoped_end_user_service.py) looks the user up and creates it when nothing is found:

candidates = self._app_scoped_end_users.find_by_session(tenant_id=tenant_id, app_id=app_id, user_id=normalized_user_id)
...
if end_user is None:
    return self._app_scoped_end_users.create(NewAppScopedEndUser(...)).value

The lookup and the insert are separate repository operations, each in its own session, and nothing makes the pair atomic: end_users has only non-unique indexes on (session_id, type) and (tenant_id, session_id, type). Every Service API request that carries a user goes through this path (controllers/service_api/wraps.py, resolve_service_api_end_user). The lookup also has no ORDER BY, so once duplicates exist the row a request gets is not stable.

Impact.

  • End-user counts and per-user statistics are inflated: one real user appears as many.
  • Anything keyed by the end user id (conversations, uploaded files, run history filtered by user) can be split across the duplicates, so a user may not see their own earlier records depending on which duplicate a later request resolves to. I measured the split of workflow runs; I have not tested the conversation and file paths.

Possible fix. A unique index on (tenant_id, app_id, session_id, type) with insert-or-reselect on conflict. Existing deployments may already contain duplicates, so the migration would need to merge them first. Because that touches the schema and existing data, I am opening the issue for discussion before proposing a change.

Theysua avatar Nov 13 '26 09:11 Theysua

The file-access impact has supporting code evidence: end-user lookups filter uploads by UploadFile.created_by == scope.user_id and tool files by ToolFile.user_id == scope.user_id. So resolving a later request to a different duplicate can exclude earlier files unless they are explicitly granted to that execution. This supports your concern, though it is not an end-to-end reproduction. [1]

Two additional paths matter for the proposed fix:

  • create_end_user_batch() also performs lookup-then-insert, and the repository's create_batch() has no conflict recovery. Adding uniqueness would therefore need conflict handling for batch provisioning too, not only get_or_create_end_user_by_type(). [2] [3]
  • Deduplication should preserve type boundaries, particularly APP_DEPLOY: the service deliberately excludes those rows from legacy-type upgrades because retyping them would strand their file grants. There is already a regression test for this distinction. [4] [5]

Any merge of existing identities should remain a maintainer-reviewed migration rather than deleting duplicate rows directly.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about dify with Dosu

dosubot[bot] avatar Nov 13 '26 09:11 dosubot[bot]