Skip to content

Problem with interrupting 'parallelFor()' execution on Windows machines #79

@AndreySamokhin

Description

@AndreySamokhin

Hello, and thank you for creating this package for R. I encountered a potential issue with the parallelFor() function on Windows machines when a C++ function is interrupted by the user from R. Below is a minimal example to reproduce the issue. I tried using the sourceCpp() function and building an R package with the same C++ code. The results were identical. The number of selected threads did not influence the result either.

I ran this code on three machines:

  • Intel Core i7-2720QM, Linux Mint 22, R version 4.3.3
  • Intel Core i7-4790K, Windows 10 x64 (build 19045), R version 4.1.3
  • AMD Ryzen 5 5625U, Windows 10 x64 (build 19045), R version 4.4.1
library(Rcpp)

sourceCpp(code = '
#include <Rcpp.h>
#include <RcppThread.h>
#include <thread>
#include <chrono>

using namespace Rcpp;

// [[Rcpp::depends(RcppThread)]]

// [[Rcpp::export]]
bool TestUserInterrupt(int n_threads = 1L) {
  RcppThread::Rcout << "Main thread ID: " << std::this_thread::get_id()
                    << std::endl;
  RcppThread::parallelFor(0, 5 * 10 * n_threads, [](int i) {
    RcppThread::Rcout << std::this_thread::get_id() << "|";
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }, n_threads);
  RcppThread::Rcout << std::endl << "Processing complete." << std::endl;
  return true;
}
')

TestUserInterrupt(1)

On the Linux machine, everything worked as expected (the "Error: C++ call interrupted by the user" message was printed to the console, and the function could be rerun).

On the Windows machines, however, when the function was interrupted, the output to the console stopped without displaying the "Error: C++ call interrupted by the user" message. A subsequent call to the function resulted in no output: R was busy, and the interruption was not possible without restarting the R session. I am not sure if it is relevant, but when using multiple threads (e.g., 4), the output was sometimes shuffled (e.g., "... 4|||53|4||2|352|||4 ...").

One more remark. Initially I encountered a problem when I tried to integrate RcppThread into another R package. In that case, R crashed when the execution was interrupted by the user from R.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions