Skip to content

Conversation

srivats22
Copy link

This PR Fixes: flutter/flutter#167410, where _initCalled was being performed twice on the web

Based on the discussion comments I have removed the calles to _initCalled in the google_sign_in_web package

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

@srivats22 srivats22 requested a review from ditman as a code owner July 29, 2025 15:15
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request removes the _initCalled completer, which introduces a race condition. The init method should be made idempotent to handle being called multiple times. The CHANGELOG.md entry should be a complete sentence ending with a period.

@stuartmorgan-g stuartmorgan-g self-requested a review July 29, 2025 18:23
@srivats22
Copy link
Author

srivats22 commented Jul 29, 2025

Hi,

any idea or pointers on how to fix this:

The following TestFailure was thrown running a test (but after
the test had completed):
Expected: throws <Instance of 'StateError'>
  Actual: <Closure: () => Future<Null> from: () => {
                            let t$goto = 0, t$completer =
async._makeAsyncAwaitCompleter(T.Null());
                            var t$36asyncBody =
async._wrapJsFunctionForAsync((t$errorCode, t$result) => {
                              if (t$errorCode === 1) return
async._asyncRethrow(t$result, t$completer);
                              while (true)
                                switch (t$goto) {
                                  case 0:
                                    // Function start
                                    t$goto = 2;
                                    return
async._asyncAwait(t$36$35plugin$35get().disconnect(C[8] ||
CT.C8), t$36asyncBody, t$completer);
                                  case 2:
                                    // returning from await.
                                    // implicit return
                                    return
async._asyncReturn(null, t$completer);
                                }
                            });
                            return
async._asyncStartSync(t$36asyncBody, t$completer);
                          }>
   Which: returned a Future that emitted <null>

When the exception was thrown, this was the stack:

its failing for the same reason in 3 of the tests and the repo check I know the issue which I will fix...

@stuartmorgan-g
Copy link
Collaborator

I'm not sure I understand the question. The expectations that check that calling a method without calling init throws a StateError are failing because you removed the code that throws the StateError if a method is called without calling init.

@srivats22
Copy link
Author

Oh so the changes in the PR is incorrect? Or something else needs to change?

@stuartmorgan-g
Copy link
Collaborator

Tests that expect that the web implementation asserts init completion everywhere are no longer valid if the web implementation no longer asserts init completion. Intentional behavioral changes often require changing tests.

@srivats22
Copy link
Author

Understood let me look at the code and see where the initCompleted is still be called in the test and remove those... Last I checked wasn't able to find any

@stuartmorgan-g
Copy link
Collaborator

From triage: Is this ready for review?

@srivats22
Copy link
Author

Yes its ready for review looks like I might need to fix the conflicts which I will get to

@stuartmorgan-g stuartmorgan-g added the triage-web Should be looked at in web triage label Aug 19, 2025
@mdebbar mdebbar requested review from mdebbar and removed request for ditman August 20, 2025 18:15
Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

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

Thanks for contributing this fix!

Some thoughts:

When the app calls init() twice (potentially with different params), it becomes a tricky situation. We already have the first init() in flight while the second init() is being executed.

I think we should let both init() calls continue to completion. Any calls to await initialized; should await the latest init() call.

init(InitParameters(clientId: 111));
init(InitParameters(clientId: 222));
await initialized;
// At this point, the plugin should be ready with clientId 222.

@stuartmorgan-g what do other platforms do in this case?

return Future.wait<void>(<Future<void>>[
_jsSdkLoadedFuture,
_initCalled!.future,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is the right fix.

There's a chance _jsSdkLoadedFuture completes and app code starts using the plugin before _gisSdkClient has been set (in init()).

return Future.wait<void>(<Future<void>>[
_jsSdkLoadedFuture,
_initCalled!.future,
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This can become:

Future<void> get initialized {
  _assertIsInitCalled();
  return _initCalled!.future; // `init()` already awaits `_jsSdkLoadedFuture` so there's no need to wait for it here.
}

'serverClientId is not supported on Web.',
);

_initCalled = Completer<void>();
Copy link
Contributor

Choose a reason for hiding this comment

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

This may work by putting the completer in a local variable:

final initCalled = _initCalled = Completer<void>();

and later:

initCalled.complete(); // Use the local completer, not `_initCalled`

@srivats22
Copy link
Author

I made the changes based on what was put in the issue... What I got from it was since the logic has been moved outside the package the code for init seems redundant... I might have miss understood let me take a look based on the comments and try and incorporate it

@stuartmorgan-g
Copy link
Collaborator

stuartmorgan-g commented Aug 21, 2025

@stuartmorgan-g what do other platforms do in this case?

Nothing; in the new version of google_sign_in, clients explicitly call an initialize method (rather than it being an internal, implicit thing on most platforms, but semi-exposed on web, as was the case before), and the docs say they must do so exactly once. It's a programming error by the client to write the code you've shown there.

If we think clients need explicit Errors when doing the wrong thing, that should be added to the app-facing package, rather than handled in each implementation. This code is a legacy of the exposed-for-web nature of the previous init code.

(Also, clients should basically never call the platform interface methods directly. That's not how most federated plugins are designed.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: google_sign_in platform-web triage-web Should be looked at in web triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[google_sign_in_web] _initCalled completed twice
3 participants