File tree 4 files changed +40
-22
lines changed 4 files changed +40
-22
lines changed Original file line number Diff line number Diff line change @@ -42,24 +42,25 @@ impl DummyArkFrontend {
42
42
let frontend = DummyFrontend :: new ( ) ;
43
43
let connection_file = frontend. get_connection_file ( ) ;
44
44
45
+ // Start the kernel in this thread so that panics are propagated
46
+ crate :: start:: start_kernel (
47
+ connection_file,
48
+ vec ! [
49
+ String :: from( "--interactive" ) ,
50
+ String :: from( "--vanilla" ) ,
51
+ String :: from( "--no-save" ) ,
52
+ String :: from( "--no-restore" ) ,
53
+ ] ,
54
+ None ,
55
+ SessionMode :: Console ,
56
+ false ,
57
+ ) ;
58
+
59
+ // Start the REPL in a background thread, does not return and is never joined
45
60
stdext:: spawn!( "dummy_kernel" , || {
46
- crate :: start:: start_kernel(
47
- connection_file,
48
- vec![
49
- String :: from( "--interactive" ) ,
50
- String :: from( "--vanilla" ) ,
51
- String :: from( "--no-save" ) ,
52
- String :: from( "--no-restore" ) ,
53
- ] ,
54
- None ,
55
- SessionMode :: Console ,
56
- false ,
57
- ) ;
61
+ RMain :: start( ) ;
58
62
} ) ;
59
63
60
- // Wait for startup to complete
61
- RMain :: wait_r_initialized ( ) ;
62
-
63
64
frontend. complete_initialization ( ) ;
64
65
frontend
65
66
}
Original file line number Diff line number Diff line change @@ -282,9 +282,14 @@ pub enum ConsoleResult {
282
282
}
283
283
284
284
impl RMain {
285
- /// Starts the main R thread and initializes the `R_MAIN` singleton.
286
- /// Doesn't return. Must be called only once.
287
- pub fn start (
285
+ /// Sets up the main R thread and initializes the `R_MAIN` singleton. Must
286
+ /// be called only once. This is doing as much setup as possible before
287
+ /// starting the R REPL. Since the REPL does not return, it might be
288
+ /// launched in a background thread (which we do in integration tests). The
289
+ /// setup can still be done in your main thread so that panics may propagate
290
+ /// as expected. Call `RMain::start_repl()` after this to actually start the
291
+ /// R REPL.
292
+ pub fn setup (
288
293
r_args : Vec < String > ,
289
294
startup_file : Option < String > ,
290
295
kernel_mutex : Arc < Mutex < Kernel > > ,
@@ -423,8 +428,13 @@ impl RMain {
423
428
if !ignore_user_r_profile {
424
429
startup:: source_user_r_profile ( ) ;
425
430
}
431
+ }
426
432
427
- // Does not return!
433
+ /// Start the REPL. Does not return!
434
+ pub fn start ( ) {
435
+ // Update the main thread ID in case the REPL is started in a background
436
+ // thread (e.g. in integration tests)
437
+ unsafe { R_MAIN_THREAD_ID = Some ( std:: thread:: current ( ) . id ( ) ) } ;
428
438
crate :: sys:: interface:: run_r ( ) ;
429
439
}
430
440
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ use std::env;
12
12
13
13
use amalthea:: connection_file:: ConnectionFile ;
14
14
use amalthea:: kernel_spec:: KernelSpec ;
15
+ use ark:: interface:: RMain ;
15
16
use ark:: interface:: SessionMode ;
16
17
use ark:: logger;
17
18
use ark:: signals:: initialize_signal_block;
@@ -315,13 +316,18 @@ fn parse_file(
315
316
Ok ( connection) => {
316
317
log:: info!( "Loaded connection information from frontend in {connection_file}" ) ;
317
318
log:: info!( "Connection data: {:?}" , connection) ;
319
+
320
+ // Set up R and start the Jupyter kernel
318
321
start_kernel (
319
322
connection,
320
323
r_args,
321
324
startup_file,
322
325
session_mode,
323
326
capture_streams,
324
327
) ;
328
+
329
+ // Start the REPL, does not return
330
+ RMain :: start ( ) ;
325
331
} ,
326
332
Err ( error) => {
327
333
log:: error!( "Couldn't read connection file {connection_file}: {error:?}" ) ;
Original file line number Diff line number Diff line change @@ -22,7 +22,8 @@ use crate::request::KernelRequest;
22
22
use crate :: request:: RRequest ;
23
23
use crate :: shell:: Shell ;
24
24
25
- /// Exported for unit tests. Does not return.
25
+ /// Exported for unit tests.
26
+ /// Call `RMain::start_repl()` after this.
26
27
pub fn start_kernel (
27
28
connection_file : ConnectionFile ,
28
29
r_args : Vec < String > ,
@@ -112,8 +113,8 @@ pub fn start_kernel(
112
113
panic ! ( "Couldn't connect to frontend: {err:?}" ) ;
113
114
}
114
115
115
- // Start the R REPL (does not return for the duration of the session)
116
- crate :: interface:: RMain :: start (
116
+ // Setup the channels and R
117
+ crate :: interface:: RMain :: setup (
117
118
r_args,
118
119
startup_file,
119
120
kernel_clone,
You can’t perform that action at this time.
0 commit comments