Skip to content

Add semaphore to limit concurrent API calls during Kubernetes observer startup#11

Open
tomerqodo wants to merge 2 commits intosentry_full_base_add_semaphore_to_limit_concurrent_api_calls_during_kubernetes_observer_startup_pr11from
sentry_full_head_add_semaphore_to_limit_concurrent_api_calls_during_kubernetes_observer_startup_pr11
Open

Add semaphore to limit concurrent API calls during Kubernetes observer startup#11
tomerqodo wants to merge 2 commits intosentry_full_base_add_semaphore_to_limit_concurrent_api_calls_during_kubernetes_observer_startup_pr11from
sentry_full_head_add_semaphore_to_limit_concurrent_api_calls_during_kubernetes_observer_startup_pr11

Conversation

@tomerqodo
Copy link
Copy Markdown

Benchmark PR from agentic-review-benchmarks#11

desertaxle and others added 2 commits January 25, 2026 12:10
…r startup

closes PrefectHQ#19937

When the observer starts, it receives events for all existing pods/jobs
and checks for duplicates via API calls. This can overwhelm the server
when there are many pods. This change adds a configurable semaphore
(default: 5 concurrent calls) to rate-limit these startup checks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment on lines +149 to 159
)
),
)

response = await orchestration_client.request(
"POST",
"/events/filter",
json=dict(filter=event_filter.model_dump(exclude_unset=True, mode="json")),
json=dict(
filter=event_filter.model_dump(exclude_unset=True, mode="json")
),
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The orchestration_client.request() call is outside the _startup_event_semaphore block, so the API requests are not actually rate-limited as intended.
Severity: CRITICAL

Suggested Fix

Move the response = await orchestration_client.request(...) call inside the async with _startup_event_semaphore: block. This will ensure that the actual network requests are being limited by the semaphore as intended.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/integrations/prefect-kubernetes/prefect_kubernetes/observer.py#L137-L159

Potential issue: The code introduces a semaphore, `_startup_event_semaphore`, to
rate-limit API calls during observer startup. However, the `async with` block for the
semaphore only wraps the creation of the `event_filter` object. The actual network
request, `await orchestration_client.request(...)`, is executed after the semaphore
context has been exited. As a result, the expensive API calls to the `/events/filter`
endpoint are not rate-limited, defeating the purpose of the change. This can lead to the
observer overwhelming the API server with concurrent requests upon startup when many
pods are present.

Did we get this right? 👍 / 👎 to inform future reviews.

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.

2 participants