Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Exit the lock before logging #1317

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

Merged
merged 4 commits into from
Nov 2, 2020

Conversation

codeboten
Copy link
Contributor

Description

Avoid logging inside a lock.

Fixes #1316

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit test

Checklist:

  • Followed the style guidelines of this project

@codeboten codeboten requested review from a team, toumorokoshi and lzchen and removed request for a team November 2, 2020 05:15
codeboten referenced this pull request Nov 2, 2020
Before this change, the following would cause unintended behaviour in the Span API's add_event, update_name and set_status methods:

- thread A calls set_status, locks to check if a span has ended, and releases the lock
- thread B obtains the lock and ends a span
- thread A continues its call of set_status

This change ensures that the update operations are done with the lock being held. It's done in a decorator, but that is completely optional.
Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

I think the try/catch is worth trying to remove, otherwise LGTM!

logger.warning("Calling %s on an ended span.", func.__name__)
return
func(self, *args, **kwargs)
try:
Copy link
Member

Choose a reason for hiding this comment

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

I might consider re-writing this try/catch as an if/else instead. A good way to do this is to set a value inside the lock, and use that to determine if the logging call is needed outside.

Exception handling has a lot of overhead aside from branching.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, this just seemed like the cleanest way to implement. Have reworked to use if/else, please take a look

raise _SpanEndedException
func(self, *args, **kwargs)
except _SpanEndedException:
logger.warning("Calling %s on an ended span.", func.__name__)
Copy link
Member

Choose a reason for hiding this comment

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

would it be good time to fix this message? I feel like this implies the cal occurs, even if the span has already ended.

@@ -371,13 +371,21 @@ def _create_immutable_attributes(attributes):
return MappingProxyType(attributes.copy() if attributes else {})


class _SpanEndedException(Exception):
Copy link
Member

Choose a reason for hiding this comment

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

this is unescessary, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correct, forgot to clean it up

Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

approving, have one comment that's worth a look though. Thanks!

@codeboten codeboten merged commit c4e28b9 into open-telemetry:master Nov 2, 2020
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.

Avoid logging in lock
3 participants