Skip to content

Protect websockets_ by mutex#1136

Merged
gittiver merged 2 commits into
CrowCpp:masterfrom
softcom-su:websockets-mutex
Feb 3, 2026
Merged

Protect websockets_ by mutex#1136
gittiver merged 2 commits into
CrowCpp:masterfrom
softcom-su:websockets-mutex

Conversation

@andrew-guz
Copy link
Copy Markdown
Contributor

@andrew-guz andrew-guz commented Jan 23, 2026

We faced the situation, when std::vector<std::shared_ptr<websocket::connection>> websockets_ will be modified in several threads in case of several simultaneous connections been opened and closed.

To prove this and for future needs, we added ThreadSanitizer support (ENABLE_TSAN flag) and new test (multithreaded_websockets_open_close) that shows this problem:

WARNING: ThreadSanitizer: data race (pid=305193)
  Read of size 8 at 0x7ffd1dd3bb48 by main thread:
    #0 __gnu_cxx::__normal_iterator<std::shared_ptr<crow::websocket::connection>*, std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > > >::__normal_iterator(std::shared_ptr<crow::websocket::connection>* const&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../include/c++/15.2.1/bits/stl_iterator.h:1059 (unittest+0x324e8b) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)
    #1 std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > >::end() /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../include/c++/15.2.1/bits/stl_vector.h:1019 (unittest+0x324e8b)
    #2 crow::Crow<>::close_websockets() /Crow/include/crow/app.h:623 (unittest+0x324a57) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)
    #3 crow::Crow<>::stop() /Crow/include/crow/app.h:614 (unittest+0x26352e) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)

  Previous write of size 8 at 0x7ffd1dd3bb48 by thread T2:
    #0 std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > >::_M_erase_at_end(std::shared_ptr<crow::websocket::connection>*) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../include/c++/15.2.1/bits/stl_vector.h:2240 (unittest+0x62b6fc) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)
    #1 std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > >::_M_erase(__gnu_cxx::__normal_iterator<std::shared_ptr<crow::websocket::connection>*, std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > > >, __gnu_cxx::__normal_iterator<std::shared_ptr<crow::websocket::connection>*, std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > > >) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../include/c++/15.2.1/bits/vector.tcc:202 (unittest+0x62b29d) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)
    #2 std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > >::erase(__gnu_cxx::__normal_iterator<std::shared_ptr<crow::websocket::connection> const*, std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > > >, __gnu_cxx::__normal_iterator<std::shared_ptr<crow::websocket::connection> const*, std::vector<std::shared_ptr<crow::websocket::connection>, std::allocator<std::shared_ptr<crow::websocket::connection> > > >) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../include/c++/15.2.1/bits/stl_vector.h:1824 (unittest+0x62ad7f) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)
    #3 crow::Crow<>::remove_websocket(std::shared_ptr<crow::websocket::connection>) /Crow/include/crow/app.h:640 (unittest+0x62a908) (BuildId: e1fdcc2ba235601f5098b0fb0206a07bdd014f10)

So, protected websockets_ usage with new mutex.

@andrew-guz andrew-guz force-pushed the websockets-mutex branch 2 times, most recently from 3f57458 to 483f5cc Compare January 23, 2026 14:24
Comment thread CMakeLists.txt
@gittiver gittiver merged commit 773faaa into CrowCpp:master Feb 3, 2026
36 of 41 checks passed
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