Skip to content

[multicast_dns] Fix start returning early while another start is in progress - #55

Open
rabiee-nasri wants to merge 1 commit into
flutter:mainfrom
rabiee-nasri:multicast-dns-concurrent-start
Open

rabiee-nasri wants to merge 1 commit into
flutter:mainfrom
rabiee-nasri:multicast-dns-concurrent-start

Conversation

@rabiee-nasri

Copy link
Copy Markdown

MDnsClient.start returned immediately when another call to start was still in progress. A caller could await client.start() and then get Bad state: mDNS client must be started before calling lookup. from lookup, because the client was not started yet.

A failed start (for example when the socket cannot be bound) also left the client marked as starting, so every later call to start did nothing.

Now the start that is in progress is kept as a future. Calls made while the client is starting return that future, and it is cleared when the start completes or fails, so a failed start can be retried. Calls made after the client is started are still ignored, as documented.

Fixes flutter/flutter#177246

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

@google-cla

google-cla Bot commented Nov 17, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rabiee-nasri
rabiee-nasri marked this pull request as ready for review November 17, 2026 18:52

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates MDnsClient in multicast_dns to handle concurrent and failed initialization attempts by changing the _starting flag from a boolean to a Future<void>? and extracting the initialization logic into a private _start method. It also adds unit tests to verify these behaviors. The review feedback identifies an issue in the stop method where checking _started before _starting causes the method to return early instead of throwing a StateError when called during startup, and provides a suggestion to swap the order of these checks.

Comment on lines 183 to 189
void stop() {
if (!_started) {
return;
}
if (_starting) {
if (_starting != null) {
throw StateError('Cannot stop mDNS client while it is starting.');
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In the current implementation of stop(), the check if (!_started) is performed before if (_starting != null). Since _started is only set to true at the very end of the start process, _started is false while the client is starting. This means calling stop() while the client is starting will return early and silently ignore the call, rather than throwing the expected StateError as intended. Swapping the order of these checks ensures that attempting to stop the client while it is starting correctly throws a StateError.

Suggested change
void stop() {
if (!_started) {
return;
}
if (_starting) {
if (_starting != null) {
throw StateError('Cannot stop mDNS client while it is starting.');
}
void stop() {
if (_starting != null) {
throw StateError('Cannot stop mDNS client while it is starting.');
}
if (!_started) {
return;
}

@rabiee-nasri

Copy link
Copy Markdown
Author

@googlebot I signed it!

…rogress

A second call to MDnsClient.start made while the first was still running returned at once, so a caller could await start and then get a StateError from lookup. A failed start also left the client marked as starting, so later calls did nothing. Share the in-progress start between callers and clear it when it completes, so a failed start can be retried.
@rabiee-nasri
rabiee-nasri force-pushed the multicast-dns-concurrent-start branch from 114c68c to 8c290eb Compare November 17, 2026 19:06
@Piinks
Piinks requested a review from vashworth November 17, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[multicast_dns] Calls to start return immediately if client is already starting

1 participant