mini_racer 0.2.8 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f6c48eedae322fc66e7dc1f816d63d8d093a18a02167a16cada1cd04a34e821
4
- data.tar.gz: 0b4de89162986b4d729abff697c04de4f54216bb1a94c5d7737b7cf011fca85e
3
+ metadata.gz: 420c1d474734931c13f1246154eb045600ce24db66c05904c5df19681a03e41b
4
+ data.tar.gz: 403320470422a7085305e6139cf769cdd5fe7e0df236830038d412ac2558c961
5
5
  SHA512:
6
- metadata.gz: ce21ef957f04319ed5109e6f5ddbc2c17be7f657d9beed5941950ccf57d36054341c5f6685c790ab217d8445e1af1758e7d16c1745d47955042453c6294784c3
7
- data.tar.gz: 4e58fdfde4f8f78ade9c470510296cb8dcd30e3c02a8827f4c4ba68ee49e41dee66de5419938c52c98d9df41d1dd02433b5b0d5389f0d0664da100f5087e0db1
6
+ metadata.gz: ef58b7272a5f641f3dc264241414d45cde6d689a34d3433b5a039dc1caeac054c435150a3b5c2532f7957efad083584fe43d4a6c8b21789c542d611c632ae080
7
+ data.tar.gz: 74b33249d8141b4d869b3c8f74617e40e271c36e472b32950391a061d27069c9b108a29884acf056ce1467c4bc3c22a6f4f84e022df53153cf30ccae387c69f1
@@ -1,9 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3
4
3
  - 2.4
5
4
  - 2.5
6
5
  - 2.6
6
+ - 2.7
7
7
  - ruby-head
8
8
  matrix:
9
9
  include:
data/CHANGELOG CHANGED
@@ -1,3 +1,34 @@
1
+ - 15-05-2020
2
+
3
+ - 0.2.13
4
+ - FIX: edge case around ensure_gc_after_idle possibly firing when context is not idle
5
+
6
+ - 15-05-2020
7
+
8
+ - 0.2.12
9
+
10
+ - FEATURE: isolate.low_memory_notification which can force a full GC
11
+ - FEATURE: MiniRacer::Context.new(ensure_gc_after_idle: 2) - to force full GC 2 seconds after context is idle, this allows you to conserve memory on isolates
12
+
13
+ - 14-05-2020
14
+
15
+ - 0.2.11
16
+
17
+ - FIX: dumping heap snapshots was not flushing the file leading to corrupt snapshots
18
+ - FIX: a use-after-free shutdown crash
19
+
20
+ - 0.2.10
21
+
22
+ - 22-04-2020
23
+
24
+ - FEATURE: memory softlimit support for nogvl_context_call
25
+
26
+ - 0.2.9
27
+
28
+ - 09-01-2020
29
+
30
+ - FIX: correct segfault when JS returns a Symbol and properly cast to ruby symbol
31
+
1
32
  - 0.2.8
2
33
 
3
34
  - 11-11-2019
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MiniRacer
2
2
 
