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

Add utility to check whether the execution is in main thread. #14457

Merged
merged 50 commits into from
Jan 7, 2021

Conversation

chaoqin-li1123
Copy link
Member

For an explanation of how to fill out the fields, please see the relevant section
in PULL_REQUESTS.md

Commit Message: Use a singleton to store the main thread id and use it for assertion that some function should be executed in main thread. Prepare for [#issue 14320] (#14320) to eliminate data plane exceptions.
Additional Description:
Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Deprecated:]

Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
…_macros

Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
@chaoqin-li1123 chaoqin-li1123 marked this pull request as draft December 17, 2020 03:56
@chaoqin-li1123 chaoqin-li1123 changed the title Add utility to check whether the execution is in main thread. [WIP]: Add utility to check whether the execution is in main thread. Dec 17, 2020
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
yanavlasov
yanavlasov previously approved these changes Jan 5, 2021
Copy link
Contributor

@alyssawilk alyssawilk left a comment

Choose a reason for hiding this comment

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

Thanks for adding this utility! One thought from me regarding testing/corner cases.

@@ -168,5 +169,19 @@ class AtomicPtr : private AtomicPtrArray<T, 1, alloc_mode> {
T* get(const MakeObject& make_object) { return BaseClass::get(0, make_object); }
};

struct MainThread {
using MainThreadSingleton = InjectableSingleton<MainThread>;
bool inMainThread() const { return main_thread_id_ == std::this_thread::get_id(); }
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if inMainThread is called before Init? should we check for that?
I'd be great to have unit tests, for corner cases like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

RELEASE_ASSERT(loader_ != nullptr, "InjectableSingleton used prior to initialization"); The assert in get() method in InjectableSingleton will fail in this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will add some unit test to verify this condition.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure how to implement the unit test. Can we expect a RELEASE_ASSERT in gtest?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think EXPECT_DEATH should work there?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, it work!

@alyssawilk alyssawilk self-assigned this Jan 5, 2021
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
…_macros

Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Copy link
Contributor

@alyssawilk alyssawilk left a comment

Choose a reason for hiding this comment

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

Looks great! In future do you mind filling out the template when you submit PRs? All of the
Additional Description:
sections.

@alyssawilk alyssawilk merged commit 40c44e5 into envoyproxy:master Jan 7, 2021
@chaoqin-li1123
Copy link
Member Author

Thanks! Will fill out all the description next time.

bool inMainThread() const { return main_thread_id_ == std::this_thread::get_id(); }
static void init() { MainThreadSingleton::initialize(new MainThread()); }
static void clear() {
free(MainThreadSingleton::getExisting());
Copy link
Member

Choose a reason for hiding this comment

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

Post review drive by: I think you have a mismatched new/free here? Should this be delete? cc @chaoqin-li1123

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, it is my mistake. i will fix it in a new PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess free doesn't leak memory here because the struct doesn't have a destructor. In the fixing PR should be changed to new/delete.

mpuncel added a commit to mpuncel/envoy that referenced this pull request Jan 8, 2021
* master: (48 commits)
  Resolve 14506, avoid libidn2 for our curl dependency (envoyproxy#14601)
  fix new/free mismatch in Mainthread utility (envoyproxy#14596)
  opencensus: deprecate Zipkin configuration. (envoyproxy#14576)
  upstream: clean up code location (envoyproxy#14580)
  configuration impl: add cast for ios compilation (envoyproxy#14590)
  buffer impl: add cast for android compilation (envoyproxy#14589)
  ratelimit: add dynamic metadata to ratelimit response (envoyproxy#14508)
  tcp_proxy: wait for CONNECT response before start streaming data (envoyproxy#14317)
  stream info: cleanup address handling (envoyproxy#14432)
  [deps] update upb to latest commit (envoyproxy#14582)
  Add utility to check whether the execution is in main thread. (envoyproxy#14457)
  listener: undeprecate bind_to_port (envoyproxy#14480)
  Fix data race in overload integration test (envoyproxy#14586)
  deps: update PGV (envoyproxy#14571)
  dependencies: update cve_scan.py for some libcurl 7.74.0 false positives. (envoyproxy#14572)
  Network::Connection: Add L4 crash dumping support (envoyproxy#14509)
  ssl: remember stat names for configured ciphers. (envoyproxy#14534)
  formatter: add custom date formatting to downstream cert start and end dates (envoyproxy#14502)
  feat(lua): allow setting response body when the upstream response body is empty (envoyproxy#14486)
  Generalize the gRPC access logger base classes (envoyproxy#14469)
  ...

Signed-off-by: Michael Puncel <mpuncel@squareup.com>
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.

5 participants