Fix device emulation crash due to contention

Device emulation packets are received and processed in a different
thread compared to the main RPC processing thread where the emulated
devices are created/deleted. No packets should be processed while the
latter is in progress otherwise the former may access devices that have
been deleted.
This commit is contained in:
Srivats P 2020-01-26 18:35:14 +05:30
parent d2ae04c488
commit f0dd0c307b
2 changed files with 4 additions and 0 deletions

View File

@ -222,6 +222,7 @@ void DeviceManager::getDeviceList(
void DeviceManager::receivePacket(PacketBuffer *pktBuf) void DeviceManager::receivePacket(PacketBuffer *pktBuf)
{ {
QMutexLocker locker(&listLock_);
uchar *pktData = pktBuf->data(); uchar *pktData = pktBuf->data();
int offset = 0; int offset = 0;
EmulDevice dk(this); EmulDevice dk(this);
@ -401,6 +402,7 @@ void DeviceManager::enumerateDevices(
const OstProto::DeviceGroup *deviceGroup, const OstProto::DeviceGroup *deviceGroup,
Operation oper) Operation oper)
{ {
QMutexLocker locker(&listLock_);
EmulDevice dk(this); EmulDevice dk(this);
OstEmul::VlanEmulation pbVlan = deviceGroup->encap() OstEmul::VlanEmulation pbVlan = deviceGroup->encap()
.GetExtension(OstEmul::vlan); .GetExtension(OstEmul::vlan);

View File

@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <QHash> #include <QHash>
#include <QMap> #include <QMap>
#include <QMultiHash> #include <QMultiHash>
#include <QMutex>
#include <QtGlobal> #include <QtGlobal>
class AbstractPort; class AbstractPort;
@ -76,6 +77,7 @@ private:
AbstractPort *port_; AbstractPort *port_;
QMutex listLock_; // protects all the lists
QHash<uint, OstProto::DeviceGroup*> deviceGroupList_; QHash<uint, OstProto::DeviceGroup*> deviceGroupList_;
QHash<DeviceKey, Device*> deviceList_; // fast access to devices QHash<DeviceKey, Device*> deviceList_; // fast access to devices
QMap<DeviceKey, Device*> sortedDeviceList_; // sorted access to devices QMap<DeviceKey, Device*> sortedDeviceList_; // sorted access to devices