3
- [![Build Status](https://travis-ci.org/discourse/mini_racer.svg?branch=master)](https://travis-ci.org/discourse/mini_racer)
3
+ [![Build Status](https://travis-ci.org/rubyjs/mini_racer.svg?branch=master)](https://travis-ci.org/rubyjs/mini_racer)
4
4
 
5
5
  Minimal, modern embedded V8 for Ruby.
6
6
 
@@ -230,12 +230,17 @@ context = MiniRacer::Context.new(isolate: isolate)
230
230
  # give up to 100ms for V8 garbage collection
231
231
  isolate.idle_notification(100)
232
232
 
233
+ # force V8 to perform a full GC
234
+ isolate.low_memory_notification
235
+
233
236
  ```
234
237
 
235
238
  This can come in handy to force V8 GC runs for example in between requests if you use MiniRacer on a web application.
236
239
 
237
240
  Note that this method maps directly to [`v8::Isolate::IdleNotification`](http://bespin.cz/~ondras/html/classv8_1_1Isolate.html#aea16cbb2e351de9a3ae7be2b7cb48297), and that in particular its return value is the same (true if there is no further garbage to collect, false otherwise) and the same caveats apply, in particular that `there is no guarantee that the [call will return] within the time limit.`
238
241
 
242
+ Additionally you may automate this process on a context by defining it with `MiniRacer::Content.new(ensure_gc_after_idle: 1)`. Using this will ensure V8 will run a full GC using `context.isolate.low_memory_notification` 1 second after the last eval on the context. Low memory notification is both slower and more aggressive than an idle_notification and will ensure long living isolates use minimal amounts of memory.
243
+
239
244
  ### V8 Runtime flags
240
245
 
241
246
  It is possible to set V8 Runtime flags:
@@ -273,7 +278,7 @@ context.eval js
273
278
  The same code without the harmony runtime flag results in a `MiniRacer::RuntimeError: RangeError: Maximum call stack size exceeded` exception.
274
279
  Please refer to http://node.green/ as a reference on other harmony features.
275
280
 
276
- A list of all V8 runtime flags can be found using `node --v8-options`, or else by perusing [the V8 source code for flags (make sure to use the right version of V8)](https://github.com/v8/v8/blob/master/src/flag-definitions.h).
281
+ A list of all V8 runtime flags can be found using `node --v8-options`, or else by perusing [the V8 source code for flags (make sure to use the right version of V8)](https://github.com/v8/v8/blob/master/src/flags/flag-definitions.h).
277
282
 
278
283
  Note that runtime flags must be set before any other operation (e.g. creating a context, a snapshot or an isolate), otherwise an exception will be thrown.
279
284
 
@@ -447,7 +452,7 @@ Add this to your .travis.yml file:
447
452
 
448
453
  ## Contributing
449
454
 
450
- Bug reports and pull requests are welcome on GitHub at https://github.com/discourse/mini_racer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
455
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubyjs/mini_racer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
451
456
 
452
457
 
453
458
  ## License
@@ -127,6 +127,7 @@ typedef struct {
127
127
  Local<Function> fun;
128
128
  Local<Value> *argv;
129
129
  EvalResult result;
130
+ size_t max_memory;
130
131
  } FunctionCall;
131
132
 
132
133
  enum IsolateFlags {
@@ -155,6 +156,10 @@ static VALUE rb_cDateTime = Qnil;
155
156
  static std::unique_ptr<Platform> current_platform = NULL;
156
157
  static std::mutex platform_lock;
157
158
 
159
+ static pthread_attr_t *thread_attr_p;
160
+ static pthread_rwlock_t exit_lock = PTHREAD_RWLOCK_INITIALIZER;
161
+ static bool ruby_exiting; // guarded by exit_lock
162
+
158
163
  static VALUE rb_platform_set_flag_as_str(VALUE _klass, VALUE flag_as_str) {
159
164
  bool platform_already_initialized = false;
160
165
 
@@ -444,8 +449,27 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local context,
444
449
  return rb_hash;
445
450
  }
446
451
 
447
- Local<String> rstr = value->ToString(context).ToLocalChecked();
448
- return rb_enc_str_new(*String::Utf8Value(isolate, rstr), rstr->Utf8Length(isolate), rb_enc_find("utf-8"));
452
+ if (value->IsSymbol()) {
453
+ v8::String::Utf8Value symbol_name(isolate,
454
+ Local<Symbol>::Cast(value)->Name());
455
+
456
+ VALUE str_symbol = rb_enc_str_new(
457
+ *symbol_name,
458
+ symbol_name.length(),
459
+ rb_enc_find("utf-8")
460
+ );
461
+
462
+ return ID2SYM(rb_intern_str(str_symbol));
463
+ }
464
+
465
+ MaybeLocal<String> rstr_maybe = value->ToString(context);
466
+
467
+ if (rstr_maybe.IsEmpty()) {
468
+ return Qnil;
469
+ } else {
470
+ Local<String> rstr = rstr_maybe.ToLocalChecked();
471
+ return rb_enc_str_new(*String::Utf8Value(isolate, rstr), rstr->Utf8Length(isolate), rb_enc_find("utf-8"));
472
+ }
449
473
  }
450
474
 
451
475
  static VALUE convert_v8_to_ruby(Isolate* isolate,
@@ -745,6 +769,16 @@ static VALUE rb_isolate_idle_notification(VALUE self, VALUE idle_time_in_ms) {
745
769
  return isolate_info->isolate->IdleNotificationDeadline(now + duration) ? Qtrue : Qfalse;
746
770
  }
747
771
 
772
+ static VALUE rb_isolate_low_memory_notification(VALUE self) {
773
+ IsolateInfo* isolate_info;
774
+ Data_Get_Struct(self, IsolateInfo, isolate_info);
775
+
776
+ if (current_platform == NULL) return Qfalse;
777
+
778
+ isolate_info->isolate->LowMemoryNotification();
779
+ return Qnil;
780
+ }
781
+
748
782
  static VALUE rb_context_init_unsafe(VALUE self, VALUE isolate, VALUE snap) {
749
783
  ContextInfo* context_info;
750
784
  Data_Get_Struct(self, ContextInfo, context_info);
@@ -1185,7 +1219,7 @@ void free_isolate(IsolateInfo* isolate_info) {
1185
1219
  delete isolate_info->allocator;
1186
1220
  }
1187
1221
 
1188
- static void *free_context_raw(void* arg) {
1222
+ static void free_context_raw(void *arg) {
1189
1223
  ContextInfo* context_info = (ContextInfo*)arg;
1190
1224
  IsolateInfo* isolate_info = context_info->isolate_info;
1191
1225
  Persistent<Context>* context = context_info->context;
@@ -1202,6 +1236,20 @@ static void *free_context_raw(void* arg) {
1202
1236
  }
1203
1237
 
1204
1238
  xfree(context_info);
1239
+ }
1240
+
1241
+ static void *free_context_thr(void* arg) {
1242
+ if (pthread_rwlock_tryrdlock(&exit_lock) != 0) {
1243
+ return NULL;
1244
+ }
1245
+ if (ruby_exiting) {
1246
+ return NULL;
1247
+ }
1248
+
1249
+ free_context_raw(arg);
1250
+
1251
+ pthread_rwlock_unlock(&exit_lock);
1252
+
1205
1253
  return NULL;
1206
1254
  }
1207
1255
 
@@ -1215,22 +1263,17 @@ static void free_context(ContextInfo* context_info) {
1215
1263
  context_info_copy->context = context_info->context;
1216
1264
 
1217
1265
  if (isolate_info && isolate_info->refs() > 1) {
1218
- pthread_t free_context_thread;
1219
- if (pthread_create(&free_context_thread, NULL, free_context_raw, (void*)context_info_copy)) {
1220
- fprintf(stderr, "WARNING failed to release memory in MiniRacer, thread to release could not be created, process will leak memory\n");
1221
- }
1222
-
1266
+ pthread_t free_context_thread;
1267
+ if (pthread_create(&free_context_thread, thread_attr_p,
1268
+ free_context_thr, (void*)context_info_copy)) {
1269
+ fprintf(stderr, "WARNING failed to release memory in MiniRacer, thread to release could not be created, process will leak memory\n");
1270
+ }
1223
1271
  } else {
1224
1272
  free_context_raw(context_info_copy);
1225
1273
  }
1226
1274
 
1227
- if (context_info->context && isolate_info && isolate_info->isolate) {
1228
- context_info->context = NULL;
1229
- }
1230
-
1231
- if (isolate_info) {
1232
- context_info->isolate_info = NULL;
1233
- }
1275
+ context_info->context = NULL;
1276
+ context_info->isolate_info = NULL;
1234
1277
  }
1235
1278
 
1236
1279
  static void deallocate_isolate(void* data) {
@@ -1388,6 +1431,8 @@ rb_heap_snapshot(VALUE self, VALUE file) {
1388
1431
  FileOutputStream stream(fp);
1389
1432
  snap->Serialize(&stream, HeapSnapshot::kJSON);
1390
1433
 
1434
+ fflush(fp);
1435
+
1391
1436
  const_cast<HeapSnapshot*>(snap)->Delete();
1392
1437
 
1393
1438
  return Qtrue;
@@ -1435,6 +1480,12 @@ nogvl_context_call(void *args) {
1435
1480
  // terminate ASAP
1436
1481
  isolate->SetData(DO_TERMINATE, (void*)false);
1437
1482
 
1483
+ if (call->max_memory > 0) {
1484
+ isolate->SetData(MEM_SOFTLIMIT_VALUE, &call->max_memory);
1485
+ isolate->SetData(MEM_SOFTLIMIT_REACHED, (void*)false);
1486
+ isolate->AddGCEpilogueCallback(gc_callback);
1487
+ }
1488
+
1438
1489
  Isolate::Scope isolate_scope(isolate);
1439
1490
  EscapableHandleScope handle_scope(isolate);
1440
1491
  TryCatch trycatch(isolate);
@@ -1492,6 +1543,13 @@ static VALUE rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
1492
1543
  call_argv = argv + 1;
1493
1544
  }
1494
1545
 
1546
+ call.max_memory = 0;
1547
+ VALUE mem_softlimit = rb_iv_get(self, "@max_memory");
1548
+ if (mem_softlimit != Qnil) {
1549
+ unsigned long sl_int = NUM2ULONG(mem_softlimit);
1550
+ call.max_memory = (size_t)sl_int;
1551
+ }
1552
+
1495
1553
  bool missingFunction = false;
1496
1554
  {
1497
1555
  Locker lock(isolate);
@@ -1549,6 +1607,16 @@ static VALUE rb_context_create_isolate_value(VALUE self) {
1549
1607
  return Data_Wrap_Struct(rb_cIsolate, NULL, &deallocate_isolate, isolate_info);
1550
1608
  }
1551
1609
 
1610
+ static void set_ruby_exiting(VALUE value) {
1611
+ (void)value;
1612
+
1613
+ int res = pthread_rwlock_wrlock(&exit_lock);
1614
+ ruby_exiting = true;
1615
+ if (res == 0) {
1616
+ pthread_rwlock_unlock(&exit_lock);
1617
+ }
1618
+ }
1619
+
1552
1620
  extern "C" {
1553
1621
 
1554
1622
  void Init_mini_racer_extension ( void )
@@ -1599,8 +1667,19 @@ extern "C" {
1599
1667
  rb_define_private_method(rb_cSnapshot, "load", (VALUE(*)(...))&rb_snapshot_load, 1);
1600
1668
 
1601
1669
  rb_define_method(rb_cIsolate, "idle_notification", (VALUE(*)(...))&rb_isolate_idle_notification, 1);
1670
+ rb_define_method(rb_cIsolate, "low_memory_notification", (VALUE(*)(...))&rb_isolate_low_memory_notification, 0);
1671
+
1602
1672
  rb_define_private_method(rb_cIsolate, "init_with_snapshot",(VALUE(*)(...))&rb_isolate_init_with_snapshot, 1);
1603
1673
 
1604
1674
  rb_define_singleton_method(rb_cPlatform, "set_flag_as_str!", (VALUE(*)(...))&rb_platform_set_flag_as_str, 1);
1675
+
1676
+ rb_set_end_proc(set_ruby_exiting, Qnil);
1677
+
1678
+ static pthread_attr_t attr;
1679
+ if (pthread_attr_init(&attr) == 0) {
1680
+ if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0) {
1681
+ thread_attr_p = &attr;
1682
+ }
1683
+ }
1605
1684
  }
1606
1685
  }
@@ -145,6 +145,15 @@ module MiniRacer
145
145
  end
146
146
  # false signals it should be fetched if requested
147
147
  @isolate = options[:isolate] || false
148
+
149
+ @ensure_gc_after_idle = options[:ensure_gc_after_idle]
150
+
151
+ if @ensure_gc_after_idle
152
+ @last_eval = nil
153
+ @ensure_gc_thread = nil
154
+ @ensure_gc_mutex = Mutex.new
155
+ end
156
+
148
157
  @disposed = false
149
158
 
150
159
  @callback_mutex = Mutex.new
@@ -203,6 +212,7 @@ module MiniRacer
203
212
  end
204
213
  ensure
205
214
  @eval_thread = nil
215
+ ensure_gc_thread if @ensure_gc_after_idle
206
216
  end
207
217
 
208
218
  def call(function_name, *arguments)
@@ -216,15 +226,17 @@ module MiniRacer
216
226
  end
217
227
  ensure
218
228
  @eval_thread = nil
229
+ ensure_gc_thread if @ensure_gc_after_idle
219
230
  end
220
231
 
221
232
  def dispose
222
233
  return if @disposed
223
234
  isolate_mutex.synchronize do
235
+ return if @disposed
224
236
  dispose_unsafe
237
+ @disposed = true
238
+ @isolate = nil # allow it to be garbage collected, if set
225
239
  end
226
- @disposed = true
227
- @isolate = nil # allow it to be garbage collected, if set
228
240
  end
229
241
 
230
242
 
@@ -273,6 +285,37 @@ module MiniRacer
273
285
 
274
286
  private
275
287
 
288
+ def ensure_gc_thread
289
+ @last_eval = Process.clock_gettime(Process::CLOCK_MONOTONIC)
290
+ @ensure_gc_mutex.synchronize do
291
+ @ensure_gc_thread = nil if !@ensure_gc_thread&.alive?
292
+ @ensure_gc_thread ||= Thread.new do
293
+ done = false
294
+ while !done
295
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
296
+
297
+ if @disposed
298
+ @ensure_gc_thread = nil
299
+ break
300
+ end
301
+
302
+ if !@eval_thread && @ensure_gc_after_idle < now - @last_eval
303
+ @ensure_gc_mutex.synchronize do
304
+ isolate_mutex.synchronize do
305
+ if !@eval_thread
306
+ isolate.low_memory_notification if !@disposed
307
+ @ensure_gc_thread = nil
308
+ done = true
309
+ end
310
+ end
311
+ end
312
+ end
313
+ sleep @ensure_gc_after_idle if !done
314
+ end
315
+ end
316
+ end
317
+ end
318
+
276
319
  def stop_attached
277
320
  @callback_mutex.synchronize{
278
321
  if @callback_running
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MiniRacer
2
- VERSION = "0.2.8"
4
+ VERSION = "0.2.13"
3
5
  end
@@ -14,18 +14,24 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://github.com/discourse/mini_racer"
15
15
  spec.license = "MIT"
16
16
 
17
+ spec.metadata = {
18
+ "bug_tracker_uri" => "https://github.com/discourse/mini_racer/issues",
19
+ "changelog_uri" => "https://github.com/discourse/mini_racer/blob/v#{spec.version}/CHANGELOG",
20
+ "documentation_uri" => "https://www.rubydoc.info/gems/mini_racer/#{spec.version}",
21
+ "source_code_uri" => "https://github.com/discourse/mini_racer/tree/v#{spec.version}",
22
+ }
17
23
 
18
24
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(benchmark|test|spec|features|examples)/}) }
19
25
  spec.bindir = "exe"
20
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
27
  spec.require_paths = ["lib"]
22
28
 
23
- spec.add_development_dependency "bundler", "~> 1.12"
24
- spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "bundler"
30
+ spec.add_development_dependency "rake", ">= 12.3.3"
25
31
  spec.add_development_dependency "minitest", "~> 5.0"
26
32
  spec.add_development_dependency "rake-compiler"
27
33
 
28
- spec.add_dependency 'libv8', '>= 6.9.411'
34
+ spec.add_dependency 'libv8', '> 7.3'
29
35
  spec.require_paths = ["lib", "ext"]
30
36
 
31
37
  spec.extensions = ["ext/mini_racer_extension/extconf.rb"]
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_racer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-11 00:00:00.000000000 Z
11
+ date: 2020-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.12'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.12'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: libv8
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - ">"
74
74
  - !ruby/object:Gem::Version
75
- version: 6.9.411
75
+ version: '7.3'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - ">"
81
81
  - !ruby/object:Gem::Version
82
- version: 6.9.411
82
+ version: '7.3'
83
83
  description: Minimal embedded v8 engine for Ruby
84
84
  email:
85
85
  - sam.saffron@gmail.com
@@ -106,7 +106,11 @@ files:
106
106
  homepage: https://github.com/discourse/mini_racer
107
107
  licenses:
108
108
  - MIT
109
- metadata: {}
109
+ metadata:
110
+ bug_tracker_uri: https://github.com/discourse/mini_racer/issues
111
+ changelog_uri: https://github.com/discourse/mini_racer/blob/v0.2.13/CHANGELOG
112
+ documentation_uri: https://www.rubydoc.info/gems/mini_racer/0.2.13
113
+ source_code_uri: https://github.com/discourse/mini_racer/tree/v0.2.13
110
114
  post_install_message:
111
115
  rdoc_options: []
112
116
  require_paths: