Skip to content

GH-49995: [R][Wasm] Fix threading issue under Wasm/Emscripten#49996

Draft
amoeba wants to merge 22 commits into
apache:mainfrom
amoeba:GH-49995--r-wasm-threading
Draft

GH-49995: [R][Wasm] Fix threading issue under Wasm/Emscripten#49996
amoeba wants to merge 22 commits into
apache:mainfrom
amoeba:GH-49995--r-wasm-threading

Conversation

@amoeba

@amoeba amoeba commented May 19, 2026

Copy link
Copy Markdown
Member

Rationale for this change

#49982 added a basic Wasm build but after doing some testing I ran into an issue with threading.

What changes are included in this PR?

  • The main fix: Modified CanRunWithCapturedR so it's always false under Emscripten. This change is the only change that was strictly needed to fix my issue. I'm not a pro with this part of the codebase so scrutiny welcome.
  • I also set options(arrow.use_threads = FALSE) when running under Wasm just in case since it makes sense to not use threads in a Wasm context
  • Added some skips to some tests, mostly timezone-related ones, so we skip some tests under Emscripten

Are these changes tested?

Yes. Crossbow jobs show the build works and I have a demo at https://amoeba.github.io/arrow-r-wasm/.

Are there any user-facing changes?

No.

@amoeba

amoeba commented May 19, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@github-actions

Copy link
Copy Markdown

Revision: dadf0f5

Submitted crossbow builds: ursacomputing/crossbow @ actions-9b0263bfa9

Task Status
test-r-wasm GitHub Actions

@amoeba

amoeba commented May 19, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@amoeba

amoeba commented May 19, 2026

Copy link
Copy Markdown
Member Author

With dadf0f5, Parquet reading works with my local test. I'm testing the changes separately so I know what's actually fixing things here.

@github-actions

Copy link
Copy Markdown

Revision: abaacb8

Submitted crossbow builds: ursacomputing/crossbow @ actions-510511a107

Task Status
test-r-wasm GitHub Actions

@amoeba amoeba marked this pull request as ready for review May 20, 2026 03:56
@amoeba amoeba requested review from jonkeane and thisisnic as code owners May 20, 2026 03:56
@amoeba

amoeba commented May 20, 2026

Copy link
Copy Markdown
Member Author

I marked this as ready for review. The PR description hopefully explains all that's needed and I put together a demo at https://amoeba.github.io/arrow-r-wasm/

