mini_racer_rs 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/COMPATIBILITY.md +27 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +55 -9
- data/crates/mini_racer_engine/src/context.rs +89 -20
- data/crates/mini_racer_engine/src/control.rs +118 -4
- data/crates/mini_racer_engine/src/counters.rs +208 -0
- data/crates/mini_racer_engine/src/error.rs +40 -0
- data/crates/mini_racer_engine/src/lib.rs +5 -2
- data/crates/mini_racer_engine/src/process/platform.rs +360 -2
- data/crates/mini_racer_engine/src/process/watchdog.rs +63 -14
- data/crates/mini_racer_engine/src/runner.rs +362 -52
- data/crates/mini_racer_engine/src/sys.rs +73 -2
- data/crates/mini_racer_sourcemap/src/map.rs +3 -2
- data/crates/mini_racer_sync/src/termination.rs +182 -44
- data/ext/mini_racer_rs/src/context.rs +45 -23
- data/ext/mini_racer_rs/src/counters.rs +40 -0
- data/ext/mini_racer_rs/src/errors.rs +21 -6
- data/ext/mini_racer_rs/src/gc_root.rs +31 -1
- data/ext/mini_racer_rs/src/heap_limit.rs +42 -0
- data/ext/mini_racer_rs/src/lib.rs +8 -0
- data/ext/mini_racer_rs/src/options.rs +89 -7
- data/ext/mini_racer_rs/src/pool.rs +811 -0
- data/ext/mini_racer_rs/src/rb_raw.rs +186 -1
- data/ext/mini_racer_rs/src/timeout.rs +132 -0
- data/lib/mini_racer_rs/heap_limit_error.rb +7 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- data/lib/mini_racer_rs.rb +1 -0
- metadata +8 -2
|
@@ -32,8 +32,8 @@ pub extern "C" fn __wrap_madvise(
|
|
|
32
32
|
/// runner to drop.
|
|
33
33
|
pub fn share(isolate: v8::OwnedIsolate) -> Result<v8::SharedIsolate, v8::OwnedIsolate> {
|
|
34
34
|
// SAFETY: the runner calls this on the thread that created `isolate`, with
|
|
35
|
-
// no reference from `Global::open` live, and the only
|
|
36
|
-
// `Arc<Runtime
|
|
35
|
+
// no reference from `Global::open` live, and the only slots it sets, an
|
|
36
|
+
// `Arc<Runtime>` and its `Collections`, are Send.
|
|
37
37
|
let shared = unsafe { isolate.try_into_shared() };
|
|
38
38
|
shared.map_err(v8::IntoSharedError::into_isolate)
|
|
39
39
|
}
|
|
@@ -208,6 +208,77 @@ unsafe extern "C" fn after_gc(
|
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
/// Has V8 ask `block` before it aborts the process at the isolate's heap
|
|
212
|
+
/// limit. `block` must stay where it is for as long as the isolate can run:
|
|
213
|
+
/// a runner holds its context's block until it has dropped the isolate, and a
|
|
214
|
+
/// forked child that takes the isolate over registers its own block instead.
|
|
215
|
+
pub fn watch_heap_limit(isolate: &mut v8::Isolate, block: &crate::control::ControlBlock) {
|
|
216
|
+
let data = std::ptr::from_ref(block).cast_mut().cast::<c_void>();
|
|
217
|
+
isolate.add_near_heap_limit_callback(near_heap_limit, data);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// Undoes `watch_heap_limit`, which must have been called, and with a
|
|
221
|
+
/// `limit` other than 0 puts the old generation's limit back to it, though
|
|
222
|
+
/// V8 keeps it no lower than the heap's live size and a quarter.
|
|
223
|
+
pub fn unwatch_heap_limit(isolate: &mut v8::Isolate, limit: usize) {
|
|
224
|
+
isolate.remove_near_heap_limit_callback(near_heap_limit, limit);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
unsafe extern "C" fn near_heap_limit(data: *mut c_void, current: usize, initial: usize) -> usize {
|
|
228
|
+
let raised = std::panic::catch_unwind(|| {
|
|
229
|
+
// SAFETY: `data` is the control block `watch_heap_limit` registered,
|
|
230
|
+
// which outlives the registration as its doc comment requires.
|
|
231
|
+
let block = unsafe { &*data.cast::<crate::control::ControlBlock>() };
|
|
232
|
+
crate::runner::near_heap_limit(block, current, initial)
|
|
233
|
+
});
|
|
234
|
+
raised.unwrap_or_else(|_| std::process::abort())
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// Has V8 report the start and end of each collection on `isolate` for its
|
|
238
|
+
/// runner's counters: scavenges, minor mark-sweeps and mark-compacts, not the
|
|
239
|
+
/// start of incremental marking or the processing of weak callbacks, which
|
|
240
|
+
/// collect nothing.
|
|
241
|
+
pub fn count_collections(isolate: &mut v8::Isolate) {
|
|
242
|
+
let collections = v8::GCType::kGCTypeScavenge
|
|
243
|
+
| v8::GCType::kGCTypeMinorMarkSweep
|
|
244
|
+
| v8::GCType::kGCTypeMarkSweepCompact;
|
|
245
|
+
isolate.add_gc_prologue_callback(collection_began, std::ptr::null_mut(), collections);
|
|
246
|
+
isolate.add_gc_epilogue_callback(collection_ended, std::ptr::null_mut(), collections);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
unsafe extern "C" fn collection_began(
|
|
250
|
+
isolate: v8::UnsafeRawIsolatePtr,
|
|
251
|
+
_type: v8::GCType,
|
|
252
|
+
_flags: v8::GCCallbackFlags,
|
|
253
|
+
_data: *mut c_void,
|
|
254
|
+
) {
|
|
255
|
+
let counted = std::panic::catch_unwind(|| {
|
|
256
|
+
// SAFETY: V8 calls a GC prologue callback on the thread that has the
|
|
257
|
+
// isolate entered, with its own non-null pointer to it.
|
|
258
|
+
let isolate = unsafe { v8::Isolate::ref_from_raw_isolate_ptr(&isolate) };
|
|
259
|
+
crate::runner::collection_began(isolate);
|
|
260
|
+
});
|
|
261
|
+
if counted.is_err() {
|
|
262
|
+
std::process::abort();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
unsafe extern "C" fn collection_ended(
|
|
267
|
+
isolate: v8::UnsafeRawIsolatePtr,
|
|
268
|
+
_type: v8::GCType,
|
|
269
|
+
_flags: v8::GCCallbackFlags,
|
|
270
|
+
_data: *mut c_void,
|
|
271
|
+
) {
|
|
272
|
+
let counted = std::panic::catch_unwind(|| {
|
|
273
|
+
// SAFETY: as in `collection_began`, for the epilogue.
|
|
274
|
+
let isolate = unsafe { v8::Isolate::ref_from_raw_isolate_ptr(&isolate) };
|
|
275
|
+
crate::runner::collection_ended(isolate);
|
|
276
|
+
});
|
|
277
|
+
if counted.is_err() {
|
|
278
|
+
std::process::abort();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
211
282
|
/// Asks the isolate's runner to park for a pending fork the next time V8
|
|
212
283
|
/// checks for interrupts, where JavaScript may be running for a long time.
|
|
213
284
|
pub fn interrupt_for_fork(handle: &v8::IsolateHandle) {
|
|
@@ -1304,8 +1304,9 @@ pub mod tests {
|
|
|
1304
1304
|
}
|
|
1305
1305
|
|
|
1306
1306
|
// Each worst case is linear, so it takes a fraction of a second even
|
|
1307
|
-
// unoptimized
|
|
1308
|
-
|
|
1307
|
+
// unoptimized, and a few seconds on a loaded CI runner; quadratic work at
|
|
1308
|
+
// these sizes would take hours.
|
|
1309
|
+
const BOUND: Duration = Duration::from_secs(30);
|
|
1309
1310
|
|
|
1310
1311
|
fn timed<T>(label: &str, work: impl FnOnce() -> T) -> T {
|
|
1311
1312
|
let started = Instant::now();
|
|
@@ -38,12 +38,10 @@ struct CauseSet(u8);
|
|
|
38
38
|
|
|
39
39
|
impl CauseSet {
|
|
40
40
|
// The causes that outlive the dispatch they arrived in, as upstream's
|
|
41
|
-
// non-watchdog termination bit does.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
| TerminationCause::HeapExhausted.bit(),
|
|
46
|
-
);
|
|
41
|
+
// non-watchdog termination bit does. An exhausted heap has no counterpart
|
|
42
|
+
// there and ends with its dispatch: the runner settles the heap once the
|
|
43
|
+
// dispatch is done, so a later script has nothing of it to report.
|
|
44
|
+
const PENDING: Self = Self(TerminationCause::Stop.bit() | TerminationCause::MaxMemory.bit());
|
|
47
45
|
|
|
48
46
|
// The causes upstream records in its request flag beside the one-shot
|
|
49
47
|
// interrupt, which V8 can use up while the flag stays.
|
|
@@ -127,7 +125,8 @@ impl DispatchKind {
|
|
|
127
125
|
}
|
|
128
126
|
|
|
129
127
|
/// A request to terminate. Timeouts and cancellations name the dispatch they
|
|
130
|
-
/// belong to and are dropped if it has already ended
|
|
128
|
+
/// belong to and are dropped if it has already ended; an exhausted heap is
|
|
129
|
+
/// dropped when none is armed.
|
|
131
130
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
132
131
|
pub enum Interrupt {
|
|
133
132
|
Stop,
|
|
@@ -198,6 +197,14 @@ struct Armed {
|
|
|
198
197
|
terminated: bool,
|
|
199
198
|
// `settle` has given the verdict, and the result is being serialized.
|
|
200
199
|
settled: bool,
|
|
200
|
+
// Its own timeout fired, which a nested script's must not take away.
|
|
201
|
+
timed_out: bool,
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#[derive(Clone, Copy)]
|
|
205
|
+
struct NestedTimer {
|
|
206
|
+
id: DispatchId,
|
|
207
|
+
fired: bool,
|
|
201
208
|
}
|
|
202
209
|
|
|
203
210
|
struct State<T> {
|
|
@@ -212,8 +219,9 @@ struct State<T> {
|
|
|
212
219
|
requests: u64,
|
|
213
220
|
disposing: bool,
|
|
214
221
|
suspended: bool,
|
|
215
|
-
//
|
|
216
|
-
|
|
222
|
+
// Nested scripts timed on their own, innermost last, each ending before
|
|
223
|
+
// the ones it is nested in.
|
|
224
|
+
nested: Vec<NestedTimer>,
|
|
217
225
|
next: u64,
|
|
218
226
|
}
|
|
219
227
|
|
|
@@ -225,7 +233,7 @@ impl<T: Terminator> State<T> {
|
|
|
225
233
|
}
|
|
226
234
|
self.cancel_armed();
|
|
227
235
|
self.suspended = false;
|
|
228
|
-
self.
|
|
236
|
+
self.nested.clear();
|
|
229
237
|
self.armed = None;
|
|
230
238
|
if self.disposing {
|
|
231
239
|
return Some(Halt::Disposed);
|
|
@@ -276,7 +284,7 @@ impl<T: Terminator> State<T> {
|
|
|
276
284
|
}
|
|
277
285
|
self.cancel_armed();
|
|
278
286
|
self.suspended = false;
|
|
279
|
-
self.
|
|
287
|
+
self.nested.clear();
|
|
280
288
|
let armed = self.armed.take()?;
|
|
281
289
|
if self.disposing {
|
|
282
290
|
return Some(Halt::Disposed);
|
|
@@ -308,7 +316,7 @@ impl<T> Default for TermCell<T> {
|
|
|
308
316
|
requests: 0,
|
|
309
317
|
disposing: false,
|
|
310
318
|
suspended: false,
|
|
311
|
-
|
|
319
|
+
nested: Vec::new(),
|
|
312
320
|
next: 0,
|
|
313
321
|
}),
|
|
314
322
|
}
|
|
@@ -345,6 +353,7 @@ impl<T: Terminator> TermCell<T> {
|
|
|
345
353
|
kind,
|
|
346
354
|
terminated: false,
|
|
347
355
|
settled: false,
|
|
356
|
+
timed_out: false,
|
|
348
357
|
});
|
|
349
358
|
if !state.live().is_empty() {
|
|
350
359
|
state.terminate_armed();
|
|
@@ -354,13 +363,25 @@ impl<T: Terminator> TermCell<T> {
|
|
|
354
363
|
}
|
|
355
364
|
|
|
356
365
|
/// Records `interrupt` and terminates the armed dispatch. Returns false
|
|
357
|
-
/// when the interrupt names a dispatch that is no longer armed
|
|
366
|
+
/// when the interrupt names a dispatch that is no longer armed, or is an
|
|
367
|
+
/// exhausted heap with none armed, which has no script to end.
|
|
358
368
|
pub fn request(&self, interrupt: Interrupt) -> bool {
|
|
359
369
|
let mut state = lock(&self.state);
|
|
360
|
-
if let Some(id) = interrupt.dispatch()
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
370
|
+
if let Some(id) = interrupt.dispatch() {
|
|
371
|
+
let timeout = matches!(interrupt, Interrupt::Timeout(_));
|
|
372
|
+
if let Some(armed) = state.armed.as_mut().filter(|armed| armed.id == id) {
|
|
373
|
+
armed.timed_out |= timeout;
|
|
374
|
+
} else if let Some(timer) = state
|
|
375
|
+
.nested
|
|
376
|
+
.iter_mut()
|
|
377
|
+
.find(|timer| timeout && timer.id == id)
|
|
378
|
+
{
|
|
379
|
+
timer.fired = true;
|
|
380
|
+
} else {
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if interrupt == Interrupt::HeapExhausted && state.armed.is_none() {
|
|
364
385
|
return false;
|
|
365
386
|
}
|
|
366
387
|
state.causes = state.causes.with(interrupt.cause());
|
|
@@ -488,34 +509,36 @@ impl<T: Terminator> TermCell<T> {
|
|
|
488
509
|
}
|
|
489
510
|
}
|
|
490
511
|
|
|
491
|
-
/// Gives a script
|
|
492
|
-
///
|
|
493
|
-
///
|
|
494
|
-
///
|
|
495
|
-
///
|
|
512
|
+
/// Gives a nested script a timeout of its own: an id the timeout names.
|
|
513
|
+
/// The caller asks only when nothing around the script ends it sooner: a
|
|
514
|
+
/// script nested in an untimed dispatch (a pump or a microtask checkpoint,
|
|
515
|
+
/// through a callback), as `mini_racer` arms its watchdog for any script
|
|
516
|
+
/// when none is armed, or one given a shorter timeout than every script
|
|
517
|
+
/// around it. `None` when nothing is armed.
|
|
496
518
|
pub fn time_nested(&self) -> Option<DispatchId> {
|
|
497
519
|
let mut state = lock(&self.state);
|
|
498
|
-
|
|
499
|
-
if armed.kind == DispatchKind::Script || state.nested_timer.is_some() {
|
|
500
|
-
return None;
|
|
501
|
-
}
|
|
520
|
+
state.armed?;
|
|
502
521
|
let id = DispatchId(state.next);
|
|
503
522
|
state.next = state.next.wrapping_add(1);
|
|
504
|
-
state.
|
|
523
|
+
state.nested.push(NestedTimer { id, fired: false });
|
|
505
524
|
drop(state);
|
|
506
525
|
Some(id)
|
|
507
526
|
}
|
|
508
527
|
|
|
509
528
|
/// Ends a nested script's own timeout. One that fired is dropped, and the
|
|
510
529
|
/// dispatch around the script carries on unless another cause needs the
|
|
511
|
-
/// termination, as `mini_racer` lifts its watchdog's.
|
|
530
|
+
/// termination, as `mini_racer` lifts its watchdog's. The armed
|
|
531
|
+
/// dispatch's own timeout, or an enclosing nested script's, once fired,
|
|
532
|
+
/// still needs it.
|
|
512
533
|
pub fn untime_nested(&self, id: DispatchId) {
|
|
513
534
|
let mut state = lock(&self.state);
|
|
514
|
-
|
|
535
|
+
let Some(index) = state.nested.iter().position(|timer| timer.id == id) else {
|
|
515
536
|
return;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
|
|
537
|
+
};
|
|
538
|
+
let timer = state.nested.remove(index);
|
|
539
|
+
let others = state.armed.is_some_and(|armed| armed.timed_out)
|
|
540
|
+
|| state.nested.iter().any(|timer| timer.fired);
|
|
541
|
+
if timer.fired && !others && state.causes.contains(TerminationCause::Timeout) {
|
|
519
542
|
let causes = state.causes.without(TerminationCause::Timeout);
|
|
520
543
|
state.set_causes(causes);
|
|
521
544
|
if state.causes.is_empty() && !state.disposing {
|
|
@@ -588,9 +611,9 @@ impl<T: Terminator> TermCell<T> {
|
|
|
588
611
|
|
|
589
612
|
/// Disarms `id` once its reply is complete. After `settle`, a cause that
|
|
590
613
|
/// landed while the result serialized, without stopping it, leaves the
|
|
591
|
-
/// result alone: a timeout or
|
|
592
|
-
/// cause waits for the next script, as `mini_racer`
|
|
593
|
-
/// that was never settled ends as `end` ends it.
|
|
614
|
+
/// result alone: a timeout, cancel or exhausted heap is dropped, and a
|
|
615
|
+
/// stop or `max_memory` cause waits for the next script, as `mini_racer`
|
|
616
|
+
/// leaves them. A dispatch that was never settled ends as `end` ends it.
|
|
594
617
|
pub fn finish(&self, id: DispatchId) -> Option<Halt> {
|
|
595
618
|
lock(&self.state).finish(id)
|
|
596
619
|
}
|
|
@@ -648,6 +671,40 @@ mod tests {
|
|
|
648
671
|
}
|
|
649
672
|
|
|
650
673
|
const STOPPED: Option<Halt> = Some(Halt::Terminated(TerminationCause::Stop));
|
|
674
|
+
const EXHAUSTED: Option<Halt> = Some(Halt::Terminated(TerminationCause::HeapExhausted));
|
|
675
|
+
|
|
676
|
+
// Whatever kind of dispatch the heap filled up in, and however it ended,
|
|
677
|
+
// the next script runs; with none armed, it is not recorded at all.
|
|
678
|
+
#[test]
|
|
679
|
+
fn an_exhausted_heap_ends_with_its_dispatch() {
|
|
680
|
+
let (cell, calls) = cell();
|
|
681
|
+
assert!(!cell.request(Interrupt::HeapExhausted));
|
|
682
|
+
assert_eq!(calls.terminate.get(), 0);
|
|
683
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
684
|
+
let id = cell
|
|
685
|
+
.begin(DispatchKind::Maintenance)
|
|
686
|
+
.expect("nothing armed");
|
|
687
|
+
cell.request(Interrupt::HeapExhausted);
|
|
688
|
+
assert_eq!(calls.terminate.get(), 1);
|
|
689
|
+
assert_eq!(cell.check(DispatchKind::Script), EXHAUSTED);
|
|
690
|
+
assert_eq!(cell.end(id), None);
|
|
691
|
+
assert!(!cell.refuses(DispatchKind::Script));
|
|
692
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
693
|
+
|
|
694
|
+
let id = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
695
|
+
assert_eq!(cell.settle(id), None);
|
|
696
|
+
cell.request(Interrupt::HeapExhausted);
|
|
697
|
+
assert_eq!(cell.finish(id), None);
|
|
698
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
699
|
+
|
|
700
|
+
let id = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
701
|
+
cell.request(Interrupt::Stop);
|
|
702
|
+
assert_eq!(cell.settle(id), STOPPED);
|
|
703
|
+
cell.request(Interrupt::HeapExhausted);
|
|
704
|
+
assert_eq!(cell.check(DispatchKind::Script), EXHAUSTED);
|
|
705
|
+
assert_eq!(cell.end(id), EXHAUSTED);
|
|
706
|
+
assert_eq!(run(&cell, DispatchKind::Script), None);
|
|
707
|
+
}
|
|
651
708
|
|
|
652
709
|
#[test]
|
|
653
710
|
fn a_stop_while_idle_is_reported_by_the_next_script_only() {
|
|
@@ -946,7 +1003,6 @@ mod tests {
|
|
|
946
1003
|
let (cell, calls) = cell();
|
|
947
1004
|
let pump = cell.begin(DispatchKind::Pump).expect("nothing armed");
|
|
948
1005
|
let nested = cell.time_nested().expect("an untimed dispatch");
|
|
949
|
-
assert_eq!(cell.time_nested(), None);
|
|
950
1006
|
|
|
951
1007
|
assert!(cell.request(Interrupt::Timeout(nested)));
|
|
952
1008
|
assert_eq!(calls.terminate.get(), 1);
|
|
@@ -979,14 +1035,96 @@ mod tests {
|
|
|
979
1035
|
}
|
|
980
1036
|
|
|
981
1037
|
#[test]
|
|
982
|
-
fn
|
|
983
|
-
let (cell,
|
|
984
|
-
let
|
|
985
|
-
|
|
986
|
-
|
|
1038
|
+
fn a_script_nested_in_a_script_can_be_timed_on_its_own() {
|
|
1039
|
+
let (cell, calls) = cell();
|
|
1040
|
+
let outer = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
1041
|
+
let nested = cell.time_nested().expect("a dispatch is armed");
|
|
1042
|
+
|
|
1043
|
+
assert!(cell.request(Interrupt::Timeout(nested)));
|
|
1044
|
+
assert_eq!(
|
|
1045
|
+
cell.check(DispatchKind::Script),
|
|
1046
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1047
|
+
);
|
|
1048
|
+
cell.untime_nested(nested);
|
|
1049
|
+
assert_eq!(calls.cancel.get(), 1);
|
|
1050
|
+
assert_eq!(cell.end(outer), None);
|
|
987
1051
|
assert_eq!(cell.time_nested(), None);
|
|
988
1052
|
}
|
|
989
1053
|
|
|
1054
|
+
#[test]
|
|
1055
|
+
fn a_nested_timeout_leaves_the_outer_scripts_own() {
|
|
1056
|
+
let (cell, calls) = cell();
|
|
1057
|
+
let outer = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
1058
|
+
let nested = cell.time_nested().expect("a dispatch is armed");
|
|
1059
|
+
cell.request(Interrupt::Timeout(nested));
|
|
1060
|
+
cell.request(Interrupt::Timeout(outer));
|
|
1061
|
+
|
|
1062
|
+
cell.untime_nested(nested);
|
|
1063
|
+
assert_eq!(calls.cancel.get(), 0);
|
|
1064
|
+
assert_eq!(
|
|
1065
|
+
cell.end(outer),
|
|
1066
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
#[test]
|
|
1071
|
+
fn nested_timers_stack_and_each_drops_only_its_own_timeout() {
|
|
1072
|
+
let (cell, calls) = cell();
|
|
1073
|
+
let pump = cell.begin(DispatchKind::Pump).expect("nothing armed");
|
|
1074
|
+
let enclosing = cell.time_nested().expect("a dispatch is armed");
|
|
1075
|
+
let inner = cell.time_nested().expect("a dispatch is armed");
|
|
1076
|
+
|
|
1077
|
+
assert!(cell.request(Interrupt::Timeout(inner)));
|
|
1078
|
+
cell.untime_nested(inner);
|
|
1079
|
+
assert_eq!(calls.cancel.get(), 1);
|
|
1080
|
+
assert_eq!(cell.check(DispatchKind::Script), None);
|
|
1081
|
+
assert!(!cell.request(Interrupt::Timeout(inner)));
|
|
1082
|
+
|
|
1083
|
+
assert!(cell.request(Interrupt::Timeout(enclosing)));
|
|
1084
|
+
assert_eq!(
|
|
1085
|
+
cell.check(DispatchKind::Script),
|
|
1086
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1087
|
+
);
|
|
1088
|
+
cell.untime_nested(enclosing);
|
|
1089
|
+
assert_eq!(calls.cancel.get(), 2);
|
|
1090
|
+
assert_eq!(cell.end(pump), None);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
#[test]
|
|
1094
|
+
fn an_enclosing_nested_timeout_that_fired_survives_the_inner_end() {
|
|
1095
|
+
let (cell, calls) = cell();
|
|
1096
|
+
let pump = cell.begin(DispatchKind::Pump).expect("nothing armed");
|
|
1097
|
+
let enclosing = cell.time_nested().expect("a dispatch is armed");
|
|
1098
|
+
let inner = cell.time_nested().expect("a dispatch is armed");
|
|
1099
|
+
cell.request(Interrupt::Timeout(inner));
|
|
1100
|
+
cell.request(Interrupt::Timeout(enclosing));
|
|
1101
|
+
|
|
1102
|
+
cell.untime_nested(inner);
|
|
1103
|
+
assert_eq!(calls.cancel.get(), 0);
|
|
1104
|
+
assert_eq!(
|
|
1105
|
+
cell.check(DispatchKind::Script),
|
|
1106
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1107
|
+
);
|
|
1108
|
+
cell.untime_nested(enclosing);
|
|
1109
|
+
assert_eq!(calls.cancel.get(), 1);
|
|
1110
|
+
assert_eq!(cell.end(pump), None);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
#[test]
|
|
1114
|
+
fn a_nested_timer_that_never_fired_drops_no_timeout() {
|
|
1115
|
+
let (cell, calls) = cell();
|
|
1116
|
+
let outer = cell.begin(DispatchKind::Script).expect("nothing armed");
|
|
1117
|
+
let nested = cell.time_nested().expect("a dispatch is armed");
|
|
1118
|
+
cell.request(Interrupt::Timeout(outer));
|
|
1119
|
+
|
|
1120
|
+
cell.untime_nested(nested);
|
|
1121
|
+
assert_eq!(calls.cancel.get(), 0);
|
|
1122
|
+
assert_eq!(
|
|
1123
|
+
cell.end(outer),
|
|
1124
|
+
Some(Halt::Terminated(TerminationCause::Timeout))
|
|
1125
|
+
);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
990
1128
|
#[test]
|
|
991
1129
|
fn a_child_takes_over_a_stop_and_a_memory_cause_but_no_timeout() {
|
|
992
1130
|
let (idle, _) = cell();
|
|
@@ -1028,10 +1166,10 @@ mod tests {
|
|
|
1028
1166
|
fn a_dispose_or_an_exhausted_heap_is_never_lifted() {
|
|
1029
1167
|
let (exhausted, calls) = cell();
|
|
1030
1168
|
exhausted.request(Interrupt::Stop);
|
|
1031
|
-
exhausted.request(Interrupt::HeapExhausted);
|
|
1032
1169
|
let outer = exhausted
|
|
1033
1170
|
.begin(DispatchKind::Script)
|
|
1034
1171
|
.expect("nothing armed");
|
|
1172
|
+
exhausted.request(Interrupt::HeapExhausted);
|
|
1035
1173
|
assert!(!exhausted.spend(exhausted.mark(), true));
|
|
1036
1174
|
assert!(exhausted.refuses(DispatchKind::Script));
|
|
1037
1175
|
assert_eq!(calls.cancel.get(), 0);
|
|
@@ -1199,9 +1337,9 @@ mod tests {
|
|
|
1199
1337
|
Some(Halt::Terminated(TerminationCause::MaxMemory))
|
|
1200
1338
|
);
|
|
1201
1339
|
|
|
1202
|
-
cell.request(Interrupt::HeapExhausted);
|
|
1340
|
+
assert!(!cell.request(Interrupt::HeapExhausted));
|
|
1203
1341
|
cell.relocked();
|
|
1204
|
-
assert!(cell.refuses(DispatchKind::Script));
|
|
1342
|
+
assert!(!cell.refuses(DispatchKind::Script));
|
|
1205
1343
|
}
|
|
1206
1344
|
|
|
1207
1345
|
// mini_racer's restore_termination: a nested script that reports a stop
|
|
@@ -6,18 +6,20 @@ use magnus::encoding::{EncodingCapable, RbEncoding};
|
|
|
6
6
|
use magnus::gc::Marker;
|
|
7
7
|
use magnus::r_hash::ForEach;
|
|
8
8
|
use magnus::scan_args::scan_args;
|
|
9
|
+
use magnus::typed_data::Obj;
|
|
9
10
|
use magnus::value::{LazyId, ReprValue};
|
|
10
11
|
use magnus::{
|
|
11
12
|
Class, DataTypeFunctions, Error, Fixnum, Module, RArray, RHash, RModule, RString, Ruby, Symbol,
|
|
12
|
-
TypedData, Value,
|
|
13
|
+
TryConvert, TypedData, Value,
|
|
13
14
|
};
|
|
14
|
-
use mini_racer_engine::
|
|
15
|
+
use mini_racer_engine::process::platform;
|
|
16
|
+
use mini_racer_engine::{ContextConfig, ContextHandle, Counts, EngineError, JsText, ScriptOptions};
|
|
15
17
|
|
|
16
18
|
use crate::encode;
|
|
17
19
|
use crate::gc_root::OwnedRoot;
|
|
18
20
|
use crate::host::{Callbacks, RubyHost, Waiter};
|
|
19
21
|
use crate::options::{self, OptionError, OptionValue};
|
|
20
|
-
use crate::{decode, errors, export, rb_raw, snapshot};
|
|
22
|
+
use crate::{decode, errors, export, rb_raw, snapshot, timeout};
|
|
21
23
|
|
|
22
24
|
// What Ruby's GC is told a live context holds outside its heap: roughly an
|
|
23
25
|
// isolate and its runner thread.
|
|
@@ -144,7 +146,7 @@ fn resolve_date_time(ruby: &Ruby) -> Result<(), Error> {
|
|
|
144
146
|
Ok(())
|
|
145
147
|
}
|
|
146
148
|
|
|
147
|
-
fn option_value(ruby: &Ruby, value: Value) -> OptionValue {
|
|
149
|
+
pub fn option_value(ruby: &Ruby, value: Value) -> OptionValue {
|
|
148
150
|
let other = || {
|
|
149
151
|
if value.is_nil() {
|
|
150
152
|
OptionValue::Nil
|
|
@@ -184,19 +186,41 @@ fn config(ruby: &Ruby, keywords: RHash) -> Result<ContextConfig, Error> {
|
|
|
184
186
|
pairs.push((key, value));
|
|
185
187
|
Ok(ForEach::Continue)
|
|
186
188
|
})?;
|
|
187
|
-
|
|
189
|
+
let config =
|
|
190
|
+
options::configure(pairs.into_iter()).map_err(|error| option_error(ruby, &error))?;
|
|
191
|
+
Ok(ContextConfig {
|
|
192
|
+
heap_limit: platform::heap_limit(),
|
|
193
|
+
..config
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
pub fn option_error(ruby: &Ruby, error: &OptionError) -> Error {
|
|
198
|
+
let message = error.to_string();
|
|
199
|
+
match *error {
|
|
188
200
|
OptionError::Type(_) | OptionError::SnapshotType(_) => {
|
|
189
|
-
errors::raised(ruby, ruby.exception_type_error(),
|
|
190
|
-
}
|
|
191
|
-
OptionError::Range(_) => {
|
|
192
|
-
errors::raised(ruby, ruby.exception_arg_error(), error.to_string())
|
|
201
|
+
errors::raised(ruby, ruby.exception_type_error(), message)
|
|
193
202
|
}
|
|
194
|
-
OptionError::
|
|
203
|
+
OptionError::Range(_) => errors::raised(ruby, ruby.exception_arg_error(), message),
|
|
204
|
+
OptionError::Keyword(_) => errors::runtime(ruby, message),
|
|
195
205
|
OptionError::InvalidSnapshot(invalid) => errors::engine(ruby, invalid.into()),
|
|
196
|
-
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/// Whether `value` is a `MiniRacer::Context` that can no longer run anything.
|
|
210
|
+
/// Makes no Ruby call.
|
|
211
|
+
pub fn is_disposed(ruby: &Ruby, value: Value) -> bool {
|
|
212
|
+
// Matched by name first, since a failed conversion builds its message
|
|
213
|
+
// through Ruby calls.
|
|
214
|
+
rb_raw::typed_data_name(ruby, value).as_deref() == Some("mini_racer/context")
|
|
215
|
+
&& Obj::<Context>::try_convert(value)
|
|
216
|
+
.is_ok_and(|context| context.handle.get().is_none_or(ContextHandle::is_disposed))
|
|
197
217
|
}
|
|
198
218
|
|
|
199
219
|
impl Context {
|
|
220
|
+
pub fn counters(&self, ruby: &Ruby) -> Result<Counts, Error> {
|
|
221
|
+
Ok(self.handle(ruby)?.counters())
|
|
222
|
+
}
|
|
223
|
+
|
|
200
224
|
fn handle(&self, ruby: &Ruby) -> Result<&ContextHandle, Error> {
|
|
201
225
|
self.handle
|
|
202
226
|
.get()
|
|
@@ -278,12 +302,12 @@ impl Context {
|
|
|
278
302
|
_ => JsText::from("<eval>"),
|
|
279
303
|
};
|
|
280
304
|
let source = js_text(ruby, source)?;
|
|
305
|
+
let options = ScriptOptions {
|
|
306
|
+
awaited,
|
|
307
|
+
timeout: timeout::current(ruby)?,
|
|
308
|
+
};
|
|
281
309
|
let bytes = rb_self.dispatch(ruby, |host| {
|
|
282
|
-
|
|
283
|
-
handle.eval_await(filename, source, host)
|
|
284
|
-
} else {
|
|
285
|
-
handle.eval(filename, source, host)
|
|
286
|
-
}
|
|
310
|
+
handle.evaluate(filename, source, options, host)
|
|
287
311
|
})?;
|
|
288
312
|
decode::value(ruby, &bytes)
|
|
289
313
|
}
|
|
@@ -307,13 +331,11 @@ impl Context {
|
|
|
307
331
|
let request = encode::call(ruby, name, args, |message| {
|
|
308
332
|
errors::runtime(ruby, [b"Context.call: ".as_slice(), &message].concat())
|
|
309
333
|
})?;
|
|
310
|
-
let
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
})?;
|
|
334
|
+
let options = ScriptOptions {
|
|
335
|
+
awaited,
|
|
336
|
+
timeout: timeout::current(ruby)?,
|
|
337
|
+
};
|
|
338
|
+
let bytes = rb_self.dispatch(ruby, |host| handle.invoke(request, options, host))?;
|
|
317
339
|
decode::value(ruby, &bytes)
|
|
318
340
|
}
|
|
319
341
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
use magnus::{Error, Object, RHash, RModule, Ruby};
|
|
2
|
+
|
|
3
|
+
use crate::context::Context;
|
|
4
|
+
use crate::export;
|
|
5
|
+
|
|
6
|
+
pub fn init(rs: RModule) -> Result<(), Error> {
|
|
7
|
+
rs.define_singleton_method("counters", export!(function counters, 1))?;
|
|
8
|
+
Ok(())
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Read without the context's lock, so a thread can read a context another
|
|
12
|
+
// thread is running.
|
|
13
|
+
fn counters(ruby: &Ruby, context: &Context) -> Result<RHash, Error> {
|
|
14
|
+
let counts = context.counters(ruby)?;
|
|
15
|
+
let terminated = counts.terminated;
|
|
16
|
+
let causes = ruby.hash_new();
|
|
17
|
+
for (name, count) in [
|
|
18
|
+
("stop", terminated.stop),
|
|
19
|
+
("timeout", terminated.timeout),
|
|
20
|
+
("interrupt", terminated.interrupt),
|
|
21
|
+
("max_memory", terminated.max_memory),
|
|
22
|
+
("heap_exhausted", terminated.heap_exhausted),
|
|
23
|
+
] {
|
|
24
|
+
causes.aset(ruby.to_symbol(name), count)?;
|
|
25
|
+
}
|
|
26
|
+
let hash = ruby.hash_new();
|
|
27
|
+
for (name, count) in [
|
|
28
|
+
("dispatches", counts.dispatches),
|
|
29
|
+
("callbacks", counts.callbacks),
|
|
30
|
+
("bytes_to_js", counts.bytes_to_js),
|
|
31
|
+
("bytes_from_js", counts.bytes_from_js),
|
|
32
|
+
("js_ns", counts.js_ns),
|
|
33
|
+
("gc_count", counts.gc_count),
|
|
34
|
+
("gc_ns", counts.gc_ns),
|
|
35
|
+
] {
|
|
36
|
+
hash.aset(ruby.to_symbol(name), count)?;
|
|
37
|
+
}
|
|
38
|
+
hash.aset(ruby.to_symbol("terminated"), causes)?;
|
|
39
|
+
Ok(hash)
|
|
40
|
+
}
|