| 1 |
import gc |
| 2 |
import logging |
| 3 |
import os |
| 4 |
import pprint |
| 5 |
import uuid |
| 6 |
|
| 7 |
import billiard as multiprocessing |
| 8 |
import pytest |
| 9 |
from kombu import Queue |
| 10 |
from kombu.pools import connections |
| 11 |
|
| 12 |
from celery import Celery, _state |
| 13 |
from celery.contrib.testing.worker import start_worker |
| 14 |
|
| 15 |
QOS_GLOBAL_ERROR = "qos.global not allowed" |
| 16 |
|
| 17 |
logger = logging.getLogger(__name__) |
| 18 |
logging.basicConfig(level=logging.INFO) |
| 19 |
|
| 20 |
|
| 21 |
def create_app(queue_name: str) -> Celery: |
| 22 |
rabbitmq_user = os.environ.get("RABBITMQ_DEFAULT_USER", "guest") |
| 23 |
rabbitmq_pass = os.environ.get("RABBITMQ_DEFAULT_PASS", "guest") |
| 24 |
redis_host = os.environ.get("REDIS_HOST", "localhost") |
| 25 |
redis_port = os.environ.get("REDIS_PORT", "6379") |
| 26 |
|
| 27 |
broker_url = os.environ.get("TEST_BROKER", f"pyamqp://{rabbitmq_user}:{rabbitmq_pass}@localhost:5672//") |
| 28 |
backend_url = os.environ.get("TEST_BACKEND", f"redis://{redis_host}:{redis_port}/0") |
| 29 |
|
| 30 |
app = Celery("quorum_qos_race", broker=broker_url, backend=backend_url) |
| 31 |
|
| 32 |
app.conf.task_queues = [ |
| 33 |
Queue( |
| 34 |
name=queue_name, |
| 35 |
queue_arguments={"x-queue-type": "quorum"}, |
| 36 |
) |
| 37 |
] |
| 38 |
app.conf.task_default_queue = queue_name |
| 39 |
app.conf.worker_prefetch_multiplier = 1 |
| 40 |
app.conf.task_acks_late = True |
| 41 |
app.conf.task_reject_on_worker_lost = True |
| 42 |
app.conf.broker_transport_options = {"confirm_publish": True} |
| 43 |
|
| 44 |
return app |
| 45 |
|
| 46 |
|
| 47 |
def dummy_task_factory(app: Celery, simulate_qos_issue: bool): |
| 48 |
@app.task(name="dummy_task") |
| 49 |
def dummy_task(): |
| 50 |
if simulate_qos_issue: |
| 51 |
raise Exception("qos.global not allowed on quorum queues (simulated)") |
| 52 |
return "ok" |
| 53 |
return dummy_task |
| 54 |
|
| 55 |
|
| 56 |
def run_worker(simulate_qos_issue: bool, result_queue: multiprocessing.Queue): |
| 57 |
queue_name = f"race_quorum_queue_{uuid.uuid4().hex}" |
| 58 |
app = create_app(queue_name) |
| 59 |
logger.info("[Celery config snapshot]:\n%s", pprint.pformat(dict(app.conf))) |
| 60 |
task = dummy_task_factory(app, simulate_qos_issue) |
| 61 |
|
| 62 |
try: |
| 63 |
with start_worker( |
| 64 |
app, |
| 65 |
queues=[queue_name], |
| 66 |
loglevel="INFO", |
| 67 |
perform_ping_check=False, |
| 68 |
shutdown_timeout=15, |
| 69 |
): |
| 70 |
res = task.delay() |
| 71 |
try: |
| 72 |
result = res.get(timeout=10) |
| 73 |
result_queue.put({"status": "ok", "result": result}) |
| 74 |
except Exception as e: |
| 75 |
result_queue.put({"status": "error", "reason": str(e)}) |
| 76 |
except Exception as e: |
| 77 |
logger.exception("[worker %s] external failure", simulate_qos_issue) |
| 78 |
result_queue.put({"status": "external_failure", "reason": str(e)}) |
| 79 |
finally: |
| 80 |
if result_queue.empty(): |
| 81 |
result_queue.put({"status": "crash", "reason": "Worker crashed without reporting"}) |
| 82 |
|
| 83 |
|
| 84 |
@pytest.mark.amqp |
| 85 |
@pytest.mark.timeout(90) |
| 86 |
def test_rabbitmq_quorum_qos_visibility_race(): |
| 87 |
try: |
| 88 |
multiprocessing.set_start_method("spawn", force=True) |
| 89 |
except RuntimeError: |
| 90 |
pass |
| 91 |
|
| 92 |
results = [] |
| 93 |
processes = [] |
| 94 |
queues = [] |
| 95 |
|
| 96 |
for i in range(3): |
| 97 |
simulate = (i == 0) |
| 98 |
q = multiprocessing.Queue() |
| 99 |
queues.append(q) |
| 100 |
|
| 101 |
p = multiprocessing.Process(target=run_worker, args=(simulate, q)) |
| 102 |
p.daemon = True |
| 103 |
processes.append(p) |
| 104 |
p.start() |
| 105 |
|
| 106 |
try: |
| 107 |
for i, (p, q) in enumerate(zip(processes, queues)): |
| 108 |
try: |
| 109 |
p.join(timeout=30) |
| 110 |
if p.is_alive(): |
| 111 |
p.terminate() |
| 112 |
p.join(timeout=10) |
| 113 |
results.append({"status": "timeout", "reason": f"[worker {i}] timeout"}) |
| 114 |
else: |
| 115 |
try: |
| 116 |
results.append(q.get(timeout=5)) |
| 117 |
except Exception as e: |
| 118 |
results.append({"status": "error", "reason": f"Result error: {str(e)}"}) |
| 119 |
except Exception: |
| 120 |
try: |
| 121 |
results.append(q.get(timeout=5)) |
| 122 |
except Exception: |
| 123 |
results.append({"status": "crash", "reason": f"Worker {i} crashed and gave no result"}) |
| 124 |
|
| 125 |
if any(QOS_GLOBAL_ERROR in r.get("reason", "").lower() for r in results): |
| 126 |
pytest.xfail("Detected global QoS usage on quorum queue (simulated failure)") |
| 127 |
finally: |
| 128 |
for i, p in enumerate(processes): |
| 129 |
if p.is_alive(): |
| 130 |
p.terminate() |
| 131 |
p.join(timeout=10) |
| 132 |
|
| 133 |
# Reset Kombu connection pools (safe public API) |
| 134 |
try: |
| 135 |
connections.clear() |
| 136 |
except Exception: |
| 137 |
pass |
| 138 |
|
| 139 |
# Reset Celery app/task global state |
| 140 |
_state._set_current_app(None) |
| 141 |
_state._task_stack.__init__() # reinitialize stack to avoid stale state |
| 142 |
|
| 143 |
# Force garbage collection |
| 144 |
gc.collect() |
| 145 |
|
| 146 |
# Reset multiprocessing to default (may help restore test_multiprocess_producer expectations) |
| 147 |
if multiprocessing.get_start_method(allow_none=True) == "spawn": |
| 148 |
try: |
| 149 |
multiprocessing.set_start_method("fork", force=True) |
| 150 |
except RuntimeError: |
| 151 |
pass |