Skip to content

Avoid resetting unique_ptr not being the owner of the pointed element#1086

Closed
whoan wants to merge 1 commit intomicrosoft:masterfrom
whoan:avoid-race-condition-on-unique-ptr
Closed

Avoid resetting unique_ptr not being the owner of the pointed element#1086
whoan wants to merge 1 commit intomicrosoft:masterfrom
whoan:avoid-race-condition-on-unique-ptr

Conversation

@whoan
Copy link
Copy Markdown
Contributor

@whoan whoan commented Mar 26, 2019

This PR is to avoid resetting a unique_ptr in a new thread as it (the new thread) is not the owner of the pointed element, and unique_ptr is not thread safe, so some obscure errors could happen. Consider this snippet for testing purposes:

#include <memory>
#include <thread>

int main() {
  std::unique_ptr<int> unique(new int);
  std::thread thread([&unique] () {
    unique.reset();
  });
  unique.reset();
  thread.join();
  return 0;
}

Both unique.reset(); might be called at the same time causing UB. I think something similar is happening in shutdown_wspp_impl.

What is the price of stop calling m_client.reset(); as suggested in this PR?

This might be related to issue #32

@whoan whoan changed the title Avoid resetting unique_ptr not being the owner of the pointer Avoid resetting unique_ptr not being the owner of the pointed element Mar 26, 2019
@BillyONeal
Copy link
Copy Markdown
Member

Hmmm I don't think this is the case because it looks like the thread is joined before the call to reset, not after. This change doesn't seem to do anything other than consume more memory?

@whoan
Copy link
Copy Markdown
Contributor Author

whoan commented Mar 26, 2019

@BillyONeal I have seen some double deletions of the unique_ptr but I wonder if it was before PR #1080 was joined. I will try to reproduce later and will let you know.

@BillyONeal
Copy link
Copy Markdown
Member

OK, feel free to reopen if you can run it down.

@BillyONeal BillyONeal closed this Mar 26, 2019
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