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

Use reader writer lock guard for CRT logger instance pointer access #2552

Merged
merged 1 commit into from
Jul 28, 2023

Conversation

SergeyRyabinin
Copy link
Contributor

@SergeyRyabinin SergeyRyabinin commented Jun 22, 2023

Issue #, if available:

Description of changes:

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (If tests are not applicable, explain.)
  • Checked if this PR is a breaking (APIs have been changed) change.
  • Checked if this PR will not introduce cross-platform inconsistent behavior.
  • Checked if this PR would require a ReadMe/Wiki update.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@@ -35,7 +38,11 @@ static int s_aws_logger_redirect_log(
{
AWS_UNREFERENCED_PARAM(logger);
// use local variable because global variable can be modified in the middle by another thread.
std::shared_ptr<CRTLogSystemInterface> localCRTLogSystem = CRTLogSystem;
std::shared_ptr<CRTLogSystemInterface> localCRTLogSystem;
Copy link
Contributor

Choose a reason for hiding this comment

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

The Reader lock allows this variable to be removed, avoid a copy, and just access CRTLogSystem directly in the four logging methods as long as the shared lock is held onto.

Copy link
Contributor

@jeking3 jeking3 left a comment

Choose a reason for hiding this comment

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

I tested this change with valgrind/drd, which does not understand the ReaderWriterLock protection. It would need DRD annotations (https://valgrind.org/docs/manual/drd-manual.html#drd-manual.effective-use, section 8.2.5) to understand the safety scoping being provided.

I changed a local copy to use std::mutex and it passed through valgrind/drd successfully, whereas without there was a thread safety issue.

@SergeyRyabinin SergeyRyabinin marked this pull request as ready for review July 28, 2023 00:10
@SergeyRyabinin SergeyRyabinin merged commit b7b8c43 into main Jul 28, 2023
3 checks passed
@SergeyRyabinin SergeyRyabinin deleted the sr/crtloggerlock branch July 28, 2023 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants