mini_racer_rs 1.5.0 → 1.5.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf41190f924dfa0445ada46e1b47ed422f25b4d88de74d4c4f0397f4190282c9
4
- data.tar.gz: 330258ac6f3a92e31e23e8123dedeae52befe9eecb0a2409a10da574a9cf7893
3
+ metadata.gz: 34e3c7be1437cb48571d429539787241ee6056f1157f2ee4946caf3a08e0d41a
4
+ data.tar.gz: 4cb4111cfa8670145d793a55099a04f101f494af4732d0fa840da9599308a0c5
5
5
  SHA512:
6
- metadata.gz: 2fc22cbc008d1db4a8b8bf22e8d79177e20cd8ae97da99ab5f1962f601097dc8f37580ed4882ed5178c35c8be47bfb0d9a6b533fcf5b0ca281f928dfc2428c4b
7
- data.tar.gz: 288df9055b1beb4231c3701e523a2176ac2907ae7762c8282c6534ca6119197c7f1c46a3ff1f534364c30c29825877637433a6144636e52ffd0d0d22da10e3e2
6
+ metadata.gz: 8bd82f6313e99898bf053818a937e6089b409e662c5f304f1191df3ae90d11cfd99ee84861ec5bc9a651599c1a02fa2d778e4726fa543cb38306364517d9ac77
7
+ data.tar.gz: 0455a6d7c65f98d44fb079af4de1894547cfe942116a3407513da5810935e08a3ddf62673e605ee15e371b2cf71b84c0602b38559d51a831f356eff93fb2db38
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.5.1](https://github.com/andymai/mini_racer_rs/compare/v1.5.0...v1.5.1) (2026-10-23)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * free disposed context memory before timeout is due ([#141](https://github.com/andymai/mini_racer_rs/issues/141)) ([1b8fd96](https://github.com/andymai/mini_racer_rs/commit/1b8fd96057d09ebb4a7e4b30eaa71c0d436c5694))
9
+ * free snapshot blob bytes after contexts are gone ([#142](https://github.com/andymai/mini_racer_rs/issues/142)) ([72f4a2d](https://github.com/andymai/mini_racer_rs/commit/72f4a2d5aa287e065ffa9a1402a5255a5ea7db99))
10
+
3
11
  ## [1.5.0](https://github.com/andymai/mini_racer_rs/compare/v1.4.0...v1.5.0) (2026-10-23)
4
12
 
5
13
 
data/Cargo.lock CHANGED
@@ -555,9 +555,9 @@ dependencies = [
555
555
 
556
556
  [[package]]
557
557
  name = "magnus"
558
- version = "0.9.2"
558
+ version = "0.9.3"
559
559
  source = "registry+https://github.com/rust-lang/crates.io-index"
560
- checksum = "b9068710e00d21be762edca59b9c4c8946dbdb29820d4f76ad559afea07982c9"
560
+ checksum = "40ef424404d166cbb71c8b8d19c9c589c748c4547c1afa1928c5504299f6c713"
561
561
  dependencies = [
562
562
  "magnus-macros",
563
563
  "rb-sys",
@@ -593,7 +593,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
593
593
 
594
594
  [[package]]
595
595
  name = "mini_racer_engine"
596
- version = "1.5.0"
596
+ version = "1.5.1"
597
597
  dependencies = [
598
598
  "deno_core_icudata",
599
599
  "libc",
@@ -606,7 +606,7 @@ dependencies = [
606
606
 
607
607
  [[package]]
608
608
  name = "mini_racer_rs"
609
- version = "1.5.0"
609
+ version = "1.5.1"
610
610
  dependencies = [
611
611
  "magnus",
612
612
  "mini_racer_engine",
@@ -617,7 +617,7 @@ dependencies = [
617
617
 
618
618
  [[package]]
619
619
  name = "mini_racer_sourcemap"
620
- version = "1.5.0"
620
+ version = "1.5.1"
621
621
  dependencies = [
622
622
  "memchr",
623
623
  "serde",
@@ -627,7 +627,7 @@ dependencies = [
627
627
 
628
628
  [[package]]
629
629
  name = "mini_racer_sync"
630
- version = "1.5.0"
630
+ version = "1.5.1"
631
631
  dependencies = [
632
632
  "loom",
633
633
  "thiserror",
@@ -635,7 +635,7 @@ dependencies = [
635
635
 
636
636
  [[package]]
637
637
  name = "mini_racer_wire"
638
- version = "1.5.0"
638
+ version = "1.5.1"
639
639
  dependencies = [
640
640
  "criterion",
641
641
  "proptest",
data/Cargo.toml CHANGED
@@ -15,14 +15,14 @@ license = "MIT"
15
15
  publish = false
16
16
  repository = "https://github.com/andymai/mini_racer_rs"
17
17
  rust-version = "1.91"
18
- version = "1.5.0"
18
+ version = "1.5.1"
19
19
 
20
20
  [workspace.dependencies]
21
21
  criterion = {version = "0.8", default-features = false, features = ["cargo_bench_support"]}
22
22
  deno_core_icudata = "=0.78.0"
23
23
  libc = "0.2"
24
24
  loom = "0.7.2"
25
- magnus = {version = "=0.9.2", features = ["rb-sys"]}
25
+ magnus = {version = "=0.9.3", features = ["rb-sys"]}
26
26
  memchr = "2"
27
27
  mini_racer_engine = {path = "crates/mini_racer_engine"}
28
28
  mini_racer_sourcemap = {path = "crates/mini_racer_sourcemap"}
@@ -869,3 +869,57 @@ fn channel(error: ChannelError) -> EngineError {
869
869
  error => protocol(error),
870
870
  }
871
871
  }
872
+
873
+ #[cfg(test)]
874
+ mod tests {
875
+ use std::sync::Arc;
876
+ use std::time::{Duration, Instant};
877
+
878
+ use mini_racer_sync::Phase;
879
+
880
+ use super::{ContextHandle, Held, Host, WaitHandle, WaitOutcome};
881
+ use crate::control::ContextConfig;
882
+
883
+ struct Parking;
884
+
885
+ impl Host for Parking {
886
+ fn wait(&mut self, wait: &WaitHandle<'_>) -> WaitOutcome {
887
+ self.wait_blocking(wait);
888
+ WaitOutcome::Ready
889
+ }
890
+
891
+ fn wait_blocking(&mut self, wait: &WaitHandle<'_>) {
892
+ while !wait.ready() {
893
+ wait.park();
894
+ }
895
+ }
896
+ }
897
+
898
+ // A weak reference keeps the block's allocation, which a thread that made
899
+ // the context and exited leaves in a malloc arena other threads take over.
900
+ #[test]
901
+ fn the_watchdog_lets_go_of_a_disposed_context_before_its_timeout() {
902
+ let context = ContextHandle::spawn(
903
+ ContextConfig {
904
+ timeout: Some(Duration::from_secs(600)),
905
+ ..ContextConfig::default()
906
+ },
907
+ &mut Parking,
908
+ )
909
+ .expect("spawn");
910
+ let Held::Block(block) = context.held() else {
911
+ panic!("a new context holds its block");
912
+ };
913
+ let block = Arc::clone(block);
914
+ let untimed = Arc::weak_count(&block);
915
+ context.eval("<eval>", "1", &mut Parking).expect("eval");
916
+ context.begin_dispose();
917
+ context.dispose(&mut Parking).expect("dispose");
918
+ let deadline = Instant::now() + Duration::from_secs(30);
919
+ while block.phase.get() != Phase::Exited {
920
+ assert!(Instant::now() < deadline, "the runner did not exit");
921
+ std::thread::sleep(Duration::from_millis(1));
922
+ }
923
+ assert_eq!(Arc::weak_count(&block), untimed);
924
+ }
925
+ }
@@ -1,5 +1,5 @@
1
1
  use std::cmp::{Ordering, Reverse};
2
- use std::collections::BinaryHeap;
2
+ use std::collections::{BinaryHeap, HashMap};
3
3
  use std::sync::{Arc, Condvar, Mutex, Weak};
4
4
  use std::time::{Duration, Instant};
5
5
 
@@ -7,11 +7,13 @@ use mini_racer_sync::{DispatchId, Interrupt, lock, wait, wait_timeout};
7
7
 
8
8
  use crate::EngineError;
9
9
  use crate::control::{ControlBlock, Timer};
10
+ use crate::process::fork::Generation;
10
11
 
11
12
  struct Entry {
12
13
  at: Instant,
13
14
  seq: u64,
14
- block: Weak<ControlBlock>,
15
+ // The context's key in `Timers::blocks`.
16
+ block: usize,
15
17
  }
16
18
 
17
19
  impl PartialEq for Entry {
@@ -37,6 +39,15 @@ impl Ord for Entry {
37
39
  #[derive(Default)]
38
40
  struct Timers {
39
41
  heap: BinaryHeap<Reverse<Entry>>,
42
+ // The contexts the entries are for, by address, each from its first
43
+ // timed script until its runner exits (`forget`). Entries outlive their
44
+ // dispatches and a weak reference keeps a context's allocation, so an
45
+ // entry names its context rather than holds it: held to its deadline, a
46
+ // disposed context's allocation fragments the malloc arena of a thread
47
+ // that made it and exited. An address is mapped only while its context's
48
+ // allocation is, and an entry left after `forget` at most wakes the
49
+ // watchdog to reread a slot.
50
+ blocks: HashMap<usize, Weak<ControlBlock>>,
40
51
  seq: u64,
41
52
  }
42
53
 
@@ -46,16 +57,31 @@ pub struct Watchdog {
46
57
  }
47
58
 
48
59
  impl Watchdog {
49
- fn push(&self, at: Instant, block: Weak<ControlBlock>) {
60
+ fn push(&self, at: Instant, block: &Arc<ControlBlock>) {
61
+ let key = address(block);
50
62
  let mut timers = lock(&self.timers);
51
63
  timers.seq = timers.seq.wrapping_add(1);
52
64
  let seq = timers.seq;
53
- timers.heap.push(Reverse(Entry { at, seq, block }));
65
+ // A live reference at this address can only be this context's; a dead
66
+ // one is a context gone without `forget`, whose place this one took.
67
+ let slot = timers.blocks.entry(key).or_default();
68
+ if slot.strong_count() == 0 {
69
+ *slot = Arc::downgrade(block);
70
+ }
71
+ timers.heap.push(Reverse(Entry {
72
+ at,
73
+ seq,
74
+ block: key,
75
+ }));
54
76
  drop(timers);
55
77
  self.changed.notify_one();
56
78
  }
57
79
  }
58
80
 
81
+ fn address(block: &ControlBlock) -> usize {
82
+ std::ptr::from_ref(block).addr()
83
+ }
84
+
59
85
  // One thread per process serves every context's timeouts. It is kept per
60
86
  // fork generation: a forked child has the parent's watchdog state but not its
61
87
  // thread, and a runner of the parent may have held its lock at the fork.
@@ -118,7 +144,7 @@ fn arm(
118
144
  drop(timer);
119
145
  // A wake entry the watchdog no longer needs is harmless: it rereads the slot.
120
146
  if wake {
121
- watchdog.push(at, Arc::downgrade(block));
147
+ watchdog.push(at, block);
122
148
  }
123
149
  Ok(())
124
150
  }
@@ -166,6 +192,15 @@ pub fn clear_nested(block: &ControlBlock, id: DispatchId) {
166
192
  lock(&block.timer).nested.retain(|(_, due)| *due != id);
167
193
  }
168
194
 
195
+ /// Drops the watchdog's reference to a context whose runner has exited, so
196
+ /// a deadline still pending keeps none of it.
197
+ pub fn forget(block: &ControlBlock, generation: &Generation) {
198
+ let watchdog = lock(generation.watchdog()).clone();
199
+ if let Some(watchdog) = watchdog {
200
+ lock(&watchdog.timers).blocks.remove(&address(block));
201
+ }
202
+ }
203
+
169
204
  fn run(watchdog: &Watchdog) {
170
205
  loop {
171
206
  let mut timers = lock(&watchdog.timers);
@@ -186,8 +221,14 @@ fn run(watchdog: &Watchdog) {
186
221
  .peek()
187
222
  .is_some_and(|Reverse(entry)| entry.at <= now)
188
223
  {
189
- if let Some(Reverse(entry)) = timers.heap.pop() {
190
- due.push(entry.block);
224
+ if let Some(Reverse(entry)) = timers.heap.pop()
225
+ && let Some(block) = timers.blocks.get(&entry.block)
226
+ {
227
+ if block.strong_count() == 0 {
228
+ timers.blocks.remove(&entry.block);
229
+ } else {
230
+ due.push(Weak::clone(block));
231
+ }
191
232
  }
192
233
  }
193
234
  // Lock order: the heap is released before any context's timer
@@ -229,6 +270,6 @@ fn fire(watchdog: &Watchdog, block: &Arc<ControlBlock>, now: Instant) {
229
270
  block.term.request(Interrupt::Timeout(id));
230
271
  }
231
272
  if let Some(at) = next {
232
- watchdog.push(at, Arc::downgrade(block));
273
+ watchdog.push(at, block);
233
274
  }
234
275
  }
@@ -298,6 +298,7 @@ pub fn run(block: &Arc<ControlBlock>, generation: &'static Generation, start: St
298
298
  let _ = block.channel.respond(Ok(Vec::new()));
299
299
  }
300
300
  drop(closing);
301
+ watchdog::forget(block, generation);
301
302
  block.phase.set(Phase::Exited);
302
303
  }
303
304
 
@@ -77,8 +77,11 @@ const INTL_REFUSED: &str = "Intl is not supported in Snapshot.new scripts";
77
77
  /// V8 reads the blob for as long as an isolate made from it lives, whenever it
78
78
  /// makes a context from it or checks a code cache against it. Empty, it stands
79
79
  /// for no snapshot, as it does in `mini_racer`.
80
+ // The bytes have an allocation of their own, freed with the last strong
81
+ // reference: a `Weak` keeps its `Arc`'s allocation, and with it bytes kept
82
+ // inline there.
80
83
  #[derive(Clone, Default, PartialEq, Eq)]
81
- pub struct SnapshotBlob(Arc<[u8]>);
84
+ pub struct SnapshotBlob(Arc<Box<[u8]>>);
82
85
 
83
86
  impl SnapshotBlob {
84
87
  /// The word V8 opens each section of a blob with, which its deserializer
@@ -89,7 +92,7 @@ impl SnapshotBlob {
89
92
 
90
93
  pub fn new(bytes: &[u8]) -> Result<Self, InvalidSnapshot> {
91
94
  Self::check(bytes)?;
92
- Ok(Self(Arc::from(bytes)))
95
+ Ok(Self(Arc::new(Box::from(bytes))))
93
96
  }
94
97
 
95
98
  /// Refuses a blob as `mini_racer` does, in its order: one too long for
@@ -137,7 +140,7 @@ impl SnapshotBlob {
137
140
 
138
141
  /// A `SnapshotBlob` that lives only while a context made from it does.
139
142
  #[derive(Debug, Clone)]
140
- pub struct WeakSnapshotBlob(Weak<[u8]>);
143
+ pub struct WeakSnapshotBlob(Weak<Box<[u8]>>);
141
144
 
142
145
  impl WeakSnapshotBlob {
143
146
  #[must_use]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniRacerRs
4
- VERSION = "1.5.0"
4
+ VERSION = "1.5.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_racer_rs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Aragon