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

RuntimeContext::Attach returns a unique_ptr to a Token, but can not use a subclass #3034

Open
maierlars opened this issue Aug 16, 2024 · 7 comments
Assignees
Labels
bug Something isn't working Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@maierlars
Copy link

maierlars commented Aug 16, 2024

I'm referring to runtime_context.h. The Attach method returns a Token but wrapped in a unique_ptr. At the same time the Token just contains a Context which is in fact just a shared_ptr to some data. I guess Token is supposed to be some kind of guard that ensures that a context is eventually detached.

Are my assumptions correct?

If so, then we don't need an allocation of a Token for that. Instead make Token a proper guard type, that semantically behaves like a unique_ptr. Is it possible to change that or am I missing something? Would this be considered a breaking api change?

I'm happy to provide a patch.

@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Aug 16, 2024
@marcalff
Copy link
Member

To investigate. Not sure there is an issue with the current code, but this could be a posible cleanup or improvement.

@maierlars
Copy link
Author

Thanks for the answer. I don't think there is a bug, its just convoluted. It's rather a suggestion for a refactoring/cleanup. I'm happy to provide a patch if you want to.

@maierlars
Copy link
Author

I understand that everyone is busy. Is there anything I can do to make this issue more forward? Are you willing to consider pull requests with respect to this issue? Thank you very much for your time.

@marcalff
Copy link
Member

marcalff commented Sep 2, 2024

Thanks for raising the issue.

The key point here is this comment:

/**
 * RuntimeContextStorage is used by RuntimeContext to store Context frames.
 *
 * Custom context management strategies can be implemented by deriving from
 * this class and passing an initialized RuntimeContextStorage object to
 * RuntimeContext::SetRuntimeContextStorage.
 */
class OPENTELEMETRY_EXPORT RuntimeContextStorage

About simplifying nostd::unique_ptr<Token> to a guard class:

  • a guard class typically assumes that the calls to Attach() and Detach() are done from the same place, making the guard a useful helper for this case.
  • there are cases when Attach() and Detach() are called from different places, when implementing a thread pool. A guard will be inconvenient for this.

About simplifying Token because it "only" contains a context:

  • this makes the assumption that nostd::unique_ptr<Token> points to the exact class Token
  • A subclass of RuntimeContextStorage, can return a subclass of Token, by using a custom token with more state.
  • The token, for thread local storage (class ThreadLocalContextStorage), in indeed just a context, but this does not mean every token are the same.

Currently, the api is designed in such a way that:

  • implementing a custom RuntimeContextStorage
  • implementing a custom Token
  • calling Attach and Detach from different places

is possible, which is likely to be the case for thread pools.

The proposed simplification will break this.

@marcalff marcalff self-assigned this Sep 2, 2024
@maierlars
Copy link
Author

maierlars commented Sep 3, 2024

Thanks for your detailed response. I appreciate it very much!

A subclass of RuntimeContextStorage, can return a subclass of Token, by using a custom token with more state.

Sadly, this is not possible as the code is right now, because the only constructor of Token is private and only RuntimeContextStorage is a friend of Token. The only way to construct a token is calling CreateToken on the RuntimeContextStorage. The Token class looks pretty much locked down. Probably I'm missing something. See godbolt.

there are cases when Attach() and Detach() are called from different places, when implementing a thread pool. A guard will be inconvenient for this.

Probably this is just an issue with naming things. For me a unique_ptr is very much a guard-like object. I don't see any difference in handling unique_ptr over any other move-only type.

@marcalff
Copy link
Member

marcalff commented Sep 3, 2024

Thanks for your detailed response. I appreciate it very much!

A subclass of RuntimeContextStorage, can return a subclass of Token, by using a custom token with more state.

Sadly, this is not possible as the code is right now, because the only constructor of Token is private and only RuntimeContextStorage is a friend of Token. The only way to construct a token is calling CreateToken on the RuntimeContextStorage. The Token class looks pretty much locked down. Probably I'm missing something. See godbolt.

It looks like nobody has implemented a custom runtime storage with a custom token yet then.

This is a confirmed bug, the Token constructor should be protected or even better public.

A PR to fix this part will be welcome ;-)

there are cases when Attach() and Detach() are called from different places, when implementing a thread pool. A guard will be inconvenient for this.

Probably this is just an issue with naming things. For me a unique_ptr is very much a guard-like object. I don't see any difference in handling unique_ptr over any other move-only type.

Sure, a unique_ptr is very much a guard-like object.

However, changing this will break both the API and ABI.
Unless the current code is somehow flawed, and absolutely needs to be fixed, this can not change.

@marcalff marcalff added triage/accepted Indicates an issue or PR is ready to be actively worked on. bug Something isn't working and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 3, 2024
@marcalff marcalff changed the title RuntimeContext::Attach returns a unique_ptr to a shared_ptr RuntimeContext::Attach returns a unique_ptr to a Token, but can not use a subclass Sep 3, 2024
Copy link

github-actions bot commented Nov 3, 2024

This issue was marked as stale due to lack of activity.

@github-actions github-actions bot added the Stale label Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

2 participants