Fix failure in building Gitea on OpenBSD because of bad memory allocation in Node.js
Tags: gitea, openbsd, node, troubleshooting
Published: 2021-05-10 07:13:33

## Trouble

I built my [Gitea](https://gitea.io/0) server on [OpenBSD](https://www.openbsd.org/) and have managed it for years.

{% post nabbisen/gitea-on-openbsd-from-source-code-update-21c0 %}

Last month and this month, I was struggling to update it from 1.13.7 to 1.14.[0-2]. I met the fatal error about memory allocation below and failed every time.

## Solution

It didn't seem to always happen on OpenBSD servers, because that in OpenBSD [Ports](https://www.openbsd.org/faq/ports/ports.html) system has been updated to 1.14. At last, I found it was because the memory size of my server was too small and seemed not to get along with Node.js and its V8 module.

I modified the updating command to add [V8](https://nodejs.org/api/v8.html)'s [`max_old_space_size`](https://nodejs.org/api/cli.html#cli_max_old_space_size_size_in_megabytes) option of Node.js so as to limit memory consumption.

```diff
- env TAGS="bindata" make -f BSDmakefile
+ env TAGS="bindata" NODE_OPTIONS="--max_old_space_size=1024" make -f BSDmakefile
```

It was successful🙂

Besides, the official documentation [says](https://nodejs.org/api/cli.html#cli_max_old_space_size_size_in_megabytes):

> On a machine with 2GB of memory, consider setting this to 1536 (1.5GB) to leave some memory for other uses and avoid swapping.

## Description

Here are logs I got when I ran the original command and failed.

### 1st trial

It said "std::bad_alloc" after `npx webpack` but I didn't know the detail.

```console
$ env TAGS="bindata" make -f BSDmakefile generate build
Running go generate...
generating bindata for options
generating bindata for public
generating bindata for templates
npm install --no-save

> core-js@3.8.1 postinstall /var/www/sites/scqr.net/gitea/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

added 224 packages from 1007 contributors, removed 988 packages, updated 282 packages and moved 6 packages in 105.905s
rm -rf public/js public/css public/fonts public/img/webpack public/serviceworker.js
npx webpack
terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
gmake: *** [Makefile:668: public/js/index.js] Abort trap (core dumped)
*** Error 2 in /var/www/sites/scqr.net/gitea (BSDmakefile:46 'FRC': "gmake" "--no-print-directory" generate build )
```

### 2nd trial

I ran the same command. Curiously, log has been changed without my manual change in OS, F/W or files permission.
It said clearly "FATAL ERROR: NewSpace::Rebalance Allocation failed - JavaScript heap out of memory".

```console
$ env TAGS="bindata" make -f BSDmakefile generate build
Running go generate...
bindata for options already up-to-date
generating bindata for public
bindata for templates already up-to-date
rm -rf public/js public/css public/fonts public/img/webpack public/serviceworker.js
npx webpack

<--- Last few GCs --->

[90805:0x7a9cc648000]    16873 ms: Scavenge 253.8 (270.9) -> 245.3 (274.6) MB, 100.5 / 0.0 ms  (average mu = 1.000, current mu = 1.000) allocation failure 
[90805:0x7a9cc648000]    17997 ms: Scavenge 258.2 (275.1) -> 249.5 (279.1) MB, 841.7 / 0.0 ms  (average mu = 1.000, current mu = 1.000) allocation failure 
[90805:0x7a9cc648000]    18190 ms: Scavenge 262.5 (279.6) -> 253.8 (283.4) MB, 17.2 / 0.0 ms  (average mu = 1.000, current mu = 1.000) allocation failure 


<--- JS stacktrace --->
Cannot get stack trace in GC.
FATAL ERROR: NewSpace::Rebalance Allocation failed - JavaScript heap out of memory

Writing Node.js report to file: report.20210510.101055.90805.0.001.json

#
# Fatal error in , line 0
# Fatal process out of memory: Zone
#
#
#
#FailureMessage Object: 0x7aa023e61a0
 1: 0x7a784e46689 node::MultiIsolatePlatform::CancelPendingDelayedTasks(v8::Isolate*) [node]
 2: 0x7a785bbcb11 V8_Fatal(char const*, ...) [node]
 3: 0x7a784f2c9d9 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 4: 0x7a78559fb30 v8::internal::Zone::ReleaseMemory() [node]
 5: 0x7a785d941e5 std::__1::vector<v8::internal::compiler::BasicBlock*, v8::internal::ZoneAllocator<v8::internal::compiler::BasicBlock*> >::__append(unsigned long) [node]
 6: 0x7a785d92456 v8::internal::compiler::Schedule::PlanNode(v8::internal::compiler::BasicBlock*, v8::internal::compiler::Node*) [node]
 7: 0x7a785d9b920 v8::internal::compiler::ScheduleLateNodeVisitor::ScheduleNode(v8::internal::compiler::BasicBlock*, v8::internal::compiler::Node*) [node]
 8: 0x7a785d9ac42 v8::internal::compiler::ScheduleLateNodeVisitor::ProcessQueue(v8::internal::compiler::Node*) [node]
 9: 0x7a785d9596b v8::internal::compiler::Scheduler::ScheduleLate() [node]
10: 0x7a785d945b7 v8::internal::compiler::Scheduler::ComputeSchedule(v8::internal::Zone*, v8::internal::compiler::Graph*, v8::base::Flags<v8::internal::compiler::Scheduler::Flag, int>, v8::internal::TickCounter*) [node]
11: 0x7a785caaf2a void v8::internal::compiler::PipelineImpl::Run<v8::internal::compiler::ComputeSchedulePhase>() [node]
12: 0x7a785ca418c v8::internal::compiler::PipelineImpl::ComputeScheduledGraph() [node]
13: 0x7a785ca1f32 v8::internal::compiler::PipelineImpl::OptimizeGraph(v8::internal::compiler::Linkage*) [node]
14: 0x7a785ca195f v8::internal::compiler::PipelineCompilationJob::ExecuteJobImpl() [node]
15: 0x7a784ff6121 v8::internal::OptimizedCompilationJob::ExecuteJob() [node]
16: 0x7a7850208ee v8::internal::OptimizingCompileDispatcher::CompileNext(v8::internal::OptimizedCompilationJob*) [node]
17: 0x7a785022201 v8::internal::OptimizingCompileDispatcher::CompileTask::RunInternal() [node]
18: 0x7a784e440c0 node::WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int) [node]
19: 0x7a9d64a5f51 pthread_create [/usr/lib/libpthread.so.26.1]
gmake: *** [Makefile:668: public/js/index.js] Illegal instruction (core dumped)
*** Error 2 in /var/www/sites/scqr.net/gitea (BSDmakefile:46 'FRC': "gmake" "--no-print-directory" generate build )
```