[SERVER-53995] ReplicaSetMonitor shared_ptr instance should be freed outside of mutex scope Created: 22/Jan/21  Updated: 29/Oct/23  Resolved: 17/Feb/21

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: None
Fix Version/s: 4.9.0

Type: Bug Priority: Major - P3
Reporter: Alexander Taskov (Inactive) Assignee: Jason Zhang
Resolution: Fixed Votes: 0
Labels: Sharding-NYC, bkp, sharding-wfbf-day
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v5.0
Sprint: Sharding 2021-02-22
Participants:
Linked BF Score: 19

 Description   

In ReplicaSetMonitorManager::getMonitorForHost() we iterate over the _monitors map, which contains weak_ptrs as the value, and extract a shared_ptr while holding the class mutex. However, if the owning shared_ptr is freed after we've acquired it here, then the local shared_ptr will be the last reference and destroy the ReplicaSetMonitor that is pointed to. The destructor of that class tries to acquire the same mutex and deadlocks.

This can be fixed by declaring the local shared_ptr outside the lock and freeing it after the lock is released. However, since each iteration of the loop acquires a different shared_ptr, one shared_ptr variable can't be used because it's current value will be released when the next one is assigned to it possibly releasing the last reference.



 Comments   
Comment by Githook User [ 17/Feb/21 ]

Author:

{'name': 'Jason Zhang', 'email': 'jason.zhang@mongodb.com', 'username': 'jz1242'}

Message: SERVER-53995 ReplicaSetMonitor shared_ptr instance should be freed outside of mutex scope
Branch: master
https://github.com/mongodb/mongo/commit/0f7281ffd6bf5f700ff9d0f6163beccf6803a58a

Generated at Thu Feb 08 05:32:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.