Skip to content

Clear image.src on loadImage error to release partial cairo state#2580

Open
iurisilvio wants to merge 1 commit into
Automattic:masterfrom
iurisilvio:fix/loadImage-clear-src-on-error
Open

Clear image.src on loadImage error to release partial cairo state#2580
iurisilvio wants to merge 1 commit into
Automattic:masterfrom
iurisilvio:fix/loadImage-clear-src-on-error

Conversation

@iurisilvio
Copy link
Copy Markdown

Fixes #2576.

loadImage() in index.js rejects the Promise on error but doesn't clear image.src. The Image keeps a reference to the input buffer (and, depending on the format, any partial cairo surface) until V8 garbage-collects the Image wrapper. Under sustained load on malformed inputs that fail mid-decode, this delays cleanup arbitrarily.

The fix assigns image.src = Buffer.alloc(0) before rejecting, which triggers Image::SetSource()clearData() synchronously — destroying any partial cairo surface and resetting the buffer reference.

One-line change; the full rationale and a standalone repro are in #2576.

loadImage's onerror handler rejects the Promise but leaves image.src
pointing at the input buffer. When libjpeg/libpng allocated a cairo
surface before failing mid-decode, that surface stays attached to the
Image until V8 GC — under sustained load on malformed inputs this
delays cleanup arbitrarily.

Assign Buffer.alloc(0) before reject so clearData() runs synchronously.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

loadImage() doesn't clear image.src on error, delaying cleanup of partial cairo state

1 participant