Skip to content

Commit

Permalink
Correctly handles Rcpp::Function exceptions so the C++ terminates cor…
Browse files Browse the repository at this point in the history
…rectly
  • Loading branch information
dipterix committed Aug 19, 2024
1 parent 78b96f1 commit 38e4291
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
6 changes: 3 additions & 3 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.1.6
Date: 2023-06-23 04:31:47 UTC
SHA: a08a8166daac9b742356bf2f12ac4287e57ee8a5
Version: 0.1.7
Date: 2024-07-16 20:14:07 UTC
SHA: 78b96f1f875b9e8f26e3a0320dac40d75b42f7b3
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: filearray
Type: Package
Title: File-Backed Array for Out-of-Memory Computation
Version: 0.1.7
Version: 0.1.7.9000
Language: en-US
Encoding: UTF-8
License: LGPL-3
Expand Down
10 changes: 9 additions & 1 deletion src/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,15 @@ SEXP FARR_subset_sequential(
}
}

} catch (...) {}
} catch (const Rcpp::LongjumpException& e) {
std::rethrow_exception(std::current_exception());
} catch (const boost::interprocess::interprocess_exception& e) {
// unable to find the file, skip
} catch (const std::exception& e) {
std::rethrow_exception(std::current_exception());
} catch (...) {
throw std::runtime_error("filearray C++: Caught an unknown exception in `FARR_subset_sequential`.");
}

}

Expand Down
69 changes: 47 additions & 22 deletions src/mapreduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,27 @@ SEXP each_partition_template(
if( read_len > 0 ){
*readlen_ptr = (double) read_len;
*count_ptr = (double) *count;
if( read_len < buffer_nelems ){
// if( tmp_arg == R_NilValue ){
// tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
// } else if( read_len - Rf_xlength(tmp_arg) != 0 ){
// UNPROTECT(1);
// tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
// }
SEXP tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
SEXP item = PROTECT( fun(tmp_arg, readlen_sxp, count_sxp) );
ret.push_back( item );
UNPROTECT( 2 ); // item, tmp_arg
} else {
SEXP item = PROTECT( fun(argbuf, readlen_sxp, count_sxp) );
ret.push_back( item );
UNPROTECT( 1 ); // item
// ret.push_back( Shield<SEXP>( fun(Shield<SEXP>(argbuf), Shield<SEXP>(wrap(read_len)), Shield<SEXP>(wrap(*count))) ) );

// https://github.com/RcppCore/Rcpp/issues/1268
try {
if( read_len < buffer_nelems ) {
SEXP tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
SEXP item = fun(tmp_arg, readlen_sxp, count_sxp);
PROTECT( item );
ret.push_back( item );
UNPROTECT( 2 ); // item, tmp_arg
} else {
SEXP item = fun(argbuf, readlen_sxp, count_sxp);
PROTECT( item );
ret.push_back( item );
UNPROTECT( 1 ); // item
}
} catch (const Rcpp::LongjumpException& e) {
std::rethrow_exception(std::current_exception());
} catch (const std::exception& e) {
std::rethrow_exception(std::current_exception());
} catch (...) {
throw std::runtime_error("filearray C++: Caught an unknown exception in `each_partition_template`.");
}
}

Expand Down Expand Up @@ -198,19 +203,39 @@ SEXP FARR_buffer_mapreduce(
break;
}
}
} catch (...) {}
} catch (const Rcpp::LongjumpException& e) {
std::rethrow_exception(std::current_exception());
} catch (const boost::interprocess::interprocess_exception& e) {
// unable to find the file, skip
} catch (const std::exception& e) {
std::rethrow_exception(std::current_exception());
} catch (...) {
throw std::runtime_error("filearray C++: Caught an unknown exception in `FARR_buffer_mapreduce`.");
}
// } catch (...) {}

}

if(reduce == R_NilValue){
UNPROTECT( 1 );
UNPROTECT( 1 ); // argbuffer
return ret;
} else {
try{

Function reduce2 = (Function) reduce;
SEXP re = PROTECT(reduce2(ret));
UNPROTECT( 2 ); // argbuffer, re
return(re);

} catch (const Rcpp::LongjumpException& e) {
std::rethrow_exception(std::current_exception());
} catch (const std::exception& e) {
std::rethrow_exception(std::current_exception());
} catch (...) {
throw std::runtime_error("filearray C++: Caught an unknown exception in `FARR_buffer_mapreduce`.");
}
}

Function reduce2 = (Function) reduce;
SEXP re = PROTECT(reduce2(ret));
UNPROTECT( 2 );
return(re);
}


Expand Down

0 comments on commit 38e4291

Please sign in to comment.