← Back to Pull requests

Bound blocking Kafka client calls #1376

Closed
gqmelo closed this pull request without merging on Oct 12, 2026 14:24 UTC
View on GitHub
gqmelo gqmelo commented on Sep 30, 2026 10:10 UTC
Author

About this change - What it does

Enforce a timeout to all Kafka API calls to prevent Karapace threads to be stuck forever. Most calls have a default timeout of -1 (infinite), so there were a lot of places where this could happen.

Why this way

confluent-kafka calls such as get_watermark_offsets, list_topics, committed and flush wait forever without a timeout.

After a broker restart the schema reader thread hung in get_watermark_offsets and never became ready, so writes failed until restart.

This gives these calls a default timeout, and run the REST proxy admin calls off the event loop.

These are the typical logs of the stuck reader:

WARNING No master set: PrimaryInfo(primary=False, primary_url=None)
INFO Schema reader isn't ready yet: <bound method KafkaSchemaReader.ready ...>
INFO fda7:a938:5bfe:5fa6:0:5e4:19e8:d5b4:35778 - "PUT /config HTTP/1.1" 500

Eventually this could be logged, but the messages above would still be printed forever until karapace is restarted, because this is just a log from librdkafka's thread. And the consequence is that it treats the consumer group as failed, but it doesn't unblock the reader thread.

MAXPOLL ... Application maximum poll interval (300000ms) exceeded

Diff: 345 lines (+326 / -19) across 7 files Reviewer: gqmelo Review duration: 17533m
All Vouch review confidence checks passed
Residual risk 0.10 = Change Risk (0.61) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.61 · 345 lines diff across 7 files
0.61
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.73 · 5 substantive comment(s)
0.73
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
84%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.350
Self Review self_review
0.140
Real PR Conversation Timeline
13 items
cloudflare-workers-and-pages[bot] cloudflare-workers-and-pages[bot] commented on Sep 30, 2026 10:13 UTC
Bot

Deploying karapace with &nbsp;<a href="https://pages.dev"><img alt="Cloudflare Pages" src="https://user-images.githubusercontent.com/23264/106598434-9e719e00-654f-11eb-9e59-6167043cfa01.png" width="16"></a> &nbsp;Cloudflare Pages

<table><tr><td><strong>Latest commit:</strong> </td><td>
<code>3458123</code>
</td></tr>
<tr><td><strong>Status:</strong></td><td>&nbsp;✅&nbsp; Deploy successful!</td></tr>
<tr><td><strong>Preview URL:</strong></td><td>
<a href='https://08e16530.karapace.pages.dev'>https://08e16530.karapace.pages.dev</a>
</td></tr>
<tr><td><strong>Branch Preview URL:</strong></td><td>
<a href='https://gqmelo-fix-schema-reader-han.karapace.pages.dev'>https://gqmelo-fix-schema-reader-han.karapace.pages.dev</a>
</td></tr>
</table>

View logs

Copilot Copilot commented on code on Sep 30, 2026 10:13 UTC
Code Review
src/karapace/core/kafka/consumer.py:1
@@ -260,7 +254,15 @@ async def commit(
         offsets: list[TopicPartition] | None = None,
     ) -> list[TopicPartition] | None:
         assert self.consumer is not None, self._START_ERROR
-        return await self._run_in_executor(self.consumer.commit, message, offsets)
+        # A synchronous `Consumer.commit` has no timeout parameter and can wait forever on a stalled broker, so bound
+        # the wait here. The executor thread may stay busy until librdkafka gives up, but the caller (and any lock
+        # it holds) is released.
+        try:
+            return await asyncio.wait_for(
+                self._run_in_executor(self.consumer.commit, message, offsets), timeout=api_timeout(None)
+            )

asyncio.wait_for only cancels the asyncio wrapper; it cannot stop a callable already running in the executor. After this timeout, commit_offsets releases the consumer lock while consumer.commit may still be running, so retries can concurrently use the same consumer and leave additional default-executor workers stuck. Keep the consumer serialized until the worker exits, or retire/recreate the consumer after a timeout so no further operations can be scheduled against the in-flight commit.

Copilot Copilot commented on code on Sep 30, 2026 10:13 UTC
Code Review
src/karapace/kafka_rest_apis/__init__.py:1
@@ -675,14 +676,19 @@ async def fetch(self, group_name: str, instance: str, content_type: str, *, requ
             formats=request.accepts,
         )
 
