-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix(sql_connect): queries are not single instanced for execute #18500
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -359,6 +359,7 @@ class QueryRef<Data, Variables> extends OperationRef<Data, Variables> { | |
| _serverStreamSubscription?.cancel(); | ||
| _serverStreamSubscription = null; | ||
| _serverStream = null; | ||
| _streamController = null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Broadcast In Please update final streamController =
StreamController<QueryResult<Data, Variables>>.broadcast(
onCancel: () {
trackedQueries.remove(queryId); // Or keep it if using WeakReference
ref._onAllSubscribersCancelled();
streamController.close(); // Close the controller
},
); |
||
| } | ||
|
|
||
| Stream<QueryResult<Data, Variables>> subscribe() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -144,7 +144,7 @@ class FirebaseDataConnect extends FirebasePlugin { | |||||||||
| if (ref != null) { | ||||||||||
| return ref; | ||||||||||
| } else { | ||||||||||
| return QueryRef<Data, Variables>( | ||||||||||
| final newRef = QueryRef<Data, Variables>( | ||||||||||
| this, | ||||||||||
| operationName, | ||||||||||
| transport!, | ||||||||||
|
|
@@ -153,6 +153,8 @@ class FirebaseDataConnect extends FirebasePlugin { | |||||||||
| varsSerializer, | ||||||||||
| vars, | ||||||||||
| ); | ||||||||||
| _queryManager.trackedQueries[queryId] = newRef; | ||||||||||
| return newRef; | ||||||||||
|
Comment on lines
+156
to
+157
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memory leak due to untracked Adding To fix this, we should use Note: You will also need to update the lookup at lines 142-143 to retrieve the target from the final weakRef = _queryManager.trackedQueries[queryId];
QueryRef<Data, Variables>? ref =
(weakRef as WeakReference<QueryRef<Data, Variables>>?)?.target;
Suggested change
|
||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -254,5 +254,38 @@ void main() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(routingTransport.websocket.auth, equals(mockAuth)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(routingTransport.websocket.appCheck, equals(mockAppCheck)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('query returns identical QueryRef instance for identical queries', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final dynamicApp = DynamicMockFirebaseApp( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: 'queryRefAppName', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options: const FirebaseOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apiKey: 'fake_api_key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| appId: 'fake_app_id', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messagingSenderId: 'fake_messaging_sender_id', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectId: 'fake_project_id', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final instance = FirebaseDataConnect( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app: dynamicApp, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connectorConfig: mockConnectorConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final ref1 = instance.query( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'listMovies', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (json) => json, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| emptySerializer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final ref2 = instance.query( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'listMovies', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (json) => json, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| emptySerializer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(identical(ref1, ref2), isTrue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is a reproduction test case that verifies the Without the You can append this test to the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent
QueryRefidentity and potential duplicate server streams.When all subscribers to a query cancel, the
QueryRefis currently removed fromtrackedQueries(via the stream controller'sonCancel). Ifquery()is subsequently called again for the same operation, a newQueryRefinstance will be created. If the developer still holds the oldQueryRefand resubscribes to it, both the old and new instances will be active, leading to multiple active server streams for the same logical query.To fix this and maintain a single canonical instance,
QueryManagershould useWeakReferenceto track queries, allowing them to be reused if they still exist in memory, while avoiding leaks.Please apply the following changes to
QueryManager(not shown in this diff):trackedQueriestype to useWeakReference:addQueryto storeWeakReferenceand NOT remove it on cancel:QueryRef.subscribe()to storeWeakReference: