Skip to content

Avoid capturing raw this pointer in WebSocket module #9247

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
20 commits merged into from
Dec 10, 2021

Conversation

JunielKatarn
Copy link
Contributor

@JunielKatarn JunielKatarn commented Dec 8, 2021

Description

  • Defines WebSocketModule::SharedState struct.
  • Enforces weak-pointer-only captures within WebSocketModule.
  • Removes URL as a member of IWebSocketResource implementations.

Type of Change

  • Bug fix

Why

CxxModules frequently pass their resources to asynchronous tasks by capturing the raw this pointer, which can lead to invalid memory access attempts due to race conditions between the thread owning the module and threads executing the asynchronous work defined in getMethods().

Microsoft Reviewers: Open in CodeFlow

@JunielKatarn JunielKatarn requested a review from a team as a code owner December 8, 2021 10:17
@JunielKatarn JunielKatarn requested a review from a team December 8, 2021 10:17
@JunielKatarn JunielKatarn changed the title [DO NOT MERGE YET] Avoid capturing raw this pointer in WS and AsyncStorage Avoid capturing raw this pointer in WS and AsyncStorage Dec 9, 2021
@JunielKatarn JunielKatarn changed the title Avoid capturing raw this pointer in WS and AsyncStorage Avoid capturing raw this pointer in WS and AsyncStorage modules. Dec 9, 2021
@JunielKatarn JunielKatarn changed the title Avoid capturing raw this pointer in WS and AsyncStorage modules. Avoid capturing raw this pointer in WS and AsyncStorage modules Dec 9, 2021
@JunielKatarn JunielKatarn requested review from vmoroz, a team and NikoAri December 9, 2021 03:44
@JunielKatarn JunielKatarn changed the title Avoid capturing raw this pointer in WS and AsyncStorage modules Avoid capturing raw this pointer in WebSocket module Dec 9, 2021
@JunielKatarn JunielKatarn requested review from a team as code owners December 9, 2021 09:49
Copy link

@NikoAri NikoAri left a comment

Choose a reason for hiding this comment

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

:shipit:

@vmoroz
Copy link
Member

vmoroz commented Dec 9, 2021

m_stream->set_option(websocket::stream_base::decorator([options = std::move(options)](websocket::request_type &req) {

options are const& - we cannot move them


Refers to: vnext/Desktop/BeastWebSocketResource.cpp:410 in 3564788. [](commit_id = 3564788, deletion_comment = False)

@vmoroz
Copy link
Member

vmoroz commented Dec 9, 2021

m_connectHandler = handler;

The handler is passed as an r-value reference, but to use the move-semantic we must use the std::move() here and in the methods below.


Refers to: vnext/Desktop/BeastWebSocketResource.cpp:523 in 3564788. [](commit_id = 3564788, deletion_comment = False)

Copy link
Member

@vmoroz vmoroz left a comment

Choose a reason for hiding this comment

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

:shipit:

@JunielKatarn JunielKatarn added Area: Networking AutoMerge Causes a PR to be automatically merged once all requirements are passed (label drives bot activity) labels Dec 10, 2021
@ghost
Copy link

ghost commented Dec 10, 2021

Hello @JunielKatarn!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 02f8b9f into microsoft:main Dec 10, 2021
@JunielKatarn JunielKatarn deleted the cxxmodule-holder branch December 10, 2021 01:22
JunielKatarn added a commit to jurocha-ms/react-native-windows that referenced this pull request Dec 17, 2021
* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
JunielKatarn added a commit to jurocha-ms/react-native-windows that referenced this pull request Dec 17, 2021
* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
JunielKatarn added a commit to jurocha-ms/react-native-windows that referenced this pull request Dec 17, 2021
* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
JunielKatarn added a commit to jurocha-ms/react-native-windows that referenced this pull request Dec 17, 2021
* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
JunielKatarn added a commit that referenced this pull request Dec 27, 2021
* Avoid capturing raw `this` pointer in WebSocket module (#9247)

* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>

* Use shared pointer in WebSocket MessageReceived (#9293)

* Set MessageReceived to self-capturing lambda

* clang format

* Change files

* Remove change files

* Change files

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
JunielKatarn added a commit that referenced this pull request Dec 27, 2021
* Avoid capturing raw `this` pointer in WebSocket module (#9247)

* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>

* Use shared pointer in WebSocket MessageReceived (#9293)

* Set MessageReceived to self-capturing lambda

* clang format

* Change files

* Remove change files

* Change files

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
ghost pushed a commit that referenced this pull request Jan 27, 2022
* Avoid capturing raw `this` pointer in WebSocket module (#9247)

* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>

* Use shared pointer in WebSocket MessageReceived (#9293)

* Set MessageReceived to self-capturing lambda

* clang format

* Change files

* Remove change files

* Change files

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
NickGerleman added a commit that referenced this pull request Jan 28, 2022
* Avoid capturing raw `this` pointer in WebSocket module (#9247)

* Use implicit (brace) constructor for getMethods()

* Define CxxModuleHolder

* Make m_asyncStorageManager shared_ptr

* Change files

* Initialize m_holder

* Implement WebSocketModule::SharedState

* Move ResourceFactory into shared state

* Revert AsyncStorage changes

* Add URL argument to IWebSocketResource::Connect

* Drop URL from factory method

* Update BeastWebSocketResource

* Handle malformed Uris within ::Connect()

* Remove urlString argument from IWebSocketResource::Make

* Use constexpr for commont test URLs

* Remove redundant move in to_hstring

* Add/remove std::move where applicable

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>

* Use shared pointer in WebSocket MessageReceived (#9293)

* Set MessageReceived to self-capturing lambda

* clang format

* Change files

* Remove change files

* Change files

* Remove vnext/Shared/InspectorPackagerConnection.cpp

* Empty

Co-authored-by: Nick Gerleman <ngerlem@microsoft.com>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Networking AutoMerge Causes a PR to be automatically merged once all requirements are passed (label drives bot activity)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants