Commit 8568a1f
Capture Rust backtraces for Postgres ERRORs raised through pg_sys FFIcalls (pgcentralfoundation#2262)
When Rust code calls a pg_sys function (e.g. pg_sys::relation_open())
and that function internally raises an ERROR via elog/ereport, the
longjmp is caught by pg_guard_ffi_boundary and converted to a Rust
panic. The panic hook captures a Rust backtrace at that point, but it
was never attached to the error — the CaughtError::PostgresError path
went through pg_re_throw() which bypassed do_ereport() entirely, so the
backtrace was discarded.
Fix this by:
- In `downcast_panic_payload()`, when unwrapping a
`CaughtError::PostgresError`, attach the backtrace from PANIC_LOCATION
to the error report. This is the common path used by both `PgTryBuilder`
and `run_guarded`, so the backtrace is available regardless of how the
error is caught.
- In `run_guarded(),` route CaughtError::PostgresError through
GuardAction::Report (and thus do_ereport) instead of
GuardAction::ReThrow (and pg_re_throw). This ensures the backtrace
appears in the ERROR's DETAIL line when the error propagates all the way
out.
- In `pg_guard_ffi_boundary`, call `FlushErrorState(`) after`
CopyErrorData` to properly clean the original error off Postgres'
fixed-size errordata[] stack before do_ereport pushes a new entry.
- Remove the now-unused `GuardAction::ReThrow` variant and
`pg_re_throw()` call.
---------
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>1 parent 86ad24a commit 8568a1f
3 files changed
Lines changed: 99 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
244 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
245 | 247 | | |
| 248 | + | |
246 | 249 | | |
247 | 250 | | |
248 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
357 | 360 | | |
358 | 361 | | |
359 | 362 | | |
| |||
386 | 389 | | |
387 | 390 | | |
388 | 391 | | |
389 | | - | |
390 | 392 | | |
391 | 393 | | |
392 | 394 | | |
| |||
428 | 430 | | |
429 | 431 | | |
430 | 432 | | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | 433 | | |
442 | 434 | | |
443 | 435 | | |
| |||
454 | 446 | | |
455 | 447 | | |
456 | 448 | | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
461 | 454 | | |
462 | 455 | | |
463 | 456 | | |
| |||
470 | 463 | | |
471 | 464 | | |
472 | 465 | | |
473 | | - | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
474 | 480 | | |
475 | 481 | | |
476 | 482 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
69 | 130 | | |
70 | 131 | | |
71 | 132 | | |
| |||
0 commit comments