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: mutationobserver memory leak - use weakmap for bookkeeping #1423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/
import {
ArrayIndexOf,
ArrayReduce,
ArrayPush,
ArrayReduce,
ArraySplice,
create,
defineProperty,
defineProperties,
forEach,
isUndefined,
isNull,
} from '../../shared/language';
Expand All @@ -28,6 +30,8 @@ const {
const wrapperLookupField = '$$lwcObserverCallbackWrapper$$';
const observerLookupField = '$$lwcNodeObservers$$';

const observerToNodesMap: WeakMap<MutationObserver, Array<Node>> = new WeakMap();

/**
* Retarget the mutation record's target value to its shadowRoot
* @param {MutationRecord} originalRecord
Expand Down Expand Up @@ -199,6 +203,21 @@ function PatchedMutationObserver(

function patchedDisconnect(this: MutationObserver): void {
originalDisconnect.call(this);

// Clear the node to observer reference which is a strong references
const observedNodes = observerToNodesMap.get(this);
if (!isUndefined(observedNodes)) {
forEach.call(observedNodes, observedNode => {
const observers = observedNode[observerLookupField];
if (!isUndefined(observers)) {
const index = ArrayIndexOf.call(observers, this);
if (index !== -1) {
ArraySplice.call(observers, index, 1);
}
}
});
observedNodes.length = 0;
}
}

/**
Expand All @@ -216,11 +235,26 @@ function patchedObserve(
if (isUndefined(target[observerLookupField])) {
defineProperty(target, observerLookupField, { value: [] });
}
ArrayPush.call(target[observerLookupField], this);
// Same observer trying to observe the same node
if (ArrayIndexOf.call(target[observerLookupField], this) === -1) {
ArrayPush.call(target[observerLookupField], this);
} // else There is more bookkeeping to do here https://dom.spec.whatwg.org/#dom-mutationobserver-observe Step #7

// If the target is a SyntheticShadowRoot, observe the host since the shadowRoot is an empty documentFragment
if (target instanceof SyntheticShadowRoot) {
target = (target as ShadowRoot).host;
}

// maintain a list of all nodes observed by this observer
if (observerToNodesMap.has(this)) {
const observedNodes = observerToNodesMap.get(this)!;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason we use ArrayPush.call() but not WeakMapHas.call()? I.e. we cache the Array prototype functions but not the WeakMap ones?

if (ArrayIndexOf.call(observedNodes, target) === -1) {
ArrayPush.call(observedNodes, target);
}
} else {
observerToNodesMap.set(this, [target]);
Copy link
Contributor

Choose a reason for hiding this comment

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

set from lang, although we are not doing that today... we should probably start doing it at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its a polyfilled feature in IE11, wasn't sure if was okay to cache these. Let me know.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, it should be ok. they all run before everything else.

Copy link
Contributor

Choose a reason for hiding this comment

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

lets open a seprate issue for this in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

return originalObserve.call(this, target, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,62 @@ describe('MutationObserver is synthetic shadow dom aware.', () => {
});
});
});
if (!process.env.NATIVE_SHADOW) {
describe('References to mutation observers are not leaked', () => {
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
it('should not leak after disconnect', () => {
const node = document.createElement('div');
container.appendChild(node);
const observer = new MutationObserver(() => {});
observer.observe(node, observerConfig);
expect(node.$$lwcNodeObservers$$.length).toBe(1);
observer.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(0);
});

it('should not leak after disconnect - multiple nodes', () => {
const node1 = document.createElement('div');
const node2 = document.createElement('div');
container.appendChild(node1);
container.appendChild(node2);
const observer = new MutationObserver(() => {});
observer.observe(node1, observerConfig);
observer.observe(node2, observerConfig);
expect(node1.$$lwcNodeObservers$$.length).toBe(1);
expect(node2.$$lwcNodeObservers$$.length).toBe(1);
observer.disconnect();
expect(node1.$$lwcNodeObservers$$.length).toBe(0);
expect(node2.$$lwcNodeObservers$$.length).toBe(0);
});

it('should not leak after disconnect - multiple observers', () => {
const node = document.createElement('div');
container.appendChild(node);
const observer1 = new MutationObserver(() => {});
const observer2 = new MutationObserver(() => {});
observer1.observe(node, observerConfig);
expect(node.$$lwcNodeObservers$$.length).toBe(1);
observer2.observe(node, observerConfig);
expect(node.$$lwcNodeObservers$$.length).toBe(2);
observer1.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(1);
observer2.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(0);
});

it('should not leak after disconnect - duplicate observe()s', () => {
const node = document.createElement('div');
container.appendChild(node);
const observer = new MutationObserver(() => {});
observer.observe(node, observerConfig);
observer.observe(node, observerConfig);
observer.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(0);
});
});
}
});