|
1 | 1 | import asyncio |
| 2 | +import os |
2 | 3 | import re |
3 | 4 | import sys |
4 | 5 | import time |
@@ -1541,3 +1542,67 @@ def test_default_attributes(sentry_init, capture_envelopes): |
1541 | 1542 | "sentry.release": {"value": "1.0.0", "type": "string"}, |
1542 | 1543 | "sentry.origin": {"value": "manual", "type": "string"}, |
1543 | 1544 | } |
| 1545 | + |
| 1546 | + |
| 1547 | +@pytest.mark.skipif( |
| 1548 | + sys.platform == "win32" |
| 1549 | + or not hasattr(os, "fork") |
| 1550 | + or not hasattr(os, "register_at_fork"), |
| 1551 | + reason="requires POSIX fork and os.register_at_fork (Python 3.7+)", |
| 1552 | +) |
| 1553 | +def test_span_batcher_lock_reset_in_child_after_fork(sentry_init): |
| 1554 | + """Regression test for the SpanBatcher fork-deadlock fix. |
| 1555 | +
|
| 1556 | + If os.fork() runs while another thread holds SpanBatcher._lock, the |
| 1557 | + child inherits the lock locked. The holding thread does not exist in |
| 1558 | + the child, so the lock can never be released and _ensure_thread |
| 1559 | + deadlocks forever. The after-fork hook must replace the lock with a |
| 1560 | + fresh one in the child and reset |
| 1561 | + _flusher / _flusher_pid / _span_buffer / _running_size / _active / |
| 1562 | + _flush_event. |
| 1563 | + """ |
| 1564 | + sentry_init( |
| 1565 | + traces_sample_rate=1.0, |
| 1566 | + _experiments={"trace_lifecycle": "stream"}, |
| 1567 | + ) |
| 1568 | + batcher = sentry_sdk.get_client().span_batcher |
| 1569 | + assert batcher is not None |
| 1570 | + |
| 1571 | + original_lock = batcher._lock |
| 1572 | + original_lock.acquire() |
| 1573 | + |
| 1574 | + batcher._span_buffer["test-trace-id"].append(object()) |
| 1575 | + batcher._running_size["test-trace-id"] = 42 |
| 1576 | + batcher._active.flag = True |
| 1577 | + batcher._flush_event.set() |
| 1578 | + batcher._running = False |
| 1579 | + |
| 1580 | + pid = os.fork() |
| 1581 | + if pid == 0: |
| 1582 | + replaced = batcher._lock is not original_lock |
| 1583 | + unheld = batcher._lock.acquire(blocking=False) |
| 1584 | + |
| 1585 | + flusher_reset = batcher._flusher is None and batcher._flusher_pid is None |
| 1586 | + span_buffer_reset = len(batcher._span_buffer) == 0 |
| 1587 | + running_size_reset = len(batcher._running_size) == 0 |
| 1588 | + |
| 1589 | + active_reset = not getattr(batcher._active, "flag", False) |
| 1590 | + event_reset = not batcher._flush_event.is_set() |
| 1591 | + running_reset = batcher._running is True |
| 1592 | + |
| 1593 | + os._exit( |
| 1594 | + 0 |
| 1595 | + if replaced |
| 1596 | + and unheld |
| 1597 | + and flusher_reset |
| 1598 | + and span_buffer_reset |
| 1599 | + and running_size_reset |
| 1600 | + and active_reset |
| 1601 | + and event_reset |
| 1602 | + and running_reset |
| 1603 | + else 1 |
| 1604 | + ) |
| 1605 | + |
| 1606 | + original_lock.release() |
| 1607 | + _, status = os.waitpid(pid, 0) |
| 1608 | + assert os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0 |
0 commit comments