Skip to content

GH-50239: [R] Data race issue from R API requests in parallel region#50488

Open
thisisnic wants to merge 1 commit into
apache:mainfrom
thisisnic:GH-50239-raceconditions
Open

GH-50239: [R] Data race issue from R API requests in parallel region#50488
thisisnic wants to merge 1 commit into
apache:mainfrom
thisisnic:GH-50239-raceconditions

Conversation

@thisisnic

@thisisnic thisisnic commented Jul 13, 2026

Copy link
Copy Markdown
Member

Rationale for this change

When use_threads = TRUE, parallel tasks are submitted to the thread pool immediately during RTasks::Append(). This means worker threads start reading R vector headers (via INTEGER(), REAL(), etc.) while the main thread is still assigning vectors into the output list via SET_VECTOR_ELT, which writes to the same header memory. ThreadSanitizer detects this as a data race.

What changes are included in this PR?

Delay submission of parallel tasks until RTasks::Finish(), matching the existing pattern for serial tasks. All R-side allocations and list assignments complete on the main thread before any worker threads start.

Are these changes tested?

Existing tests cover the affected code path.
I also ran some local benchmarks to see if this change affected performance, but seems fine.

Are there any user-facing changes?

No.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #50239 has been automatically assigned in GitHub to PR creator.

@thisisnic thisisnic marked this pull request as ready for review July 13, 2026 08:40
@thisisnic thisisnic requested a review from jonkeane as a code owner July 13, 2026 08:40
Copilot AI review requested due to automatic review settings July 13, 2026 08:41
@thisisnic

Copy link
Copy Markdown
Member Author

@ursabot please benchmark

@rok

rok commented Jul 13, 2026

Copy link
Copy Markdown
Member

Benchmark runs are scheduled for commit 824f648. Watch https://buildkite.com/apache-arrow and https://conbench.arrow-dev.org for updates. A comment will be posted here when the runs are complete.

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

This PR addresses a ThreadSanitizer-detected data race in the R bindings when use_threads = TRUE by preventing worker threads from touching R vectors until after all R-side allocations and list assignments are complete on the main thread.

Changes:

  • Delay scheduling of parallel RTasks work until RTasks::Finish() instead of submitting immediately in RTasks::Append().
  • Add storage for delayed parallel tasks and ensure it is cleared on Reset().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
r/src/RTasks.cpp Queues parallel tasks in Append(), then submits them in Finish() after serial work completes; clears the new queue in Reset().
r/src/r_task_group.h Adds delayed_parallel_tasks_ to hold parallel tasks until Finish().

@conbench-apache-arrow

Copy link
Copy Markdown

Thanks for your patience. Conbench analyzed the 4 benchmarking runs that have been run so far on PR commit 824f648.

There were 7 benchmark results indicating a performance regression:

The full Conbench report has more details.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants