fix(api): close TOCTOU race in RateLimit.enter()#39179
Open
amogh-nagri-11 wants to merge 3 commits into
Open
Conversation
session.add/session.commit for a new segment ran outside the redis_client.lock covering the max-position read, so concurrent create_segment calls on the same document could read the same max_position and commit duplicate segment positions.
hlen() and hset() were two separate, unsynchronized Redis round-trips, letting concurrent requests all read a count under the cap and all get admitted, exceeding max_active_requests. Replaced with a single atomic Lua script that checks-and-sets in one round trip.
amogh-nagri-11
requested review from
JohnJyong,
QuantumGhost and
laipz8200
as code owners
July 17, 2026 05:05
6 tasks
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-07-17 05:20:35.968975799 +0000
+++ /tmp/pyrefly_pr.txt 2026-07-17 05:20:25.581880003 +0000
@@ -4118,15 +4118,15 @@
ERROR `None` is not subscriptable [unsupported-operation]
--> tests/unit_tests/core/app/entities/test_task_entities.py:58:16
ERROR Object of class `Mapping` has no attribute `close` [missing-attribute]
- --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:396:9
+ --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:392:9
ERROR Argument `TestRateLimitGenerator.test_should_handle_generator_without_close_method.SimpleGenerator` is not assignable to parameter `generator` with type `Generator[str] | Mapping[str, Any]` in function `core.app.features.rate_limiting.rate_limit.RateLimit.generate` [bad-argument-type]
- --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:429:43
+ --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:425:43
ERROR Object of class `Mapping` has no attribute `close` [missing-attribute]
- --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:430:9
+ --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:426:9
ERROR Object of class `Mapping` has no attribute `close` [missing-attribute]
- --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:448:9
+ --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:444:9
ERROR Argument `Mapping[str, Any] | RateLimitGenerator` is not assignable to parameter `i` with type `SupportsNext[@_]` in function `next` [bad-argument-type]
- --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:451:18
+ --> tests/unit_tests/core/app/features/rate_limiting/test_rate_limit.py:447:18
ERROR Argument `SimpleNamespace` is not assignable to parameter `app_record` with type `App` in function `core.app.features.annotation_reply.annotation_reply.AnnotationReplyFeature.query` [bad-argument-type]
--> tests/unit_tests/core/app/features/test_annotation_reply.py:68:24
ERROR Argument `SimpleNamespace` is not assignable to parameter `message` with type `Message` in function `core.app.features.annotation_reply.annotation_reply.AnnotationReplyFeature.query` [bad-argument-type]
@@ -7869,13 +7869,13 @@
ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.SegmentService.delete_child_chunk` [bad-argument-type]
--> tests/unit_tests/services/test_dataset_service_segment.py:280:64
ERROR Argument `SimpleNamespace` is not assignable to parameter `segment` with type `DocumentSegment` in function `services.dataset_service.SegmentService.update_child_chunk` [bad-argument-type]
- --> tests/unit_tests/services/test_dataset_service_segment.py:946:49
+ --> tests/unit_tests/services/test_dataset_service_segment.py:1003:49
ERROR Argument `SimpleNamespace` is not assignable to parameter `document` with type `Document` in function `services.dataset_service.SegmentService.update_child_chunk` [bad-argument-type]
- --> tests/unit_tests/services/test_dataset_service_segment.py:946:68
+ --> tests/unit_tests/services/test_dataset_service_segment.py:1003:68
ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.SegmentService.update_child_chunk` [bad-argument-type]
- --> tests/unit_tests/services/test_dataset_service_segment.py:946:87
+ --> tests/unit_tests/services/test_dataset_service_segment.py:1003:87
ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.SegmentService.delete_child_chunk` [bad-argument-type]
- --> tests/unit_tests/services/test_dataset_service_segment.py:960:60
+ --> tests/unit_tests/services/test_dataset_service_segment.py:1017:60
ERROR Argument `str` is not assignable to parameter `type` with type `Literal['basic', 'bearer', 'custom'] | None` in function `services.entities.external_knowledge_entities.external_knowledge_entities.AuthorizationConfig.__init__` [bad-argument-type]
--> tests/unit_tests/services/test_external_dataset_service.py:118:60
ERROR Argument `str` is not assignable to parameter `type` with type `Literal['api-key', 'no-auth']` in function `services.entities.external_knowledge_entities.external_knowledge_entities.Authorization.__init__` [bad-argument-type]
|
Contributor
Pyrefly Type Coverage
|
…test create_segment takes session as an explicit parameter and never touches the module-level db global, so patching it was unnecessary and appears fragile under CI's parallel (-n auto) test execution, where the patch target intermittently reported "module has no attribute db". Build the mock session directly instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #39177
RateLimit.enter()(api/core/app/features/rate_limiting/rate_limit.py) used two separate, unsynchronized Redis round-trips —hlen()to check the active-request count againstmax_active_requests, thenhset()to register the request — to enforce a per-app concurrency cap. This is a classic check-then-act race: under concurrent requests, multiple callers could all read a count below the cap before any of them wrote, so all of them get admitted, letting the active count exceedmax_active_requests.Replaced the two-step check with a single atomic Redis Lua script (
_ENTER_SCRIPT) that checksHLENagainst the cap and conditionally does theHSETin one round trip, closing the race entirely (Redis executes scripts single-threaded, so no other client can interleave between the check and the write).Note: eval isn't wrapped by RedisClientWrapper (unlike hlen/hset, which auto-prefix keys), so the key is prefixed manually via the same serialize_redis_name_arg/get_redis_key_prefix helpers the wrapper uses internally, to keep behavior consistent with REDIS_KEY_PREFIX config.
No dependencies added.
Screenshots
N/A — backend-only logic fix, no UI change.
Checklist