(self)
| 1561 | |
| 1562 | @pytest.mark.threading |
| 1563 | def test_cancellation(self): |
| 1564 | if (threading.current_thread().ident != |
| 1565 | threading.main_thread().ident): |
| 1566 | pytest.skip("test only works from main Python thread") |
| 1567 | |
| 1568 | def signal_from_thread(): |
| 1569 | # Give our workload a chance to start up |
| 1570 | time.sleep(0.2) |
| 1571 | signal.raise_signal(signal.SIGINT) |
| 1572 | |
| 1573 | # We start with a small CSV reading workload and increase its size |
| 1574 | # until it's large enough to get an interruption during it, even in |
| 1575 | # release mode on fast machines. |
| 1576 | last_duration = 0.0 |
| 1577 | workload_size = 100_000 |
| 1578 | attempts = 0 |
| 1579 | |
| 1580 | while last_duration < 5.0 and attempts < 10: |
| 1581 | print("workload size:", workload_size) |
| 1582 | large_csv = b"a,b,c\n" + b"1,2,3\n" * workload_size |
| 1583 | exc_info = None |
| 1584 | |
| 1585 | try: |
| 1586 | # We use a signal fd to reliably ensure that the signal |
| 1587 | # has been delivered to Python, regardless of how exactly |
| 1588 | # it was caught. |
| 1589 | with util.signal_wakeup_fd() as sigfd: |
| 1590 | try: |
| 1591 | t = threading.Thread(target=signal_from_thread) |
| 1592 | t.start() |
| 1593 | t1 = time.time() |
| 1594 | try: |
| 1595 | self.read_bytes(large_csv) |
| 1596 | except KeyboardInterrupt as e: |
| 1597 | exc_info = e |
| 1598 | last_duration = time.time() - t1 |
| 1599 | finally: |
| 1600 | # Wait for signal to arrive if it didn't already, |
| 1601 | # to avoid getting a KeyboardInterrupt after the |
| 1602 | # `except` block below. |
| 1603 | select.select([sigfd], [], [sigfd], 10.0) |
| 1604 | |
| 1605 | except KeyboardInterrupt: |
| 1606 | # KeyboardInterrupt didn't interrupt `read_bytes` above. |
| 1607 | pass |
| 1608 | |
| 1609 | if exc_info is not None: |
| 1610 | # We managed to get `self.read_bytes` interrupted, see if it |
| 1611 | # was actually interrupted inside Arrow C++ or in the Python |
| 1612 | # scaffolding. |
| 1613 | if exc_info.__context__ is not None: |
| 1614 | # Interrupted inside Arrow C++, we're satisfied now |
| 1615 | break |
| 1616 | |
| 1617 | # Increase workload size to get a better chance |
| 1618 | workload_size = workload_size * 3 |
| 1619 | |
| 1620 | if exc_info is None: |
nothing calls this directly
no test coverage detected