Open danthegoodman1 opened 2 years ago
Maybe if there was a way to pass in the max_heap_size
flag?
This works for limiting memory:
v8.SetFlags("--max-heap-size=32 --initial-heap-size=2")
However it sets for all contexts. We use 1 context per process for increased security but I believe it won't be enough for all.
@mustafaakin all contexts as in shared or that is the limit for each individual context?
I believe it's global, shared all.
Flags are here:
Or
https://gist.github.com/andrewiggins/68c3165d47769a39eb5ae16e3001d6c6
isolated-vm project has a configurable limit per isolate:
It passes the following:
I believe the Isolate* iso = Isolate::New(params);
function can be replaced with auto holder = IsolateEnvironment::New(memory_limit, std::move(snapshot_blob), snapshot_blob_length);
somehow but neither my C or CGO is good enough to implement it.
https://github.com/rogchap/v8go/blob/cee5f84bb54338bdef150b3e43daace4669c5de2/v8go.cc#L154
@mustafaakin thanks for the pointers! I'm not very fluent in CGO either nor v8 specifically so I'd be treading dangerous waters playing in there. Haven't had the time to tinker on my own, perhaps I should find someone fluent enough to bother :)
I'd love to make it per isolate and or context so I can limit time and memory, right now I am coupling golang and isolated-vm to handle this.
Not sure I understand the proposed solution, I tried limiting memory through flag (I have set the flag "--max-heap-size 16") and got exception in cgo, which I cannot recover from in go:
<--- Last few GCs --->
[1:0x7f51ef55c5e0] 40374 ms: Mark-Compact (reduce) 28.3 (29.1) -> 17.1 (17.9) MB, 254.7 / 0.0 ms (average mu = 0.498, current mu = 0.408) allocation failure; scavenge might not succeed
<--- JS stacktrace --->
#
# Fatal javascript OOM in Reached heap limit
#
SIGTRAP: trace trap
PC=0xdf4076 m=20 sigcode=128
signal arrived during cgo execution
It would be extremely useful to be able to limit the memory usage of a context so that it cannot exceed some limit (and that we can also view how much was used, but I believe that is already possible).
Maybe a hack to work around this would be to poll the
Isolate.GetHeapStatistics()
, but that does not allow for fast respone to large memory usage.