Skip to content

Fix memory leaks in Promise whenAll#1016

Merged
designatednerd merged 2 commits intoapollographql:masterfrom
AnthonyMDev:whenAll-memory-leaks
Feb 13, 2020
Merged

Fix memory leaks in Promise whenAll#1016
designatednerd merged 2 commits intoapollographql:masterfrom
AnthonyMDev:whenAll-memory-leaks

Conversation

@AnthonyMDev
Copy link
Copy Markdown
Contributor

When any of the promises reject, we were calling reject, but never leaving the DispatchGroup. This means the notify block was never being scheduled. This is what we wanted, but the unintended consequence of that is that the notify block was then retained indefinitely, and the group.notify block is retaining the promises or the resultOrPromises. This way, the notify block gets called and releases it's reference to the promises, but if any rejects occured, it does not call fulfill.

@designatednerd I was seeing memory leaks in my application and was able to track it down to this. I wrote a unit test for Promise, but unit testing memory leaks of value semantic entities is impossible, so I can't provide a unit test for ResultOrPromise. I tested the fix with my app, and the memory leaks are gone now.

Copy link
Copy Markdown
Contributor

@designatednerd designatednerd left a comment

Choose a reason for hiding this comment

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

One question but THANK YOU for looking into this.

waitForExpectations(timeout: 1)

XCTAssertNil(weakPromise0)
XCTAssertNil(weakPromise1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So before this fix, this particular assert would fail, correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, all three of the asserts were failing. The notify block references the entire array of promises, so all of them are retained.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

oof, ouch. well this is definitely an improvement.

@designatednerd designatednerd added this to the 0.23.0 milestone Feb 13, 2020
@designatednerd designatednerd merged commit 802624e into apollographql:master Feb 13, 2020
@AnthonyMDev AnthonyMDev deleted the whenAll-memory-leaks branch February 13, 2020 23:06
@AnthonyMDev AnthonyMDev restored the whenAll-memory-leaks branch February 13, 2020 23:06
@AnthonyMDev AnthonyMDev deleted the whenAll-memory-leaks branch March 23, 2021 18:11
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