Skip to content

Commit 877bb50

Browse files
authored
Improve nodejs stack size limit heuristic.
Setting stack size exactly to the system limit will cause node to segfault before it has a chance to notice that it ran out of space. In my experiemnts values closer than 64 KiB may still cause a segfault from time to time. I also changed the multiplier to 1024, since that's what v8 actually uses for the flag value.
1 parent d431144 commit 877bb50

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tool.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -891,17 +891,23 @@ func runNode(script string, args []string, dir string, quiet bool, out io.Writer
891891
// - OS process limit
892892
// - Node.js (V8) limit
893893
//
894-
// GopherJS fetches the current OS process limit, and sets the
895-
// Node.js limit to the same value. So both limits are kept in sync
896-
// and can be controlled by setting OS process limit. E.g.:
894+
// GopherJS fetches the current OS process limit, and sets the Node.js limit
895+
// to a value slightly below it (otherwise nodejs is likely to segfault).
896+
// The backoff size has been determined experimentally on a linux machine,
897+
// so it may not be 100% reliable. So both limits are kept in sync and can
898+
// be controlled by setting OS process limit. E.g.:
897899
//
898900
// ulimit -s 10000 && gopherjs test
899901
//
900902
cur, err := sysutil.RlimitStack()
901903
if err != nil {
902904
return fmt.Errorf("failed to get stack size limit: %v", err)
903905
}
904-
allArgs = append(allArgs, fmt.Sprintf("--stack_size=%v", cur/1000)) // Convert from bytes to KB.
906+
cur = cur / 1024 // Convert bytes to KiB.
907+
if backoff := uint64(64); cur > backoff {
908+
cur = cur - backoff
909+
}
910+
allArgs = append(allArgs, fmt.Sprintf("--stack_size=%v", cur))
905911
}
906912

907913
allArgs = append(allArgs, script)

0 commit comments

Comments
 (0)