-
Notifications
You must be signed in to change notification settings - Fork 4.1k
GH-49995: [R][Wasm] Fix threading issue under Wasm/Emscripten #49996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amoeba
wants to merge
4
commits into
apache:main
Choose a base branch
from
amoeba:GH-49995--r-wasm-threading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| // Smoke-test and run the testthat suite for the arrow R package under webR. | ||
| // | ||
| // This script is called by r_wasm_test.sh after it sets up the CRAN-like | ||
| // repo and installs the webr npm package. | ||
| // | ||
| // Environment variables: | ||
| // ARROW_WASM_REPO_DIR - path to the local CRAN-like repo containing | ||
| // the arrow wasm binary package | ||
|
|
||
| const { WebR } = require("webr"); | ||
| const http = require("http"); | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| const repoDir = process.env.ARROW_WASM_REPO_DIR; | ||
| if (!repoDir) { | ||
| console.error("ERROR: ARROW_WASM_REPO_DIR not set"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| async function main() { | ||
| // Serve the local repo over HTTP so webR (Emscripten) can access it. | ||
| // webR's R runs in an Emscripten sandbox and cannot access the host | ||
| // filesystem directly — it fetches packages over HTTP instead. | ||
| const server = http.createServer((req, res) => { | ||
| const filePath = path.join(repoDir, decodeURIComponent(req.url)); | ||
| fs.readFile(filePath, (err, data) => { | ||
| if (err) { | ||
| res.writeHead(404); | ||
| res.end(); | ||
| } else { | ||
| res.writeHead(200); | ||
| res.end(data); | ||
| } | ||
| }); | ||
| }); | ||
| server.listen(8080); | ||
| console.log("✓ Repo server on :8080"); | ||
|
|
||
| const webR = new WebR({ | ||
| RArgs: ["--quiet"], | ||
| interactive: false, | ||
| }); | ||
|
|
||
| await webR.init(); | ||
| console.log("✓ webR initialized"); | ||
|
|
||
| // Install the arrow Wasm package, put localhost:8080 before repo.r-wasm.org | ||
| // (which is used for deps) | ||
| await webR.installPackages(["arrow"], { | ||
| repos: ["http://localhost:8080", "https://repo.r-wasm.org"], | ||
| quiet: false, | ||
| mount: false, | ||
| }); | ||
| console.log("✓ arrow installed"); | ||
|
|
||
| // Install test deps. TOOD: This is flakey. We could parse the DESCRIPTION | ||
| // file to be more robust. | ||
| await webR.installPackages( | ||
| ["testthat", "tibble", "dplyr", "withr", "pillar"], | ||
| { | ||
| repos: ["https://repo.r-wasm.org"], | ||
| quiet: false, | ||
| mount: false, | ||
| }, | ||
| ); | ||
| console.log("✓ test dependencies installed"); | ||
|
|
||
| // Test the package loads and functions basically | ||
| const loadResult = await webR.evalRString(` | ||
| library(arrow) | ||
| cat("arrow loaded\\n") | ||
| cat("R.version$os =", R.version$os, "\\n") | ||
| use_threads <- getOption("arrow.use_threads") | ||
| cat("arrow.use_threads =", use_threads, "\\n") | ||
| stopifnot(identical(use_threads, FALSE)) | ||
| tab <- arrow::as_arrow_table(data.frame(x = 1:10, y = letters[1:10])) | ||
| stopifnot(nrow(tab) == 10L) | ||
| cat("Created Arrow table with", nrow(tab), "rows\\n") | ||
| "PASS" | ||
| `); | ||
|
|
||
| if (loadResult !== "PASS") { | ||
| console.error("Package load test FAILED"); | ||
| await webR.close(); | ||
| server.close(); | ||
| process.exit(1); | ||
| } | ||
| console.log("✓ Package loads and works correctly"); | ||
|
|
||
| // Run tests | ||
| console.log("Running testthat suite under webR..."); | ||
|
|
||
| const testResult = await webR.evalRString(` | ||
| library(testthat) | ||
| library(arrow) | ||
| results <- testthat::test_package("arrow", reporter = "summary", stop_on_failure = FALSE) | ||
| df <- as.data.frame(results) | ||
| n_pass <- sum(df$passed) | ||
| n_skip <- sum(df$skipped) | ||
| n_fail <- sum(df$failed) | ||
| n_error <- sum(df$error) | ||
| cat(sprintf("Results: %d passed, %d skipped, %d failed, %d errors\\n", | ||
| n_pass, n_skip, n_fail, n_error)) | ||
| if (n_fail > 0 || n_error > 0) "FAIL" else "PASS" | ||
| `); | ||
|
|
||
| if (testResult !== "PASS") { | ||
| console.error("testthat suite FAILED"); | ||
| await webR.close(); | ||
| server.close(); | ||
| process.exit(1); | ||
| } | ||
| console.log("✓ testthat suite passed"); | ||
|
|
||
| console.log("✓ All tests passed!"); | ||
| await webR.close(); | ||
| server.close(); | ||
| } | ||
|
|
||
| main().catch((e) => { | ||
| console.error("FAILED:", e); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env bash | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # Test the arrow R package built for WebAssembly. | ||
| # | ||
| # This script is intended to run inside the ghcr.io/r-universe-org/build-wasm | ||
| # Docker container after rwasm::build() has produced a .tgz binary. It: | ||
| # 1. Sets up a CRAN-like repo structure from the built .tgz | ||
| # 2. Installs the npm webr package (Node.js webR runtime) | ||
| # 3. Boots webR, installs arrow from the local repo, and verifies: | ||
| # - The package can be installed and loaded | ||
| # - Multithreading is disabled (arrow.use_threads == FALSE) | ||
| # - The testthat test suite runs | ||
| # | ||
| # Tests that require threading are automatically skipped via | ||
| # skip_if_not(CanRunWithCapturedR()) since CanRunWithCapturedR() returns | ||
| # FALSE under Emscripten. | ||
| # | ||
| # Usage: | ||
| # r_wasm_test.sh <path-to-arrow-r-dir> | ||
| # | ||
| # Example: | ||
| # r_wasm_test.sh /work | ||
| # | ||
| # The arrow .tgz file(s) should already exist in <path-to-arrow-r-dir>. | ||
|
|
||
| set -euxo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| 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) | ||
| if [ -z "${tgz_file}" ]; then | ||
| echo "ERROR: No arrow_*.tgz found in ${arrow_r_dir}" >&2 | ||
| exit 1 | ||
| fi | ||
| echo "Found Wasm binary: ${tgz_file}" | ||
|
|
||
| repo_dir=$(mktemp -d) | ||
| # TODO: Not sure if we need this | ||
| # Cover multiple R minor versions in case the npm webr package | ||
| # uses a different R version than the Docker image's build R. | ||
| for r_ver in 4.4 4.5 4.6; do | ||
| contrib_dir="${repo_dir}/bin/emscripten/contrib/${r_ver}" | ||
| mkdir -p "${contrib_dir}" | ||
| cp "${tgz_file}" "${contrib_dir}/" | ||
| # type=mac.binary matches .tgz file extension | ||
| R -q -e "tools::write_PACKAGES('${contrib_dir}', type = 'mac.binary')" | ||
| done | ||
|
|
||
| echo "Repo structure:" | ||
| find "${repo_dir}" -type f | ||
|
|
||
| # Install webr in a temporary node project | ||
| work_dir=$(mktemp -d) | ||
| cd "${work_dir}" | ||
| npm init -y > /dev/null 2>&1 | ||
| npm install --silent webr 2>/dev/null | ||
|
|
||
| # Run our test script | ||
| ARROW_WASM_REPO_DIR="${repo_dir}" node "${SCRIPT_DIR}/r_wasm_test.cjs" | ||
|
|
||
| # Cleanup temp dirs | ||
| rm -rf "${work_dir}" "${repo_dir}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.