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

[21672] Fix data race in TypeObjectFactory::get_instance (backport #5238) #5241

Merged
merged 2 commits into from
Sep 17, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Sep 17, 2024

Description

While debugging an error reported by LeakSanitizer in #4916, an important data race was discovered in TypeObjectFactory::get_instance.

The test that PR adds is creating several writers from different threads simultaneously.

This leads to calling TypeObjectFactory::get_instance when registering the local writer (during the creation of its WriterProxyData).

The data race made several factories to be constructed, but only one was assigned to the global g_instance pointer.

Only one of the factories was destroyed when the process finished, making LeakSanitizer complain.

This PR adds a mechanism based on an atomic enumeration holding the state of the singleton instance.

@Mergifyio backport 2.14.x

Contributor Checklist

  • Commit messages follow the project guidelines.

  • The code follows the style guidelines of this project.

  • Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been added; the added tests pass locally

  • N/A: Any new/modified methods have been properly documented using Doxygen.

  • N/A: Any new configuration API has an equivalent XML API (with the corresponding XSD extension)

  • Changes are backport compatible: they do NOT break ABI nor change library core behavior.

  • Changes are API compatible.

  • N/A: New feature has been added to the versions.md file (if applicable).

  • N/A: New feature has been documented/Current behavior is correctly described in the documentation.

  • [x]: Applicable backports have been included in the description.

    • This was discovered in 2.10.x, so this PR targets that branch.
    • It is also present in 2.14.x, so this PR can be directly forward ported to that branch. Marked so above.
    • For 3.x, the interfaces have changed, and the implementation is based on a static shared_ptr, to it is enough to add a test similar to the one in this PR.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • If this is a critical bug fix, backports to the critical-only supported branches have been requested.
  • Check CI results: changes do not issue any warning.
  • Check CI results: failing tests are unrelated with the changes.

This is an automatic backport of pull request #5238 done by [Mergify](https://mergify.com).

* Refs #21664. Regression test.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Improve synchronization in regression test.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Return created instance to visualize data-race and make test fail.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Just count the number of different instances.
This way we have a single final expectation.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Avoid using g_instance inside the instance.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Inject factory in all methods called inside `register_builtin_annotations_types`.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Use atomic enumeration to control the instance state.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21664. Uncrustify.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21672. Notify condition from main thread.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21672. FIx EOL at end of file.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21672. Refactor to create builtin objects inside factory constructor.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

* Refs #21672. Uncrustify.

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

---------

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>
(cherry picked from commit 8f4b4a5)

# Conflicts:
#	src/cpp/dynamic-types/TypeObjectFactory.cpp
@mergify mergify bot added the conflicts Backport PR wich git cherry pick failed label Sep 17, 2024
Copy link
Contributor Author

mergify bot commented Sep 17, 2024

Cherry-pick of 8f4b4a5 has failed:

On branch mergify/bp/2.14.x/pr-5238
Your branch is up to date with 'origin/2.14.x'.

You are currently cherry-picking commit 8f4b4a583.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   include/fastrtps/types/BuiltinAnnotationsTypeObject.h
	modified:   include/fastrtps/types/TypeObjectFactory.h
	modified:   src/cpp/dynamic-types/BuiltinAnnotationsTypeObject.cpp
	modified:   test/unittest/xtypes/XTypesTests.cpp

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/cpp/dynamic-types/TypeObjectFactory.cpp

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@MiguelCompany MiguelCompany added this to the v2.14.4 milestone Sep 17, 2024
Signed-off-by: Miguel Company <miguelcompany@eprosima.com>
@MiguelCompany MiguelCompany removed the conflicts Backport PR wich git cherry pick failed label Sep 17, 2024
@github-actions github-actions bot added the ci-pending PR which CI is running label Sep 17, 2024
@MiguelCompany MiguelCompany merged commit 1970c71 into 2.14.x Sep 17, 2024
18 checks passed
@MiguelCompany MiguelCompany deleted the mergify/bp/2.14.x/pr-5238 branch September 17, 2024 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci-pending PR which CI is running
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant