-
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
10
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dadf0f5
Try to fix issue
amoeba abaacb8
test not setting usethreads
amoeba bafaf05
set back
amoeba 19efaf3
wip, run tests under wasm
amoeba 47738fa
set NODE_PATH
amoeba 9f03a2a
try this for tests
amoeba dd555bc
tidy up a bit
amoeba 80a2fb1
configure tzdb
amoeba dee814c
add skip
amoeba b8810d9
add debug info for ci
amoeba 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,160 @@ | ||
| // 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 the arrow R package under webR, then run the testthat suite. | ||
| // Called by r_wasm_test.sh. Requires env vars: | ||
| // ARROW_WASM_REPO_DIR - local CRAN-like repo with the arrow .tgz | ||
| // ARROW_R_TESTS_DIR - path to tests/testthat in the source tree | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| const testsDir = process.env.ARROW_R_TESTS_DIR; | ||
| if (!testsDir) { | ||
| console.error("ERROR: ARROW_R_TESTS_DIR not set"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| function listFilesRecursive(dir) { | ||
| const results = []; | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| const full = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| results.push(...listFilesRecursive(full)); | ||
| } else { | ||
| results.push(full); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| async function main() { | ||
| // Serve the repo over HTTP (webR can't access the host filesystem directly) | ||
| 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"); | ||
|
|
||
| // Upload test files to webR VFS (rwasm doesn't include tests in binaries) | ||
| const vfsTestDir = "/tmp/arrow-tests"; | ||
| await webR.FS.mkdir(vfsTestDir); | ||
| const testFiles = listFilesRecursive(testsDir); | ||
| const createdDirs = new Set([vfsTestDir]); | ||
| for (const file of testFiles) { | ||
| const rel = path.relative(testsDir, file); | ||
| const vfsPath = path.posix.join(vfsTestDir, rel.split(path.sep).join("/")); | ||
| const vfsDir = path.posix.dirname(vfsPath); | ||
| if (!createdDirs.has(vfsDir)) { | ||
| await webR.evalRVoid(`dir.create("${vfsDir}", recursive=TRUE, showWarnings=FALSE)`); | ||
| createdDirs.add(vfsDir); | ||
| } | ||
| await webR.FS.writeFile(vfsPath, fs.readFileSync(file)); | ||
| } | ||
| console.log(`✓ Uploaded ${testFiles.length} test files to VFS`); | ||
|
|
||
| // Install arrow from local repo, deps from r-wasm.org | ||
| await webR.installPackages(["arrow"], { | ||
| repos: ["http://localhost:8080", "https://repo.r-wasm.org"], | ||
| quiet: false, | ||
| mount: false, | ||
| }); | ||
| console.log("✓ arrow installed"); | ||
|
|
||
| // Install test deps parsed from DESCRIPTION | ||
| const depsList = await webR.evalRString(` | ||
| desc <- read.dcf(system.file("DESCRIPTION", package = "arrow"), | ||
| fields = c("Imports", "Suggests")) | ||
| pkgs <- unlist(strsplit(paste(na.omit(desc[1,]), collapse = ","), ",\\\\s*")) | ||
| pkgs <- trimws(sub("\\\\s*\\\\(.*\\\\)", "", pkgs)) | ||
| pkgs <- pkgs[pkgs != "" & pkgs != "R"] | ||
| pkgs <- pkgs[!pkgs %in% loadedNamespaces()] | ||
| paste(pkgs, collapse = "\\n") | ||
| `); | ||
| const testDeps = depsList.split("\n").filter(Boolean); | ||
| console.log(`Installing ${testDeps.length} dependencies from DESCRIPTION...`); | ||
| await webR.installPackages(testDeps, { | ||
| repos: ["https://repo.r-wasm.org"], | ||
| quiet: false, | ||
| mount: false, | ||
| }); | ||
| console.log("✓ test dependencies installed"); | ||
|
|
||
| // Smoke test: package loads, threading disabled, basic operations work | ||
| const loadResult = await webR.evalRString(` | ||
| library(arrow) | ||
| cat("R.version$os =", R.version$os, "\\n") | ||
| stopifnot(identical(getOption("arrow.use_threads"), FALSE)) | ||
| tab <- arrow::as_arrow_table(data.frame(x = 1:10, y = letters[1:10])) | ||
| stopifnot(nrow(tab) == 10L) | ||
| cat("Created table with", nrow(tab), "rows\\n") | ||
| "PASS" | ||
| `); | ||
| if (loadResult !== "PASS") { | ||
| throw new Error("Smoke test failed"); | ||
| } | ||
| console.log("✓ Smoke test passed"); | ||
|
|
||
| // Run testthat suite | ||
| console.log("Running testthat suite..."); | ||
| const testResult = await webR.evalRString(` | ||
| library(testthat) | ||
| results <- testthat::test_dir( | ||
| "${vfsTestDir}", | ||
| reporter = "summary", | ||
| stop_on_failure = FALSE, | ||
| package = "arrow" | ||
| ) | ||
| df <- as.data.frame(results) | ||
| cat(sprintf("Results: %d passed, %d skipped, %d failed, %d errors\\n", | ||
| sum(df$passed), sum(df$skipped), sum(df$failed), sum(df$error))) | ||
| if (sum(df$failed) > 0 || sum(df$error) > 0) "FAIL" else "PASS" | ||
| `); | ||
| if (testResult !== "PASS") { | ||
| throw new Error("testthat suite failed"); | ||
| } | ||
| console.log("✓ testthat suite 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}" ARROW_R_TESTS_DIR="${arrow_r_dir}/tests/testthat" NODE_PATH="${work_dir}/node_modules" 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
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
Oops, something went wrong.
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.