Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in Chrome - Error objects do not like to be persistent #9108

Merged
merged 2 commits into from
Jul 30, 2019

Conversation

juj
Copy link
Collaborator

@juj juj commented Jul 29, 2019

Fix memory leak in Chrome - Error objects do not like to be persistent, they seem to heavily capture variables they have in their scope.

In Chrome, calling var x = new Error(); in global scope causes an immediate capture of the scope, which leads in Module being captured, along with .data, .wasm, .mem etc. raw byte arrays, never reclaiming the memory.

I don't think there's any pressing reason for ExitStatus to inherit from Error anyways? (was it due to convenience to get a pretty-printed error callstack?)

…t, they seem to heavily capture variables they have in their scope.
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a mention of this in the Changelog, as it is user-observable in theory.

Yeah, I think this was intended to be nicer to debug with, but I'm really not sure how specifically it helps. If it was important there would have been a test with it :)

@VirtualTim
Copy link
Collaborator

Do you think this is related to #8571, or the linked Chrome bugs?

@juj
Copy link
Collaborator Author

juj commented Jul 30, 2019

It is possible, although it's hard to say without profiling - definitely worth a retest after this lands.

@juj juj merged commit 84a2f01 into emscripten-core:incoming Jul 30, 2019
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
emscripten-core#9108)

* Fix memory leak in Chrome - Error objects do not like to be persistent, they seem to heavily capture variables they have in their scope.

* Add comment in ChangeLog
adamziel added a commit to WordPress/wordpress-playground that referenced this pull request May 30, 2023
…race attached

Overrides Emscripten's default ExitStatus object which gets
thrown on failure. Unfortunately, the default object is not
a subclass of Error and does not provide any stack trace.

This is a deliberate behavior on Emscripten's end to prevent
memory leaks after the program exits. See:

emscripten-core/emscripten#9108

In case of WordPress Playground, the worker in which the PHP
runs will typically exit after the PHP program finishes, so
we don't have to worry about memory leaks.

As for assigning to a previously undeclared ExitStatus variable here,
the Emscripten module declares `ExitStatus` as `function ExitStatus`
which means it gets hoisted to the top of the scope and can be
reassigned here – before the actual declaration is reached.

If that sounds weird, try this example:

```js
ExitStatus = () => { console.log("reassigned"); }
function ExitStatus() {}
ExitStatus();
// logs "reassigned"
```
adamziel added a commit to WordPress/wordpress-playground that referenced this pull request May 30, 2023
…race attached (#470)

## Description

Overrides Emscripten's default ExitStatus object which gets thrown on
failure. Unfortunately, the default object is not a subclass of Error
and does not provide any stack trace.

This is a deliberate behavior on Emscripten's end to prevent memory
leaks after the program exits. See:

emscripten-core/emscripten#9108

In case of WordPress Playground, the worker in which the PHP runs will
typically exit after the PHP program finishes, so we don't have to worry
about memory leaks.

As for assigning to a previously undeclared ExitStatus variable here,
the Emscripten module declares `ExitStatus` as `function ExitStatus`
which means it gets hoisted to the top of the scope and can be
reassigned here – before the actual declaration is reached.

If that sounds weird, try this example:

```js
ExitStatus = () => { console.log("reassigned"); }
function ExitStatus() {}
ExitStatus();
// logs "reassigned"
```

## Testing instructions

Confirm the CI tests passed

Related: #416
cc @wojtekn
Pookie717 added a commit to Pookie717/wordpress-playground that referenced this pull request Oct 1, 2023
…race attached (#470)

## Description

Overrides Emscripten's default ExitStatus object which gets thrown on
failure. Unfortunately, the default object is not a subclass of Error
and does not provide any stack trace.

This is a deliberate behavior on Emscripten's end to prevent memory
leaks after the program exits. See:

emscripten-core/emscripten#9108

In case of WordPress Playground, the worker in which the PHP runs will
typically exit after the PHP program finishes, so we don't have to worry
about memory leaks.

As for assigning to a previously undeclared ExitStatus variable here,
the Emscripten module declares `ExitStatus` as `function ExitStatus`
which means it gets hoisted to the top of the scope and can be
reassigned here – before the actual declaration is reached.

If that sounds weird, try this example:

```js
ExitStatus = () => { console.log("reassigned"); }
function ExitStatus() {}
ExitStatus();
// logs "reassigned"
```

## Testing instructions

Confirm the CI tests passed

Related: WordPress/wordpress-playground#416
cc @wojtekn
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.

3 participants