Skip to content

Commit 38e4291

Browse files
committed
Correctly handles Rcpp::Function exceptions so the C++ terminates correctly
1 parent 78b96f1 commit 38e4291

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed

CRAN-SUBMISSION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Version: 0.1.6
2-
Date: 2023-06-23 04:31:47 UTC
3-
SHA: a08a8166daac9b742356bf2f12ac4287e57ee8a5
1+
Version: 0.1.7
2+
Date: 2024-07-16 20:14:07 UTC
3+
SHA: 78b96f1f875b9e8f26e3a0320dac40d75b42f7b3

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: filearray
22
Type: Package
33
Title: File-Backed Array for Out-of-Memory Computation
4-
Version: 0.1.7
4+
Version: 0.1.7.9000
55
Language: en-US
66
Encoding: UTF-8
77
License: LGPL-3

src/load.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,15 @@ SEXP FARR_subset_sequential(
242242
}
243243
}
244244

245-
} catch (...) {}
245+
} catch (const Rcpp::LongjumpException& e) {
246+
std::rethrow_exception(std::current_exception());
247+
} catch (const boost::interprocess::interprocess_exception& e) {
248+
// unable to find the file, skip
249+
} catch (const std::exception& e) {
250+
std::rethrow_exception(std::current_exception());
251+
} catch (...) {
252+
throw std::runtime_error("filearray C++: Caught an unknown exception in `FARR_subset_sequential`.");
253+
}
246254

247255
}
248256

src/mapreduce.cpp

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,27 @@ SEXP each_partition_template(
4747
if( read_len > 0 ){
4848
*readlen_ptr = (double) read_len;
4949
*count_ptr = (double) *count;
50-
if( read_len < buffer_nelems ){
51-
// if( tmp_arg == R_NilValue ){
52-
// tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
53-
// } else if( read_len - Rf_xlength(tmp_arg) != 0 ){
54-
// UNPROTECT(1);
55-
// tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
56-
// }
57-
SEXP tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
58-
SEXP item = PROTECT( fun(tmp_arg, readlen_sxp, count_sxp) );
59-
ret.push_back( item );
60-
UNPROTECT( 2 ); // item, tmp_arg
61-
} else {
62-
SEXP item = PROTECT( fun(argbuf, readlen_sxp, count_sxp) );
63-
ret.push_back( item );
64-
UNPROTECT( 1 ); // item
65-
// ret.push_back( Shield<SEXP>( fun(Shield<SEXP>(argbuf), Shield<SEXP>(wrap(read_len)), Shield<SEXP>(wrap(*count))) ) );
50+
51+
// https://github.com/RcppCore/Rcpp/issues/1268
52+
try {
53+
if( read_len < buffer_nelems ) {
54+
SEXP tmp_arg = PROTECT(sub_vec_range(argbuf, 0, read_len));
55+
SEXP item = fun(tmp_arg, readlen_sxp, count_sxp);
56+
PROTECT( item );
57+
ret.push_back( item );
58+
UNPROTECT( 2 ); // item, tmp_arg
59+
} else {
60+
SEXP item = fun(argbuf, readlen_sxp, count_sxp);
61+
PROTECT( item );
62+
ret.push_back( item );
63+
UNPROTECT( 1 ); // item
64+
}
65+
} catch (const Rcpp::LongjumpException& e) {
66+
std::rethrow_exception(std::current_exception());
67+
} catch (const std::exception& e) {
68+
std::rethrow_exception(std::current_exception());
69+
} catch (...) {
70+
throw std::runtime_error("filearray C++: Caught an unknown exception in `each_partition_template`.");
6671
}
6772
}
6873

@@ -198,19 +203,39 @@ SEXP FARR_buffer_mapreduce(
198203
break;
199204
}
200205
}
201-
} catch (...) {}
206+
} catch (const Rcpp::LongjumpException& e) {
207+
std::rethrow_exception(std::current_exception());
208+
} catch (const boost::interprocess::interprocess_exception& e) {
209+
// unable to find the file, skip
210+
} catch (const std::exception& e) {
211+
std::rethrow_exception(std::current_exception());
212+
} catch (...) {
213+
throw std::runtime_error("filearray C++: Caught an unknown exception in `FARR_buffer_mapreduce`.");
214+
}
215+
// } catch (...) {}
202216

203217
}
204218

205219
if(reduce == R_NilValue){
206-
UNPROTECT( 1 );
220+
UNPROTECT( 1 ); // argbuffer
207221
return ret;
222+
} else {
223+
try{
224+
225+
Function reduce2 = (Function) reduce;
226+
SEXP re = PROTECT(reduce2(ret));
227+
UNPROTECT( 2 ); // argbuffer, re
228+
return(re);
229+
230+
} catch (const Rcpp::LongjumpException& e) {
231+
std::rethrow_exception(std::current_exception());
232+
} catch (const std::exception& e) {
233+
std::rethrow_exception(std::current_exception());
234+
} catch (...) {
235+
throw std::runtime_error("filearray C++: Caught an unknown exception in `FARR_buffer_mapreduce`.");
236+
}
208237
}
209238

210-
Function reduce2 = (Function) reduce;
211-
SEXP re = PROTECT(reduce2(ret));
212-
UNPROTECT( 2 );
213-
return(re);
214239
}
215240

216241

0 commit comments

Comments
 (0)