Bound blocking Kafka client calls #1376
Deploying karapace with <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> Cloudflare Pages
<table><tr><td><strong>Latest commit:</strong> </td><td>
<code>3458123</code>
</td></tr>
<tr><td><strong>Status:</strong></td><td> ✅ 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>
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.
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.
<!-- 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>
</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:
| 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>
Fixed
Fixed
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"> <b>src/karapace/api/oidc</b></td><tr>
<td> <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"> <b>src/karapace/core</b></td><tr>
<td> <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> <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"> <b>src/karapace/core/coordinator</b></td><tr>
<td> <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"> <b>src/karapace/core/kafka</b></td><tr>
<td> <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> <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> <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"> <b>src/karapace/kafka_rest_apis</b></td><tr>
<td> <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> </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 -->
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.
May be it's closing a proxy that's still in use ?
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.
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).
Closing this in favour of the actual bugfix: https://github.com/confluentinc/librdkafka/pull/5628
gqmelo
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,committedandflushwait forever without a timeout.After a broker restart the schema reader thread hung in
get_watermark_offsetsand 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:
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.