@amoeba amoeba changed the title GH-49995: [R][Wasm] Fix threading issue under Wasm/Emscritpen GH-49995: [R][Wasm] Fix threading issue under Wasm/Emscripten May 20, 2026
Comment on lines +48 to +56
bool CanRunWithCapturedR() {
#ifdef __EMSCRIPTEN__
// Threading is not supported under Emscripten/WASM. Always take the
// synchronous path to avoid attempting pthread_create which will fail
// with "thread constructor failed: Not supported".
return false;
#else
return MainRThread::GetInstance().Executor() == nullptr;
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a test (or maybe it's that we actually need to run the tests under wasm(???) to catch this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like maybe we can at least run some R code in the container if not run the test suite. I'll report back.

@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting review Awaiting review labels May 20, 2026
@amoeba amoeba requested review from assignUser, kou and raulcd as code owners May 20, 2026 19:39
@amoeba

amoeba commented May 20, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@github-actions github-actions Bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels May 20, 2026
@amoeba

amoeba commented May 20, 2026

Copy link
Copy Markdown
Member Author

I'm testing trying to install, load the package, and run the test suite in CI here. I tried testing locally but I'm running into what appears to be some qemu errors since I'm running Docker on Mac so we'll see how this runs in CI.

@github-actions

Copy link
Copy Markdown

Revision: 19efaf3

Submitted crossbow builds: ursacomputing/crossbow @ actions-6a9811d919

Task Status
test-r-wasm GitHub Actions

@amoeba

amoeba commented May 20, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@github-actions

Copy link
Copy Markdown

Revision: 47738fa

Submitted crossbow builds: ursacomputing/crossbow @ actions-8e088c6ac8

Task Status
test-r-wasm GitHub Actions

@amoeba

amoeba commented May 20, 2026

Copy link
Copy Markdown
Member Author

The job runs but we don't actually run any tests,

Running testthat suite under webR...
No installed testthat tests found for arrow.
Results: 0 passed, 0 skipped, 0 failed, 0 errors
✓ testthat suite passed
✓ All tests passed!

More soon!

@amoeba

amoeba commented May 21, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@github-actions

Copy link
Copy Markdown

Revision: 9f03a2a

Submitted crossbow builds: ursacomputing/crossbow @ actions-d71cf926ef

Task Status
test-r-wasm GitHub Actions

@amoeba

amoeba commented May 21, 2026

Copy link
Copy Markdown
Member Author

I couldn't find any examples of people running their test suites under Wasm but I'm going to see if I can't get it working in 9f03a2a. If that doesn't work easily, I think it'd be good enough to just write a simple test script to run instead.

@amoeba

amoeba commented May 21, 2026

Copy link
Copy Markdown
Member Author

That went shockingly well,

Results: 6376 passed, 92 skipped, 1 failed, 3 errors

Here are the errors/failures:

── 1. Error ('test-dataset-dplyr.R:76:3'): filter() on timestamp columns ───────
Error in `compute.arrow_dplyr_query(x)`: Invalid: Cannot locate or parse timezone 'UTC': discover_tz_dir failed to find zoneinfo

── 2. Error ('test-dataset-dplyr.R:124:3'): filter() on date32 columns ─────────
Error in `compute.arrow_dplyr_query(x)`: Invalid: Cannot locate or parse timezone 'UTC': discover_tz_dir failed to find zoneinfo

── 3. Failure ('test-dplyr-filter.R:425:3'): filter() with across() ────────────
Expected `via_table <- rlang::eval_tidy(expr, rlang::new_data_mask(rlang::env(.input = arrow_table(tbl))))` not to throw any warnings.

── 4. Error ('test-dplyr-query.R:650:3'): Scalars in expressions match the type 
Error: Error: NotImplemented: Function 'greater' has no kernel matching input types (timestamp[us, tz=UTC], string)

@amoeba amoeba force-pushed the GH-49995--r-wasm-threading branch from 984f37e to 3866da3 Compare July 2, 2026 02:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Comment on lines +55 to +61
const server = http.createServer((req, res) => {
const filePath = path.join(repoDir, decodeURIComponent(req.url));
if (!filePath.startsWith(path.resolve(repoDir))) {
res.writeHead(403);
res.end();
return;
}
Comment on lines 239 to 242
example_data
)
skip_on_emscripten()
compare_dplyr_binding(
Comment thread r/R/arrow-package.R Outdated
Comment on lines +192 to +196
tz_path <- tzdb::tzdb_path("text")
packageStartupMessage("[configure_tzdb] tzdb path: ", tz_path)
packageStartupMessage("[configure_tzdb] path exists: ", dir.exists(tz_path))
if (dir.exists(tz_path)) {
tz_files <- list.files(tz_path, recursive = TRUE)
bool CanRunWithCapturedR() { return MainRThread::GetInstance().Executor() == nullptr; }
bool CanRunWithCapturedR() {
#ifdef __EMSCRIPTEN__
// Threading is not supported under Emscripten/WASM. Always take the
Copilot AI review requested due to automatic review settings July 2, 2026 02:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.

Comment thread r/R/arrow-package.R Outdated
Comment on lines +193 to +197
tz_path <- tzdb::tzdb_path("text")
packageStartupMessage("[configure_tzdb] tzdb path: ", tz_path)
packageStartupMessage("[configure_tzdb] path exists: ", dir.exists(tz_path))
if (dir.exists(tz_path)) {
tz_files <- list.files(tz_path, recursive = TRUE)
Comment thread r/R/arrow-package.R Outdated
Comment on lines +210 to +213
packageStartupMessage(
"The tzdb package was available but failed to initialize: ",
e,
"[configure_tzdb] tzdb package available but failed to initialize: ",
conditionMessage(e)
)
Comment thread r/R/arrow-package.R
Comment on lines 220 to 224
packageStartupMessage(
"The tzdb package is not installed. ",
"[configure_tzdb] tzdb package is NOT installed. ",
"Timezones will not be available to Arrow compute functions. ",
"If you get errors when using Arrow on datetimes, try running ",
"`install.packages('tzdb')` and trying again."
Comment on lines +55 to +62
const server = http.createServer((req, res) => {
const filePath = path.join(repoDir, decodeURIComponent(req.url));
if (!filePath.startsWith(path.resolve(repoDir))) {
res.writeHead(403);
res.end();
return;
}
fs.readFile(filePath, (err, data) => {
Comment thread ci/scripts/r_wasm_test.sh
Comment on lines +48 to +52
tgz_file=$(ls "${arrow_r_dir}"/arrow_*.tgz 2>/dev/null | head -1)
if [ -z "${tgz_file}" ]; then
echo "ERROR: No arrow_*.tgz found in ${arrow_r_dir}" >&2
exit 1
fi
Comment thread r/tests/testthat/test-dplyr-arrange.R Outdated
collect(),
example_data
)
skip_on_emscripten()
Comment thread r/tests/testthat/test-dataset-dplyr.R Outdated
Comment on lines +122 to +123
skip_if_not_available("re2")
skip_on_emscripten()
@amoeba amoeba requested a review from pitrou as a code owner July 2, 2026 03:01
@amoeba

amoeba commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Revision: 4bf5669

Submitted crossbow builds: ursacomputing/crossbow @ actions-4d03e485b0

Task Status
test-r-wasm GitHub Actions

Copilot AI review requested due to automatic review settings July 2, 2026 03:36
@amoeba

amoeba commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Comment thread r/R/arrow-package.R
Comment on lines 196 to 200
packageStartupMessage(
"The tzdb package was available but failed to initialize: ",
e,
"Timezones will not be available to Arrow compute functions."
)
}
Comment thread ci/scripts/r_wasm_test.sh
arrow_r_dir="${1:-.}"

# Set up a fake CRAN-like repo so we can install the package
tgz_file=$(ls "${arrow_r_dir}"/arrow_*.tgz 2>/dev/null | head -1)
Comment on lines +55 to +61
const server = http.createServer((req, res) => {
const filePath = path.join(repoDir, decodeURIComponent(req.url));
if (!filePath.startsWith(path.resolve(repoDir))) {
res.writeHead(403);
res.end();
return;
}
@amoeba amoeba marked this pull request as draft July 2, 2026 03:42
@amoeba

amoeba commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@pitrou

pitrou commented Jul 2, 2026

Copy link
Copy Markdown
Member

Hey @DerThorsten do you want to take a look at this? You're probably one of the world experts on this :)

@DerThorsten

Copy link
Copy Markdown

Hey @DerThorsten do you want to take a look at this? You're probably one of the world experts on this :)

Sure, but not sure if I manage today

@pitrou

pitrou commented Jul 2, 2026

Copy link
Copy Markdown
Member

There's no urgency :)

@amoeba

amoeba commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit test-r-wasm

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Revision: 9cc1264

Submitted crossbow builds: ursacomputing/crossbow @ actions-83127ccf1f

Task Status
test-r-wasm GitHub Actions

@rok

rok commented Jul 3, 2026

Copy link
Copy Markdown
Member

@amoeba you might want to rebase on this to get the test-r-wasm job passing the c++ compilation (it's been failing for 11 days now on nightly).

@rok

rok commented Jul 3, 2026

Copy link
Copy Markdown
Member

Ah, I see you fixed the ncores issue in r/tools/nixlibs.R, never mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants