Automattic / Automattic/node-canvas

FATAL ERROR: v8::ArrayBuffer::New Allocation failed - process out of memory

Open
#2,314 9 comments 4 reactions 0 assignees View on GitHub
Bug
Dominant language
JavaScript
Stars
10.7k
Forks
1.2k
Avg merge
4d 8h
Merged PRs (30d)
1

Description

## Issue
I use pdf.js and node-canvas to render pdf pages to images. Most of the time that work very well. But now I have an example witch triggeres "FATAL ERROR: v8::ArrayBuffer::New Allocation failed - process out of memory". My guess is that this is a bug in node-canvas cause when I use the same PDF with pdf.js within the Browser its getting rendered without an issue.

## Steps to Reproduce
* Create empty folder
* npm init
* npm install canvas
* npm install pdfjs-dist
* add package.json `"type": "module"`
* add the following example main.mjs
* download the attach pdf and put it into the package folder

main.mjs
```javascript
import Canvas from "canvas";
import * as pdfjsLib from "pdfjs-dist/legacy/build/pdf.mjs";
import fs from "fs";

const data = fs.readFileSync("./fbc6f45f18d16464611606a5e1bc845dac3313c8.pdf");

async function run() {
const pdfDocument = await pdfjsLib.getDocument({data: new Uint8Array(data)}).promise;
const page = await pdfDocument.getPage(1);
const viewport = page.getViewport({ scale: 1 });
const canvas = Canvas.createCanvas(500, 1000);
const context = canvas.getContext("2d");
await page.render({ canvasContext: context, viewport: viewport }).promise;
}
await run();
```

[fbc6f45f18d16464611606a5e1bc845dac3313c8.pdf](https://github.com/Automattic/node-canvas/files/13511609/fbc6f45f18d16464611606a5e1bc845dac3313c8.pdf)

Currently this code crashes the node process with the following error:

```text
<--- Last few GCs --->

[35416:0000017159A0F250] 1398 ms: Mark-Compact (reduce) 18.2 (36.5) -> 18.2 (20.2) MB, 8.11 / 0.00 ms (average mu = 0.944, current mu = 0.002) external memory pressure; GC in old space requested
[35416:0000017159A0F250] 1406 ms: Mark-Compact (reduce) 18.2 (20.2) -> 18.2 (20.0) MB, 8.24 / 0.00 ms (average mu = 0.886, current mu = 0.004) external memory pressure; GC in old space requested

<--- JS stacktrace --->

FATAL ERROR: v8::ArrayBuffer::New Allocation failed - process out of memory
1: 00007FF76E86BCDF node_api_throw_syntax_error+203199
2: 00007FF76E7E8226 v8::base::CPU::num_virtual_address_bits+74518
3: 00007FF76E7EA041 v8::base::CPU::num_virtual_address_bits+82225
4: 00007FF76F252421 v8::Isolate::ReportExternalAllocationLimitReached+65
5: 00007FF76F23BBB8 v8::Function::Experimental_IsNopFunction+1336
6: 00007FF76F249F66 v8::ArrayBuffer::New+118
7: 00007FF97586996B Canvas::getHeight+85323
8: 00007FF97585431C Backend::getHeight+1532
9: 00007FF76F2087FE v8::SharedValueConveyor::SharedValueConveyor+416270
10: 00007FF76F2083FA v8::SharedValueConveyor::SharedValueConveyor+415242
11: 00007FF76F2086BF v8::SharedValueConveyor::SharedValueConveyor+415951
12: 00007FF76F208530 v8::SharedValueConveyor::SharedValueConveyor+415552
13: 00007FF76F302F5E v8::PropertyDescriptor::writable+674286
14: 00007FF76F274600 v8::PropertyDescriptor::writable+90256
15: 00007FF76F274600 v8::PropertyDescriptor::writable+90256
16: 00007FF6EF539838
```

## Your Environment
* node v20.5.1
* Windows 11 + Linux within Docker
* canvas 2.11.2
* pdfjs-dist 4.0.269

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.