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

[pigeon] Fix async handling in C++ generator #3040

Merged
merged 5 commits into from
Jan 12, 2023

Conversation

stuartmorgan
Copy link
Contributor

@stuartmorgan stuartmorgan commented Jan 11, 2023

Reworks the structure of success and return value handling in the C++ generator in order to simplify the flows and eliminate undefined behavior due to references to stack-based objects in a long-lived lamba capture.

Core changes:

  • wrapped is no longer created at the beginning of the method, populated, and then returned at the end, since that made async hard to do correctly. Instead, each distinct case that would return has its own call to reply.
    • While this does mean that there are now multiple calls to reply, which can be a source of multi-reply errors (e.g., if a return afterward is forgotten), in practice those cases would have been errors anyway because wrapped would have had the wrong number of elements.
    • This eliminated &wrapped, which had undefined behavior for any plugin code that actually called reply asynchronously.
  • reply is now passed as a copy, rather than a reference, again to avoid undefined behavior for actually async methods.

Because this if fixing undefined behavior, this is tested with generator unit tests, as integration tests may or may not pass. In fact we have one test running right now, and it's luck that it's been passing. In practice most of the real testing will come from adding more async tests, which will happen in another PR that I have in progress (which I had to pause because the new integration tests crashed, which is how I ended up in this PR).

Minor change:

  • Now that we don't need to conform to wrapped's type, WrapError returns an EncodableValue to simplify its usage.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

Copy link
Contributor

@tarrinneal tarrinneal left a comment

Choose a reason for hiding this comment

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

This all looks good to me. I'll let you decide if you want to merge it now or wait for #3037

@stuartmorgan
Copy link
Contributor Author

Since this fixes a very serious bug, including undefined behavior in a test we're currently running in CI, I'm going to go ahead and land it now. Since the changes here are pretty contained I think it's probably easier to apply this into your PR rather than the other way (unlike my other in-progress change); I can do that tomorrow if you want.

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 12, 2023
@auto-submit auto-submit bot merged commit 56c6ce5 into flutter:main Jan 12, 2023
@stuartmorgan stuartmorgan deleted the pigeon-cpp-async-fixes branch January 12, 2023 03:37
Maatteogekko pushed a commit to Maatteogekko/packages that referenced this pull request Feb 4, 2023
* Add unit test

* Switch all errors to direct return

* Make 'wrapped' entirely local

* Switch reply to copy instead of reference

* Version bump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: pigeon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants