-
Notifications
You must be signed in to change notification settings - Fork 915
Segmentation Fault on Windows when using asyncio.to_thread on consumer with test container #1953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @trajano, it appears the issue might be coming from the asyncio library and not confluent-kafka-python. Upon a quick search through https://github.com/python/cpython issues, I found this resolved issue python/cpython#123321 which seems to be related. What Python version are you running? One of the later patch versions of 3.12 (unsure which) and all version of 3.13 should have this issue fixed. |
I'm on windows and Python 3.13.2 installed via scoop. |
How are you triggering the cancellation? Just to try and rule out confluent-kafka-python's involvement, can you try running this script to see if the causes the same behavior? The script schedules a import asyncio
import time
import sys
async def sleep_in_thread(seconds: float):
print("Sleeping for", seconds, "seconds...")
await asyncio.to_thread(time.sleep, seconds)
print("Finished sleeping")
async def main():
task = asyncio.create_task(sleep_in_thread(10))
await asyncio.sleep(3)
print("Cancelling the task...")
task.cancel()
try:
await task
except asyncio.CancelledError:
print("Task was cancelled")
if __name__ == "__main__":
asyncio.run(main()) |
no error on your code. Putting it in a test as well works.
|
Thanks for running the snippet, I'm more inclined to think that this is an issue with the confluent-kafka-python library. BTW I took another look at your workaround and wanted to point out that the call to Can you try adding |
I'll probably close this bug shortly. |
Ok got it to fail, but with test container.
Note test "passes" but will yield a segfault after. |
So it appears to be with the test container example because this works
|
I'll close this one for now I am suspecting it's something on testcontainers rather than this project right now. I'll reopen if the testcontainers team pass the blame back. |
I'm not sure if the synchronous fixture plays well with the async test.
|
This works though import asyncio
import time
import sys
from confluent_kafka import Consumer
import pytest
from testcontainers.core.waiting_utils import wait_for_logs
from testcontainers.kafka import KafkaContainer
@pytest.fixture(scope="module")
def kafka_container():
"""Provide a temporary Redis container."""
with KafkaContainer("mirror.gcr.io/confluentinc/cp-kafka:7.6.0") as container:
wait_for_logs(container, "Awaiting socket connections on 0.0.0.0:9093")
for topic in ["mytopic"]:
container.exec(
f"kafka-topics "
f"--bootstrap-server localhost:9092 "
f"--create "
f"--topic {topic} "
f"--partitions 3 "
f"--replication-factor 1",
)
yield container
async def main(consumer):
await asyncio.to_thread(consumer.poll, timeout=10)
@pytest.mark.asyncio(loop_scope="function")
async def test_sample(kafka_container):
consumer = Consumer({
'bootstrap.servers': kafka_container.get_bootstrap_server(),
'group.id': 'mygroup',
'auto.offset.reset': 'earliest',
"debug":"all"})
consumer.subscribe(['mytopic'])
await main(consumer)
consumer.close() |
The only thing I can think of why the blame would switch back to this project is if there was something in the C code that didn't release some sort of connection or thread. At that point I am not certain what to do :) |
When doing
If I trigger a cancellation on
kafka_consumer
, on Windows I get a Segmentation Fault.I have this workaround for now
UPDATE: here's how I start up Kafka locally in a docker compose
The text was updated successfully, but these errors were encountered: