Skip to content

Commit d73e458

Browse files
authoredAug 17, 2019
ClientInterface: Use recursive mutex to prevent freeze in on_newplayer() (#8808)
1 parent 13b22e2 commit d73e458

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed
 

Diff for: ‎src/clientiface.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ ClientInterface::~ClientInterface()
636636
Delete clients
637637
*/
638638
{
639-
MutexAutoLock clientslock(m_clients_mutex);
639+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
640640

641641
for (auto &client_it : m_clients) {
642642
// Delete client
@@ -648,7 +648,7 @@ ClientInterface::~ClientInterface()
648648
std::vector<session_t> ClientInterface::getClientIDs(ClientState min_state)
649649
{
650650
std::vector<session_t> reply;
651-
MutexAutoLock clientslock(m_clients_mutex);
651+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
652652

653653
for (const auto &m_client : m_clients) {
654654
if (m_client.second->getState() >= min_state)
@@ -660,7 +660,7 @@ std::vector<session_t> ClientInterface::getClientIDs(ClientState min_state)
660660

661661
void ClientInterface::markBlockposAsNotSent(const v3s16 &pos)
662662
{
663-
MutexAutoLock clientslock(m_clients_mutex);
663+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
664664
for (const auto &client : m_clients) {
665665
if (client.second->getState() >= CS_Active)
666666
client.second->SetBlockNotSent(pos);
@@ -705,7 +705,7 @@ void ClientInterface::UpdatePlayerList()
705705
infostream << "* " << player->getName() << "\t";
706706

707707
{
708-
MutexAutoLock clientslock(m_clients_mutex);
708+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
709709
RemoteClient* client = lockedGetClientNoEx(i);
710710
if (client)
711711
client->PrintInfo(infostream);
@@ -724,7 +724,7 @@ void ClientInterface::send(session_t peer_id, u8 channelnum,
724724

725725
void ClientInterface::sendToAll(NetworkPacket *pkt)
726726
{
727-
MutexAutoLock clientslock(m_clients_mutex);
727+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
728728
for (auto &client_it : m_clients) {
729729
RemoteClient *client = client_it.second;
730730

@@ -739,7 +739,7 @@ void ClientInterface::sendToAll(NetworkPacket *pkt)
739739
void ClientInterface::sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt,
740740
u16 min_proto_ver)
741741
{
742-
MutexAutoLock clientslock(m_clients_mutex);
742+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
743743
for (auto &client_it : m_clients) {
744744
RemoteClient *client = client_it.second;
745745
NetworkPacket *pkt_to_send = nullptr;
@@ -763,7 +763,7 @@ void ClientInterface::sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacyp
763763

764764
RemoteClient* ClientInterface::getClientNoEx(session_t peer_id, ClientState state_min)
765765
{
766-
MutexAutoLock clientslock(m_clients_mutex);
766+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
767767
RemoteClientMap::const_iterator n = m_clients.find(peer_id);
768768
// The client may not exist; clients are immediately removed if their
769769
// access is denied, and this event occurs later then.
@@ -792,7 +792,7 @@ RemoteClient* ClientInterface::lockedGetClientNoEx(session_t peer_id, ClientStat
792792

793793
ClientState ClientInterface::getClientState(session_t peer_id)
794794
{
795-
MutexAutoLock clientslock(m_clients_mutex);
795+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
796796
RemoteClientMap::const_iterator n = m_clients.find(peer_id);
797797
// The client may not exist; clients are immediately removed if their
798798
// access is denied, and this event occurs later then.
@@ -804,7 +804,7 @@ ClientState ClientInterface::getClientState(session_t peer_id)
804804

805805
void ClientInterface::setPlayerName(session_t peer_id, const std::string &name)
806806
{
807-
MutexAutoLock clientslock(m_clients_mutex);
807+
RecursiveMutexAutoLock clientslock(m_clients_mutex);
808808
RemoteClientMap::iterator n = m_clients.find(peer_id);
809809
// The client may not exist; clients are immediately removed if their
810810
// access is denied, and this event occurs later then.
@@ -814,7 +814,7 @@ void ClientInterface::setPlayerName(session_t peer_id, const std::string &name)
814814

815815
void ClientInterface::DeleteClient(session_t peer_id)
816816
{
817-
MutexAutoLock conlock(m_clients_mutex);
817+
RecursiveMutexAutoLock conlock(m_clients_mutex);
818818

819819
// Error check
820820
RemoteClientMap::iterator n = m_clients.find(peer_id);
@@ -844,7 +844,7 @@ void ClientInterface::DeleteClient(session_t peer_id)
844844

845845
void ClientInterface::CreateClient(session_t peer_id)
846846
{
847-
MutexAutoLock conlock(m_clients_mutex);
847+
RecursiveMutexAutoLock conlock(m_clients_mutex);
848848

849849
// Error check
850850
RemoteClientMap::iterator n = m_clients.find(peer_id);
@@ -860,7 +860,7 @@ void ClientInterface::CreateClient(session_t peer_id)
860860
void ClientInterface::event(session_t peer_id, ClientStateEvent event)
861861
{
862862
{
863-
MutexAutoLock clientlock(m_clients_mutex);
863+
RecursiveMutexAutoLock clientlock(m_clients_mutex);
864864

865865
// Error check
866866
RemoteClientMap::iterator n = m_clients.find(peer_id);
@@ -881,7 +881,7 @@ void ClientInterface::event(session_t peer_id, ClientStateEvent event)
881881

882882
u16 ClientInterface::getProtocolVersion(session_t peer_id)
883883
{
884-
MutexAutoLock conlock(m_clients_mutex);
884+
RecursiveMutexAutoLock conlock(m_clients_mutex);
885885

886886
// Error check
887887
RemoteClientMap::iterator n = m_clients.find(peer_id);
@@ -896,7 +896,7 @@ u16 ClientInterface::getProtocolVersion(session_t peer_id)
896896
void ClientInterface::setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch,
897897
const std::string &full)
898898
{
899-
MutexAutoLock conlock(m_clients_mutex);
899+
RecursiveMutexAutoLock conlock(m_clients_mutex);
900900

901901
// Error check
902902
RemoteClientMap::iterator n = m_clients.find(peer_id);

Diff for: ‎src/clientiface.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class ClientInterface {
501501

502502
// Connection
503503
std::shared_ptr<con::Connection> m_con;
504-
std::mutex m_clients_mutex;
504+
std::recursive_mutex m_clients_mutex;
505505
// Connected clients (behind the con mutex)
506506
RemoteClientMap m_clients;
507507
std::vector<std::string> m_clients_names; //for announcing masterserver

0 commit comments

Comments
 (0)
Please sign in to comment.