+    @staticmethod
+    async def _run_blocking(func: Callable[..., T], *args: object) -> T:
+        """Run a blocking Kafka admin call off the event loop, so a slow broker can't stall every other request."""
+        return await asyncio.get_running_loop().run_in_executor(None, func, *args)

Passing None uses the event loop's shared default executor, which is also used by every AsyncKafkaConsumer operation. During a broker outage, concurrent per-user admin requests can occupy all of its workers, causing consumer start/fetch/commit/stop operations to queue even though the loop itself remains responsive. Use a dedicated bounded executor for admin calls and shut it down with the proxy.

copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Sep 30, 2026 10:13 UTC
Reviewer

<!-- ccr-overview-v2 -->

Copilot review overview

🟡 Changes recommended

Timed-out work can remain active and exhaust the shared executor or race with subsequent consumer operations.

Review effort: Balanced
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> · 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>

<details open>
<summary><strong>Open (2)</strong></summary>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> [Prevent consumer reuse while timed-out commits remain in flight](#discussion_r4143460343) · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> [Use a dedicated bounded executor for admin operations](#discussion_r4143460395) · New

  • </details>

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Adds bounded Kafka client calls and moves REST proxy admin work off the event loop.

    Changes:

  • Applies default timeouts to blocking Kafka operations.

  • Bounds schema-reader watermark queries and async commits.

  • Adds timeout and event-loop responsiveness tests.
  • | File | Description |
    | ---- | ----------- |
    | src/​karapace/​core/​kafka/​common.py | Adds shared default API timeout handling. |
    | src/​karapace/​core/​kafka/​consumer.py | Bounds consumer metadata, offset, and commit waits. |
    | src/​karapace/​core/​kafka/​producer.py | Adds a default flush timeout. |
    | src/​karapace/​core/​schema_reader.py | Bounds watermark queries. |
    | src/​karapace/​kafka_rest_apis/​__init__.py | Offloads admin operations to an executor. |
    | tests/​unit/​core/​kafka/​test_bounded_timeouts.py | Tests bounded Kafka calls. |
    | tests/​unit/​kafka_rest_apis/​test_rest_proxy_event_loop.py | Tests event-loop responsiveness. |
    | tests/​unit/​test_schema_reader.py | Tests bounded schema-reader watermark queries. |
    </details>

    ---

    💡 <a href="/Aiven-Open/karapace/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>

    gqmelo gqmelo commented on code on Sep 30, 2026 11:36 UTC
    Code Review
    src/karapace/core/kafka/consumer.py:1
    @@ -260,7 +254,15 @@ async def commit(
             offsets: list[TopicPartition] | None = None,
         ) -> list[TopicPartition] | None:
             assert self.consumer is not None, self._START_ERROR
    -        return await self._run_in_executor(self.consumer.commit, message, offsets)
    +        # A synchronous `Consumer.commit` has no timeout parameter and can wait forever on a stalled broker, so bound
    +        # the wait here. The executor thread may stay busy until librdkafka gives up, but the caller (and any lock
    +        # it holds) is released.
    +        try:
    +            return await asyncio.wait_for(
    +                self._run_in_executor(self.consumer.commit, message, offsets), timeout=api_timeout(None)
    +            )

    Fixed

    gqmelo gqmelo commented on code on Sep 30, 2026 11:37 UTC
    Code Review
    src/karapace/kafka_rest_apis/__init__.py:1
    @@ -675,14 +676,19 @@ async def fetch(self, group_name: str, instance: str, content_type: str, *, requ
                 formats=request.accepts,
             )
     
    +    @staticmethod
    +    async def _run_blocking(func: Callable[..., T], *args: object) -> T:
    +        """Run a blocking Kafka admin call off the event loop, so a slow broker can't stall every other request."""
    +        return await asyncio.get_running_loop().run_in_executor(None, func, *args)

    Fixed

    github-actions[bot] github-actions[bot] commented on Sep 30, 2026 12:13 UTC
    Bot

    Coverage report

    <img title="Coverage for the whole project went from 87.04% to 87.19%" src="https://img.shields.io/badge/Coverage%20evolution-87%25%20%3E%2087%25-brightgreen.svg"> <img title="97.36% of the statement lines added by this PR are covered" src="https://img.shields.io/badge/PR%20Coverage-97%25-orange.svg"><details><summary>Click to see where and how coverage changed</summary><table><thead>
    <tr><th>File</th><th>Statements</th><th>Missing</th><th>Coverage</th><th>Coverage<br>(new stmts)</th><th>Lines missing</th></tr>
    </thead>
    <tbody><tr>
    <td colspan="6">&nbsp;&nbsp;<b>src/karapace/api/oidc</b></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-8051b5385d5341be49416033622c98d8d08f1f7db8f4db467efbda273f970d06">validator.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-8051b5385d5341be49416033622c98d8d08f1f7db8f4db467efbda273f970d06"><img title="This PR adds 9 to the number of statements in src/karapace/api/oidc/validator.py, taking it from 114 to 123." src="https://img.shields.io/badge/123-%28%2B9%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-8051b5385d5341be49416033622c98d8d08f1f7db8f4db467efbda273f970d06"><img title="This PR doesn't change the number of statements missing coverage in src/karapace/api/oidc/validator.py, which is 6." src="https://img.shields.io/badge/6-%28%C2%B10%29-lightgrey.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-8051b5385d5341be49416033622c98d8d08f1f7db8f4db467efbda273f970d06"><img title="This PR adds 0.58 percentage points to the coverage rate in src/karapace/api/oidc/validator.py, taking it from 93.5% (108/114) to 94.08% (117/123)." src="https://img.shields.io/badge/94%25-%28108/114%20%3E%20117/123%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-8051b5385d5341be49416033622c98d8d08f1f7db8f4db467efbda273f970d06"><img title="This PR does not seem to add statements to src/karapace/api/oidc/validator.py." src="https://img.shields.io/badge/N/A-grey.svg"></a></td><td></td><tr>
    <td colspan="6">&nbsp;&nbsp;<b>src/karapace/core</b></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c70bf297e094de072e79d1574c51c5a2135165041cad966f91267280d6c97ef0">client.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c70bf297e094de072e79d1574c51c5a2135165041cad966f91267280d6c97ef0"><img title="This PR doesn't change the number of statements in src/karapace/core/client.py, which is 159." src="https://img.shields.io/badge/159-%28%C2%B10%29-5d89ba.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c70bf297e094de072e79d1574c51c5a2135165041cad966f91267280d6c97ef0"><img title="This PR removes 1 from the number of statements missing coverage in src/karapace/core/client.py, taking it from 1 to 0." src="https://img.shields.io/badge/0-%28--1%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c70bf297e094de072e79d1574c51c5a2135165041cad966f91267280d6c97ef0"><img title="This PR adds 0.53 percentage points to the coverage rate in src/karapace/core/client.py, taking it from 99.46% (158/159) to 100% (159/159)." src="https://img.shields.io/badge/100%25-%28158/159%20%3E%20159/159%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c70bf297e094de072e79d1574c51c5a2135165041cad966f91267280d6c97ef0"><img title="This PR does not seem to add statements to src/karapace/core/client.py." src="https://img.shields.io/badge/N/A-grey.svg"></a></td><td></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-6cd8290f0758d841ec17abc6efadbe227e2ecd9d462d9a7514fd31e5d754dc2a">schema_reader.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-6cd8290f0758d841ec17abc6efadbe227e2ecd9d462d9a7514fd31e5d754dc2a"><img title="This PR adds 1 to the number of statements in src/karapace/core/schema_reader.py, taking it from 510 to 511." src="https://img.shields.io/badge/511-%28%2B1%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-6cd8290f0758d841ec17abc6efadbe227e2ecd9d462d9a7514fd31e5d754dc2a"><img title="This PR doesn't change the number of statements missing coverage in src/karapace/core/schema_reader.py, which is 0." src="https://img.shields.io/badge/0-%28%C2%B10%29-lightgrey.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-6cd8290f0758d841ec17abc6efadbe227e2ecd9d462d9a7514fd31e5d754dc2a"><img title="This PR adds 0.00 percentage points to the coverage rate in src/karapace/core/schema_reader.py, taking it from 98.89% (510/510) to 98.9% (511/511)." src="https://img.shields.io/badge/98%25-%28510/510%20%3E%20511/511%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-6cd8290f0758d841ec17abc6efadbe227e2ecd9d462d9a7514fd31e5d754dc2a"><img title="In this PR, 14 new statements are added to src/karapace/core/schema_reader.py, 14 of which are covered (100%)." src="https://img.shields.io/badge/100%25-%2814/14%29-brightgreen.svg"></a></td><td></td><tr>
    <td colspan="6">&nbsp;&nbsp;<b>src/karapace/core/coordinator</b></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-88a89e4da8cc2d324cd96a004f7c464aa1c454d2065d83ec9f39ea9a71c39350">schema_coordinator.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-88a89e4da8cc2d324cd96a004f7c464aa1c454d2065d83ec9f39ea9a71c39350"><img title="This PR doesn't change the number of statements in src/karapace/core/coordinator/schema_coordinator.py, which is 514." src="https://img.shields.io/badge/514-%28%C2%B10%29-5d89ba.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-88a89e4da8cc2d324cd96a004f7c464aa1c454d2065d83ec9f39ea9a71c39350"><img title="This PR removes 2 from the number of statements missing coverage in src/karapace/core/coordinator/schema_coordinator.py, taking it from 42 to 40." src="https://img.shields.io/badge/40-%28--2%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-88a89e4da8cc2d324cd96a004f7c464aa1c454d2065d83ec9f39ea9a71c39350"><img title="This PR adds 0.46 percentage points to the coverage rate in src/karapace/core/coordinator/schema_coordinator.py, taking it from 90.67% (472/514) to 91.13% (474/514)." src="https://img.shields.io/badge/91%25-%28472/514%20%3E%20474/514%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-88a89e4da8cc2d324cd96a004f7c464aa1c454d2065d83ec9f39ea9a71c39350"><img title="This PR does not seem to add statements to src/karapace/core/coordinator/schema_coordinator.py." src="https://img.shields.io/badge/N/A-grey.svg"></a></td><td></td><tr>
    <td colspan="6">&nbsp;&nbsp;<b>src/karapace/core/kafka</b></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-e0aecc3b1fb73ff9839103e6ca8124aa7d6085c77fe393753595f392eeb72d35">common.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-e0aecc3b1fb73ff9839103e6ca8124aa7d6085c77fe393753595f392eeb72d35"><img title="This PR adds 6 to the number of statements in src/karapace/core/kafka/common.py, taking it from 89 to 95." src="https://img.shields.io/badge/95-%28%2B6%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-e0aecc3b1fb73ff9839103e6ca8124aa7d6085c77fe393753595f392eeb72d35"><img title="This PR removes 2 from the number of statements missing coverage in src/karapace/core/kafka/common.py, taking it from 7 to 5." src="https://img.shields.io/badge/5-%28--2%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-e0aecc3b1fb73ff9839103e6ca8124aa7d6085c77fe393753595f392eeb72d35"><img title="This PR adds 4.15 percentage points to the coverage rate in src/karapace/core/kafka/common.py, taking it from 87.15% (82/89) to 91.3% (90/95)." src="https://img.shields.io/badge/91%25-%2882/89%20%3E%2090/95%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-e0aecc3b1fb73ff9839103e6ca8124aa7d6085c77fe393753595f392eeb72d35"><img title="In this PR, 21 new statements are added to src/karapace/core/kafka/common.py, 21 of which are covered (100%)." src="https://img.shields.io/badge/100%25-%2821/21%29-brightgreen.svg"></a></td><td></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-b5ce95ec69a641c4e5c47136ee519caacc4458eadda28b4f6691edcec68724ae">consumer.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-b5ce95ec69a641c4e5c47136ee519caacc4458eadda28b4f6691edcec68724ae"><img title="This PR adds 5 to the number of statements in src/karapace/core/kafka/consumer.py, taking it from 150 to 155." src="https://img.shields.io/badge/155-%28%2B5%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-b5ce95ec69a641c4e5c47136ee519caacc4458eadda28b4f6691edcec68724ae"><img title="This PR removes 6 from the number of statements missing coverage in src/karapace/core/kafka/consumer.py, taking it from 17 to 11." src="https://img.shields.io/badge/11-%28--6%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-b5ce95ec69a641c4e5c47136ee519caacc4458eadda28b4f6691edcec68724ae"><img title="This PR adds 4.83 percentage points to the coverage rate in src/karapace/core/kafka/consumer.py, taking it from 88.23% (133/150) to 93.06% (144/155)." src="https://img.shields.io/badge/93%25-%28133/150%20%3E%20144/155%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-b5ce95ec69a641c4e5c47136ee519caacc4458eadda28b4f6691edcec68724ae"><img title="In this PR, 33 new statements are added to src/karapace/core/kafka/consumer.py, 32 of which are covered (96.96%)." src="https://img.shields.io/badge/96%25-%2832/33%29-orange.svg"></a></td><td><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-b5ce95ec69a641c4e5c47136ee519caacc4458eadda28b4f6691edcec68724aeR65-R65">65</a></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c827f6ef29f68a4c58dc9a333b372d7f9db19ff677f40e13259a4d099e28be0e">producer.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c827f6ef29f68a4c58dc9a333b372d7f9db19ff677f40e13259a4d099e28be0e"><img title="This PR doesn't change the number of statements in src/karapace/core/kafka/producer.py, which is 67." src="https://img.shields.io/badge/67-%28%C2%B10%29-5d89ba.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c827f6ef29f68a4c58dc9a333b372d7f9db19ff677f40e13259a4d099e28be0e"><img title="This PR removes 2 from the number of statements missing coverage in src/karapace/core/kafka/producer.py, taking it from 2 to 0." src="https://img.shields.io/badge/0-%28--2%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c827f6ef29f68a4c58dc9a333b372d7f9db19ff677f40e13259a4d099e28be0e"><img title="This PR adds 2.74 percentage points to the coverage rate in src/karapace/core/kafka/producer.py, taking it from 97.26% (65/67) to 100% (67/67)." src="https://img.shields.io/badge/100%25-%2865/67%20%3E%2067/67%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-c827f6ef29f68a4c58dc9a333b372d7f9db19ff677f40e13259a4d099e28be0e"><img title="This PR does not seem to add statements to src/karapace/core/kafka/producer.py." src="https://img.shields.io/badge/N/A-grey.svg"></a></td><td></td><tr>
    <td colspan="6">&nbsp;&nbsp;<b>src/karapace/kafka_rest_apis</b></td><tr>
    <td>&nbsp;&nbsp;<a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-bf1040a2f2782f55f69591098af0c6e637bd9152549524781a2a88577688aa79">__init__.py</a></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-bf1040a2f2782f55f69591098af0c6e637bd9152549524781a2a88577688aa79"><img title="This PR adds 6 to the number of statements in src/karapace/kafka_rest_apis/__init__.py, taking it from 676 to 682." src="https://img.shields.io/badge/682-%28%2B6%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-bf1040a2f2782f55f69591098af0c6e637bd9152549524781a2a88577688aa79"><img title="This PR doesn't change the number of statements missing coverage in src/karapace/kafka_rest_apis/__init__.py, which is 76." src="https://img.shields.io/badge/76-%28%C2%B10%29-lightgrey.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-bf1040a2f2782f55f69591098af0c6e637bd9152549524781a2a88577688aa79"><img title="This PR adds 0.08 percentage points to the coverage rate in src/karapace/kafka_rest_apis/__init__.py, taking it from 88.12% (600/676) to 88.2% (606/682)." src="https://img.shields.io/badge/88%25-%28600/676%20%3E%20606/682%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-bf1040a2f2782f55f69591098af0c6e637bd9152549524781a2a88577688aa79"><img title="In this PR, 46 new statements are added to src/karapace/kafka_rest_apis/__init__.py, 44 of which are covered (95.65%)." src="https://img.shields.io/badge/95%25-%2844/46%29-orange.svg"></a></td><td><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-bf1040a2f2782f55f69591098af0c6e637bd9152549524781a2a88577688aa79R786-R787">786-787</a></td></tbody>
    <tfoot>
    <tr>
    <td><b>Project Total</b></td>

    <td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR adds 27 to the number of statements in the whole project, taking it from 11661 to 11688." src="https://img.shields.io/badge/12k-%28%2B27%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR removes 13 from the number of statements missing coverage in the whole project, taking it from 1297 to 1284." src="https://img.shields.io/badge/1.3k-%28--13%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR adds 0.15 percentage points to the coverage rate in the whole project, taking it from 87.04% (10364/11661) to 87.19% (10404/11688)." src="https://img.shields.io/badge/87%25-%2810k/12k%20%3E%2010k/12k%29-brightgreen.svg"></a></td><td align="center"><a href="https://github.com/Aiven-Open/karapace/pull/1376/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="In this PR, 114 new statements are added to the whole project, 111 of which are covered (97.36%)." src="https://img.shields.io/badge/97%25-%28111/114%29-orange.svg"></a></td><td>&nbsp;</td>
    </tr>
    </tfoot>
    </table>

    <sub>

    This report was generated by python-coverage-comment-action

    </sub>
    </details>

    <!-- This comment was produced by python-coverage-comment-action -->

    muralibasani muralibasani commented on code on Sep 30, 2026 13:05 UTC
    Code Review
    src/karapace/core/kafka/consumer.py:1
    @@ -260,7 +255,19 @@ async def commit(
             offsets: list[TopicPartition] | None = None,
         ) -> list[TopicPartition] | None:
             assert self.consumer is not None, self._START_ERROR
    -        return await self._run_in_executor(self.consumer.commit, message, offsets)
    +        # A synchronous `Consumer.commit` has no timeout parameter and can wait forever on a stalled broker, so bound
    +        # the wait here. Giving up doesn't stop the executor thread, so refuse new commits until it is done instead
    +        # of stacking more stuck workers on the same consumer. The caller (and any lock it holds) is released.
    +        if self._commit_in_flight is not None and not self._commit_in_flight.done():
    +            raise KafkaTimeoutError()
    +        in_flight = self.loop.run_in_executor(None, self.consumer.commit, message, offsets)
    +        # Retrieve the outcome of a commit we stopped waiting for, so it isn't reported as never retrieved.
    +        in_flight.add_done_callback(lambda done: done.cancelled() or done.exception())
    +        self._commit_in_flight = in_flight
    +        try:
    +            return await asyncio.wait_for(asyncio.shield(in_flight), timeout=api_timeout(None))

    Looks like a commit that times out keeps running. (After 30s the handler returns a 500 and releases the consumer lock, but the worker thread is still inside Consumer.commit.)
    I think poll, consume and seek can also overlap the stuck commit.

    muralibasani muralibasani commented on code on Sep 30, 2026 13:08 UTC
    Code Review
    src/karapace/kafka_rest_apis/__init__.py:1
    @@ -817,6 +829,7 @@ async def aclose(self) -> None:
     
                 self.admin_client = None
                 self.consumer_manager = None
    +            self.admin_executor.shutdown(wait=False)

    May be it's closing a proxy that's still in use ?

    gqmelo gqmelo commented on code on Oct 01, 2026 08:45 UTC
    Code Review
    src/karapace/core/kafka/consumer.py:1
    @@ -260,7 +255,19 @@ async def commit(
             offsets: list[TopicPartition] | None = None,
         ) -> list[TopicPartition] | None:
             assert self.consumer is not None, self._START_ERROR
    -        return await self._run_in_executor(self.consumer.commit, message, offsets)
    +        # A synchronous `Consumer.commit` has no timeout parameter and can wait forever on a stalled broker, so bound
    +        # the wait here. Giving up doesn't stop the executor thread, so refuse new commits until it is done instead
    +        # of stacking more stuck workers on the same consumer. The caller (and any lock it holds) is released.
    +        if self._commit_in_flight is not None and not self._commit_in_flight.done():
    +            raise KafkaTimeoutError()
    +        in_flight = self.loop.run_in_executor(None, self.consumer.commit, message, offsets)
    +        # Retrieve the outcome of a commit we stopped waiting for, so it isn't reported as never retrieved.
    +        in_flight.add_done_callback(lambda done: done.cancelled() or done.exception())
    +        self._commit_in_flight = in_flight
    +        try:
    +            return await asyncio.wait_for(asyncio.shield(in_flight), timeout=api_timeout(None))

    yes, that is true. Although I believe it shouldn't exactly be a problem because we are passing explicit offsets to self.consumer.commit. But maybe it is better to add the same pattern to all methods and fail-fast if something is still in flight.

    gqmelo gqmelo commented on code on Oct 01, 2026 09:01 UTC
    Code Review
    src/karapace/kafka_rest_apis/__init__.py:1
    @@ -817,6 +829,7 @@ async def aclose(self) -> None:
     
                 self.admin_client = None
                 self.consumer_manager = None
    +            self.admin_executor.shutdown(wait=False)

    Hmm, but I don't think this adds any additional failure.

    This is called from here: https://github.com/Aiven-Open/karapace/blob/34581233f5c6ffff7a9c1a61a949675e47239dc0/src/karapace/kafka_rest_apis/__init__.py#L145

    And currently it doesn't seem to consider whether there is a pending request. If there is, I would expect something to fail anyway. In this case, it will be just a different exception (the executor refusing to run something after shutdown).

    gqmelo gqmelo commented on Oct 12, 2026 14:24 UTC
    Author

    Closing this in favour of the actual bugfix: https://github.com/confluentinc/librdkafka/pull/5628

    Reviewers
    Assignees
    gqmelo gqmelo
    Labels
    risk: low confidence: 84.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 60.8% (MEDIUM) · Higher = larger blast radius
    60.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.2% · Lower = rushed or shallow review
    84.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 9.6% · Safe to merge
    9.6%