From 71817736f700ebfcf388d29baf823a0f9504f825 Mon Sep 17 00:00:00 2001 From: Shreehitha Arushan Date: Sun, 15 Nov 2026 19:14:33 +0530 Subject: [PATCH] GH-55605: [R] Don't abort when comparing R extension types off the R thread --- r/src/extension-impl.cpp | 11 +++++++++-- r/tests/testthat/test-extension.R | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/r/src/extension-impl.cpp b/r/src/extension-impl.cpp index c470beda8c58..668940e226b2 100644 --- a/r/src/extension-impl.cpp +++ b/r/src/extension-impl.cpp @@ -37,7 +37,8 @@ bool RExtensionType::ExtensionEquals(const arrow::ExtensionType& other) const { } // With any ambiguity, we need to materialize the R6 instance and call its - // ExtensionEquals method. We can't do this on the non-R thread. + // ExtensionEquals method. This is only possible from the R thread or from + // inside RunWithCapturedR(). arrow::Result result = SafeCallIntoR( [&]() { cpp11::environment instance = r6_instance(); @@ -52,8 +53,14 @@ bool RExtensionType::ExtensionEquals(const arrow::ExtensionType& other) const { }, "RExtensionType$ExtensionEquals()"); + // An error here means R could not be called from this thread (e.g., when a + // Dataset is scanned on worker threads). Throwing would terminate the process, + // because this is not the R thread (GH-55605). Fall back to the default + // ExtensionEquals() behaviour instead: the serialized metadata differs, so the + // types are not equal. If the R method itself errored, RunWithCapturedR() + // re-raises that error. if (!result.ok()) { - throw std::runtime_error(result.status().message()); + return false; } return result.ValueUnsafe(); diff --git a/r/tests/testthat/test-extension.R b/r/tests/testthat/test-extension.R index d82c96fa5e71..6ec45dc8478e 100644 --- a/r/tests/testthat/test-extension.R +++ b/r/tests/testthat/test-extension.R @@ -172,6 +172,32 @@ test_that("extension subclasses can override the ExtensionEquals method", { unregister_extension_type("some_extension_subclass") }) +test_that("comparing extension types on a non-R thread does not abort (GH-55605)", { + skip_if_not_available("dataset") + + unit_type <- function(unit) { + new_extension_type(int32(), "arrow.test.unit", charToRaw(unit)) + } + register_extension_type(unit_type("m")) + on.exit(unregister_extension_type("arrow.test.unit")) + + tf <- tempfile() + dir.create(tf) + on.exit(unlink(tf, recursive = TRUE), add = TRUE) + + for (unit in c("m", "km")) { + write_ipc_file( + arrow_table(x = unit_type(unit)$WrapArray(Array$create(1:3))), + file.path(tf, paste0(unit, ".arrow")) + ) + } + ds <- open_dataset(tf, format = "arrow") + + # The parts have different extension metadata, so they can't be combined + expect_error(dplyr::collect(ds), "not permitted") + expect_error(Scanner$create(ds)$ToTable(), "not permitted") +}) + test_that("vctrs extension type works", { custom_vctr <- vctrs::new_vctr( 1:4,