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

Revert #7276, but test garbage collection of ObservableQuery. #7695

Merged
merged 2 commits into from
Feb 12, 2021
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Apollo Client 3.3.10 (not yet released)

### Bug fixes

- Revert PR [#7276](https://github.com/apollographql/apollo-client/pull/7276), but test that garbage collection reclaims torn-down `ObservableQuery` objects. <br/>
[@benjamn](https://github.com/benjamn) in [#7695](https://github.com/apollographql/apollo-client/pull/7695)

### Improvements

- Avoid calling `forceUpdate` when component is unmounted. <br/>
Expand Down
69 changes: 65 additions & 4 deletions scripts/memory/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,29 @@ function makeRegistry(callback, reject) {

describe("garbage collection", () => {
itAsync("should collect client.cache after client.stop()", (resolve, reject) => {
const expectedKeys = new Set([
"client.cache",
"ObservableQuery",
]);

const registry = makeRegistry(key => {
assert.strictEqual(key, "client.cache");
resolve();
if (expectedKeys.delete(key) && !expectedKeys.size) {
resolve();
}
}, reject);

const localVar = makeVar(123);

(function (client) {
registry.register(client.cache, "client.cache");

client.watchQuery({
const obsQuery = client.watchQuery({
query: gql`query { local }`,
}).subscribe({
});

registry.register(obsQuery, "ObservableQuery");

obsQuery.subscribe({
next(result) {
assert.deepStrictEqual(result.data, {
local: 123,
Expand All @@ -84,4 +94,55 @@ describe("garbage collection", () => {
}),
}));
});

itAsync("should collect ObservableQuery after tear-down", (resolve, reject) => {
const expectedKeys = new Set([
"ObservableQuery",
]);

const registry = makeRegistry(key => {
// Referring to client here should keep the client itself alive
// until after the ObservableQuery is (or should have been)
// collected. Collecting the ObservableQuery just because the whole
// client instance was collected is not interesting.
assert.strictEqual(client instanceof ApolloClient, true);

if (expectedKeys.delete(key) && !expectedKeys.size) {
resolve();
}
}, reject);

const localVar = makeVar(123);

const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
local() {
return localVar();
},
},
},
},
}),
});

(function () {
const obsQuery = client.watchQuery({
query: gql`query { local }`,
});

registry.register(obsQuery, "ObservableQuery");

const sub = obsQuery.subscribe({
next(result) {
assert.deepStrictEqual(result.data, {
local: 123,
});
sub.unsubscribe();
},
});
})();
});
});
5 changes: 0 additions & 5 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,6 @@ once, rather than every time you call fetchMore.`);
delete this.reobserver;
}

// Since the user-provided context object can retain arbitrarily large
// amounts of memory, we delete it when the ObservableQuery is torn
// down, to avoid the possibility of memory leaks.
delete this.options.context;

// stop all active GraphQL subscriptions
this.subscriptions.forEach(sub => sub.unsubscribe());
this.subscriptions.clear();
Expand Down