Skip to content

fix(api): avoid duplicate streaming app concurrency admission - #45038

Merged
hjlarry merged 1 commit into
langgenius:mainfrom
hjlarry:codex/fix-app-concurrency-limit-one
Oct 29, 2026
Merged

hjlarry merged 1 commit into
langgenius:mainfrom
hjlarry:codex/fix-app-concurrency-limit-one

Conversation

@hjlarry

@hjlarry hjlarry commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #45037.

fix https://linear.app/dify/issue/ENG-1364/bug-workflow-%E5%B9%B6%E5%8F%91%E4%B8%8A%E9%99%90%E4%B8%BA-1-%E6%97%B6%E5%8D%95%E6%AC%A1-test-run-%E8%A2%AB%E5%90%8C%E4%B8%80%E8%AF%B7%E6%B1%82%E7%9A%84%E9%87%8D%E5%A4%8D%E9%99%90%E6%B5%81%E6%A3%80%E6%9F%A5%E6%8B%A6%E6%88%AA

Streaming Workflow and Advanced Chat requests entered the app concurrency limiter twice, so a limit of 1 rejected the only active request. Remove the duplicate admission contexts and keep the slot owned by the outer guard and RateLimitGenerator until the stream finishes, fails, or closes. Remove the unused rate_limit_context helper and its contextlib import.

Add regression coverage for both app modes that checks first-request admission, rejection of an overlapping request, slot retention, and admission after normal completion, stream failure, or explicit close.

Validation

  • 87 related unit tests passed: test_app_generate_service.py and core/app/features/rate_limiting/test_rate_limit.py.
  • DEBUG=false make lint passed.
  • DEBUG=false make type-check passed.
  • The reporter verified the concurrency fix in the test deployment.

Checklist

  • This change requires a documentation update.
  • This PR is linked to an issue describing the reproduction.
  • I've verified the change and added regression tests.
  • I ran make lint && make type-check for the backend.
  • No public API or documentation changes are required.

From Codex

@hjlarry
hjlarry requested a review from QuantumGhost as a code owner October 29, 2026 06:29
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-10-29 06:31:22.901856069 +0000
+++ /tmp/pyrefly_pr.txt	2026-10-29 06:31:12.229742607 +0000
@@ -6005,17 +6005,17 @@
 ERROR Argument `Literal['app']` is not assignable to parameter `value` with type `ApiTokenType | SQLCoreOperations[ApiTokenType]` in function `sqlalchemy.orm.base.Mapped.__set__` [bad-argument-type]
    --> tests/unit_tests/services/test_api_token_service.py:323:30
 ERROR Argument `AppMode | str` is not assignable to parameter `mode` with type `AppMode | SQLCoreOperations[AppMode]` in function `models.model.App.__init__` [bad-argument-type]
-  --> tests/unit_tests/services/test_app_generate_service.py:84:14
+  --> tests/unit_tests/services/test_app_generate_service.py:87:14
 ERROR Class member `_TrackingRateLimit.exit` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/services/test_app_generate_service.py:738:21
+   --> tests/unit_tests/services/test_app_generate_service.py:824:21
 ERROR Class member `_TrackingRateLimit.exit` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/services/test_app_generate_service.py:774:21
+   --> tests/unit_tests/services/test_app_generate_service.py:860:21
 ERROR Class member `_TrackingRateLimit.exit` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/services/test_app_generate_service.py:808:21
+   --> tests/unit_tests/services/test_app_generate_service.py:894:21
 ERROR Argument `_FakeStreams` is not assignable to parameter `redis_client` with type `Redis[Unknown] | RedisCluster[Unknown]` in function `libs.broadcast_channel.redis.streams_channel.StreamsBroadcastChannel.__init__` [bad-argument-type]
-    --> tests/unit_tests/services/test_app_generate_service.py:1144:40
+    --> tests/unit_tests/services/test_app_generate_service.py:1230:40
 ERROR Argument `_FakeRedisClient` is not assignable to parameter `redis_client` with type `Redis[Unknown] | RedisCluster[Unknown]` in function `libs.broadcast_channel.redis.pubsub_channel.BroadcastChannel.__init__` [bad-argument-type]
-    --> tests/unit_tests/services/test_app_generate_service.py:1162:38
+    --> tests/unit_tests/services/test_app_generate_service.py:1248:38
 ERROR Argument `str | None` is not assignable to parameter `tenant_id` with type `str` in function `services.app_service.AppService.create_app` [bad-argument-type]
    --> tests/unit_tests/services/test_app_service.py:257:17
 ERROR Argument `str | None` is not assignable to parameter `tenant_id` with type `str` in function `services.app_service.AppService.create_app` [bad-argument-type]

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 80.30% 80.30% +0.00%
Strict coverage 79.99% 79.99% +0.00%
Typed symbols 77,369 77,371 +2
Untyped symbols 19,053 19,053 0
Modules 3990 3990 0

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.55%. Comparing base (4eef080) to head (c58a802).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45038      +/-   ##
==========================================
- Coverage   89.55%   89.55%   -0.01%     
==========================================
  Files        6013     6013              
  Lines      377225   377215      -10     
  Branches    76368    76367       -1     
==========================================
- Hits       337830   337821       -9     
- Misses      33402    33403       +1     
+ Partials     5993     5991       -2     
Flag Coverage Δ
api 89.41% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hjlarry
hjlarry enabled auto-merge October 29, 2026 06:53
@hjlarry
hjlarry added this pull request to the merge queue Oct 29, 2026
Merged via the queue into langgenius:main with commit 2626b79 Oct 29, 2026
49 checks passed
@hjlarry
hjlarry deleted the codex/fix-app-concurrency-limit-one branch October 29, 2026 07:08
hjlarry added a commit that referenced this pull request Oct 29, 2026
@laipz8200 laipz8200 mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streaming app requests are rejected when maximum concurrency is 1

2 participants