Commit 8719c6c
committed
[SPARK-55240][CORE] Refactor LazyTry stacktrace handling to use wrapper instead of suppressed exception
### What changes were proposed in this pull request?
This PR refactors how `doTryWithCallerStacktrace` and `getTryWithCallerStacktrace` handle stacktrace stitching, with improved behavior for `LazyTry`:
**Key changes:**
1. **New internal wrapper class `TryStackTraceWrapper`**: Instead of modifying the original exception's suppressed list in `doTryWithCallerStacktrace`, we now wrap the exception in a `TryStackTraceWrapper` that carries:
- The original exception
- The depth of stacktrace frames to preserve (optimization: just an int, not a copy of the array)
- An `OriginalTryStackTraceException` holding the original stacktrace for later use
2. **Conditional suppressed exception in `LazyTry`**:
- `LazyTry` now tracks whether the lazy value has been accessed (`materialized` flag)
- On **first access**: No suppressed exception is added (clean output since the caller context is the same as initialization)
- On **subsequent accesses**: The original stacktrace is added as a suppressed exception to help with debugging
3. **`getTryWithCallerStacktrace` now accepts `isFirstAccess` parameter**:
- Default is `true` (no suppressed exception added)
- When `false`, adds the original stacktrace as suppressed if not already present (idempotent check)
### Why are the changes needed?
1. **Cleaner first-access output**: On the first access to a `LazyTry` value, the suppressed exception showing the "original" stacktrace is redundant because it's essentially the same context. Removing it makes error output cleaner.
2. **Preserved debugging info for subsequent accesses**: When accessing a failed `LazyTry` from a different call site, the suppressed exception showing where the error originally occurred is valuable for debugging.
3. **Better encapsulation**: The wrapper is internal-only and never exposed to users. The original exception type is preserved when thrown.
### Does this PR introduce _any_ user-facing change?
Yes:
- On first access to a failed `LazyTry`, exceptions will no longer have a suppressed `OriginalTryStackTraceException` (cleaner output)
- On subsequent accesses, the behavior is unchanged (suppressed exception shows original stacktrace)
### How was this patch tested?
Existing unit tests in `UtilsSuite` and `LazyTrySuite` updated and passing.
### Was this patch authored or co-authored using generative AI tooling?
Yes. cursor 2.4
Closes apache#54007 from cloud-fan/error.
Authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>1 parent 778661a commit 8719c6c
File tree
3 files changed
+93
-79
lines changed- core/src
- main/scala/org/apache/spark/util
- test/scala/org/apache/spark/util
3 files changed
+93
-79
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
48 | 51 | | |
49 | 52 | | |
50 | 53 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
59 | 63 | | |
60 | 64 | | |
61 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1368 | 1368 | | |
1369 | 1369 | | |
1370 | 1370 | | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
1371 | 1376 | | |
1372 | | - | |
1373 | | - | |
1374 | | - | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
1375 | 1391 | | |
1376 | 1392 | | |
1377 | 1393 | | |
1378 | 1394 | | |
1379 | 1395 | | |
1380 | 1396 | | |
1381 | 1397 | | |
1382 | | - | |
1383 | | - | |
1384 | | - | |
| 1398 | + | |
| 1399 | + | |
1385 | 1400 | | |
1386 | 1401 | | |
1387 | 1402 | | |
| |||
1392 | 1407 | | |
1393 | 1408 | | |
1394 | 1409 | | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
1395 | 1414 | | |
1396 | 1415 | | |
1397 | 1416 | | |
1398 | 1417 | | |
1399 | 1418 | | |
1400 | 1419 | | |
1401 | 1420 | | |
1402 | | - | |
1403 | | - | |
1404 | | - | |
1405 | | - | |
1406 | | - | |
1407 | | - | |
1408 | | - | |
1409 | | - | |
1410 | | - | |
1411 | | - | |
1412 | | - | |
1413 | | - | |
1414 | | - | |
1415 | | - | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
1416 | 1430 | | |
1417 | | - | |
1418 | 1431 | | |
1419 | 1432 | | |
1420 | 1433 | | |
| |||
1424 | 1437 | | |
1425 | 1438 | | |
1426 | 1439 | | |
1427 | | - | |
1428 | | - | |
1429 | | - | |
1430 | | - | |
1431 | | - | |
1432 | | - | |
1433 | | - | |
| 1440 | + | |
| 1441 | + | |
1434 | 1442 | | |
1435 | 1443 | | |
| 1444 | + | |
1436 | 1445 | | |
1437 | 1446 | | |
1438 | | - | |
1439 | | - | |
1440 | | - | |
1441 | | - | |
1442 | | - | |
1443 | | - | |
1444 | | - | |
1445 | | - | |
1446 | | - | |
1447 | | - | |
1448 | | - | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
1449 | 1452 | | |
1450 | 1453 | | |
1451 | 1454 | | |
1452 | | - | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
1453 | 1468 | | |
1454 | 1469 | | |
1455 | 1470 | | |
| |||
Lines changed: 27 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1640 | 1640 | | |
1641 | 1641 | | |
1642 | 1642 | | |
1643 | | - | |
| 1643 | + | |
1644 | 1644 | | |
1645 | 1645 | | |
1646 | 1646 | | |
| |||
1669 | 1669 | | |
1670 | 1670 | | |
1671 | 1671 | | |
1672 | | - | |
1673 | | - | |
1674 | | - | |
1675 | | - | |
1676 | | - | |
1677 | | - | |
1678 | | - | |
1679 | | - | |
1680 | | - | |
1681 | | - | |
1682 | | - | |
1683 | | - | |
1684 | | - | |
1685 | | - | |
1686 | | - | |
1687 | | - | |
1688 | | - | |
1689 | | - | |
1690 | | - | |
1691 | | - | |
| 1672 | + | |
| 1673 | + | |
1692 | 1674 | | |
1693 | 1675 | | |
1694 | 1676 | | |
| |||
1715 | 1697 | | |
1716 | 1698 | | |
1717 | 1699 | | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
1718 | 1718 | | |
1719 | 1719 | | |
1720 | 1720 | | |
| |||
1760 | 1760 | | |
1761 | 1761 | | |
1762 | 1762 | | |
1763 | | - | |
1764 | | - | |
1765 | | - | |
1766 | | - | |
1767 | | - | |
1768 | | - | |
1769 | | - | |
1770 | | - | |
1771 | 1763 | | |
1772 | 1764 | | |
1773 | 1765 | | |
1774 | 1766 | | |
1775 | 1767 | | |
1776 | | - | |
| 1768 | + | |
| 1769 | + | |
1777 | 1770 | | |
1778 | 1771 | | |
1779 | 1772 | | |
| |||
1821 | 1814 | | |
1822 | 1815 | | |
1823 | 1816 | | |
1824 | | - | |
| 1817 | + | |
| 1818 | + | |
1825 | 1819 | | |
1826 | 1820 | | |
1827 | 1821 | | |
| |||
1865 | 1859 | | |
1866 | 1860 | | |
1867 | 1861 | | |
1868 | | - | |
| 1862 | + | |
| 1863 | + | |
1869 | 1864 | | |
1870 | 1865 | | |
1871 | 1866 | | |
| |||
0 commit comments