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

Prevent memory leak of PerformanceObserver #24074

Open
gterzian opened this issue Aug 28, 2019 · 4 comments
Open

Prevent memory leak of PerformanceObserver #24074

gterzian opened this issue Aug 28, 2019 · 4 comments
Labels
A-content/script Related to the script thread

Comments

@gterzian
Copy link
Member

#24072 fixes the leak, but the underlying reason is still not clear.

It could be that if we didn't clear the performance list, then it ended up getting entries added to it but never removed, so if you quickly navigated away from the page it just sat there being a memory leak?

Originally posted by @asajeffrey in #24072 (comment)

See also other comments in that PR.

@gterzian gterzian changed the title Figure out why Performane would result in leaking Window after a pipeline was closed Figure out why keeping Performance would result in leaking Window after a pipeline was closed Aug 28, 2019
@gterzian gterzian added the A-content/script Related to the script thread label Aug 28, 2019
@gterzian
Copy link
Member Author

gterzian commented Aug 29, 2019

The theory of the Performance takes being kept in the task queue seems plausible.

cc @asajeffrey @jdm I think it is plausible, and also that it's not what happened in this specific case, and that instead we're leaking PerformanceObserver since they are never removed from Performance(which itself wasn't set to None either), unless script calls

fn Disconnect(&self) {

We could look into changing the structure of how observers are stored, so that they are only kept alive by their corresponding PerformanceObserverCallback in script(so they would drop if script unsets the callback, I think).

I need to look more into it.

It could look something like: we could separate the PerformanceObserver DOM object, from a PerformanceObserverImpl, with the latter keeping track of things like DOMPerformanceEntryList and other "data", whereas the DOM object would just be a wrapper around the callback.

Then the global could keep a strong ref to the Impl, and a Weak<PerformanceObserver> that would drop if the script callback is set to None.

Then the global could run a periodic checkpoint like is done for messageports at https://github.com/servo/servo/pull/23637/files#diff-59d233642d0ce6d687484bdd009e1017R484

@gterzian
Copy link
Member Author

On the other hand, I'm not actually sure the code at https://gist.github.com/kanaka/119f5ed9841e23e35d07e8944cca6aa7 creates any observers, which could invalidate the above logic(at least with regards to the leaking perceived when running that script). Will require looking into more closely.

@gterzian
Copy link
Member Author

gterzian commented Aug 29, 2019

Ah ok, even if there are no observers set, we still often buffer the entry at

if add_to_performance_entries_buffer {

And this happens for instance each time script does a fetch:

.queue_entry(performance_entry.upcast::<PerformanceEntry>(), true);

used for example by the stylesheet loader(used in the debug script that was leaking docs):

network_listener::submit_timing(self)

Itself called by fetch at

listener.submit_resource_timing();

@gterzian
Copy link
Member Author

gterzian commented Aug 29, 2019

Ok so this #24109 fixes the actual cause of the leak, and removes the previous changes that set Performance as a whole to None.

The cause of the perceived leak was indeed the buffer, as no performance observers were created.

Leaving this one open, since I think we should still fix the issue with the performance observers, which leak unless script calls Disconnect on them. See #24074 (comment), the fix for that would be finding a way to store the observers while allowing them to drop when script isn't keeping a reference to their callback.

@gterzian gterzian changed the title Figure out why keeping Performance would result in leaking Window after a pipeline was closed Prevent memory leak of PerformanceObserver Aug 30, 2019
bors-servo pushed a commit that referenced this issue Sep 3, 2019
…asajeffrey

Performance: limit buffer size, clear on pipeline exit

<!-- Please describe your changes on the following line: -->

Part of #24074

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24109)
<!-- Reviewable:end -->
bors-servo pushed a commit that referenced this issue Sep 3, 2019
…<try>

Performance: limit buffer size, clear on pipeline exit

<!-- Please describe your changes on the following line: -->

Part of #24074

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24109)
<!-- Reviewable:end -->
bors-servo pushed a commit that referenced this issue Sep 4, 2019
…<try>

Performance: limit buffer size, clear on pipeline exit

<!-- Please describe your changes on the following line: -->

Part of #24074

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24109)
<!-- Reviewable:end -->
bors-servo pushed a commit that referenced this issue Sep 4, 2019
…asajeffrey

Performance: limit buffer size, clear on pipeline exit

<!-- Please describe your changes on the following line: -->

Part of #24074

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24109)
<!-- Reviewable:end -->
bors-servo pushed a commit that referenced this issue Sep 5, 2019
…asajeffrey

Performance: limit buffer size, clear on pipeline exit

<!-- Please describe your changes on the following line: -->

Part of #24074

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24109)
<!-- Reviewable:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-content/script Related to the script thread
Projects
None yet
Development

No branches or pull requests

1 participant