Skip to content

Commit 61c3472

Browse files
authored
fix: restore floating-point rounding mode after panic (apache#26412)
## Which issue does this PR close? - Closes apache#7356. ## Rationale for this change A caught panic in `alter_fp_rounding_mode` leaves the thread using the temporary rounding mode, which can affect later floating-point calculations on that thread. ## What changes are included in this PR? Restore the saved rounding mode with a private `Drop` guard so cleanup also runs during unwinding. The change is confined to `rounding.rs` and adds no dependency or public API. ## What is the testing strategy for this PR? The regression test starts in downward rounding mode, panics during an upward-rounded operation, catches the panic, and checks that downward rounding was restored. It restores the test thread's original mode before asserting. This requires a Rust unit test because SQL and the DataFrame API cannot directly supply the panicking callback. The test was run against the unfixed implementation and failed because upward rounding remained active. Validation: - `cargo fmt --all` and `cargo fmt --all -- --check` passed. - `cargo test --profile ci -p datafusion-common -p datafusion-expr-common` passed: 847 unit tests and 89 documentation tests, with five documentation tests ignored. - `cargo clippy -p datafusion-common -p datafusion-expr-common --all-targets --all-features -- -D warnings` passed. `cargo clippy --all-targets --all-features -- -D warnings` reported three pre-existing `mutable_key_type` errors in `datafusion/optimizer/src/push_down_filter.rs`, tracked by apache#26181. Its remaining bundled Protobuf build was stopped after these errors. The full lint suite, SQL logic suite, and extended workspace tests were started but did not complete locally. The separate core and CLI test run was not reached. ## Are there any user-facing changes? After a caught panic, subsequent floating-point calculations on the same thread retain the rounding mode that was active before the operation. No public API or normal SQL behavior changes.
1 parent 3470bd0 commit 61c3472

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

‎datafusion/common/src/rounding.rs‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,18 @@ where
271271
not(target_os = "windows")
272272
))]
273273
unsafe {
274-
let current = fegetround();
274+
struct RoundingModeGuard(i32);
275+
276+
impl Drop for RoundingModeGuard {
277+
fn drop(&mut self) {
278+
unsafe { fesetround(self.0) };
279+
}
280+
}
281+
282+
// Restore the previous mode even if the operation panics.
283+
let _guard = RoundingModeGuard(fegetround());
275284
fesetround(if UPPER { FE_UPWARD } else { FE_DOWNWARD });
276-
let result = operation(lhs, rhs);
277-
fesetround(current);
278-
result
285+
operation(lhs, rhs)
279286
}
280287
#[cfg(any(
281288
not(any(target_arch = "x86_64", target_arch = "aarch64")),
@@ -288,6 +295,35 @@ where
288295
mod tests {
289296
use super::{next_down, next_up};
290297

298+
#[cfg(all(
299+
any(target_arch = "x86_64", target_arch = "aarch64"),
300+
not(target_os = "windows")
301+
))]
302+
#[test]
303+
fn test_alter_fp_rounding_mode_restores_after_panic() {
304+
use super::{FE_DOWNWARD, alter_fp_rounding_mode, fegetround, fesetround};
305+
use crate::ScalarValue;
306+
307+
unsafe {
308+
let original = fegetround();
309+
fesetround(FE_DOWNWARD);
310+
311+
let result = std::panic::catch_unwind(|| {
312+
alter_fp_rounding_mode::<true, _>(
313+
&ScalarValue::Null,
314+
&ScalarValue::Null,
315+
|_, _| panic!("operation panicked"),
316+
)
317+
});
318+
319+
let restored = fegetround();
320+
fesetround(original);
321+
322+
assert!(result.is_err());
323+
assert_eq!(restored, FE_DOWNWARD);
324+
}
325+
}
326+
291327
#[test]
292328
fn test_next_down() {
293329
let x = 1.0f64;

0 commit comments

Comments
 (0)