Skip to content

Commit 346edbc

Browse files
committed
core: Fix a issue with Devicelist request being deleted out of order of access
1 parent ac8a02f commit 346edbc

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

core/wsserver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,15 @@ void WSServer::cleanUpSocket(QWebSocket *ws)
686686
{
687687
pendingDeviceListQuery -= pendingDeviceListWebsocket.count(ws);
688688
pendingDeviceListWebsocket.removeAll(ws);
689-
QMutableListIterator<MRequest*> it(pendingDeviceListRequests);
689+
QMutableListIterator<MRequest> it(pendingDeviceListRequests);
690690
while (it.hasNext())
691691
{
692692
it.next();
693-
if (it.value()->owner == ws)
693+
MRequest& req = it.value();
694+
if (req.owner == ws)
694695
{
695-
it.value()->owner = nullptr;
696-
it.value()->state = RequestState::CANCELLED;
696+
req.owner = nullptr;
697+
req.state = RequestState::CANCELLED;
697698
it.remove();
698699
}
699700
}

core/wsserver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private slots:
185185
unsigned int numberOfAsyncFactory;
186186
unsigned int pendingDeviceListQuery;
187187
QList<QWebSocket*> pendingDeviceListWebsocket;
188-
QList<MRequest*> pendingDeviceListRequests;
188+
QList<MRequest> pendingDeviceListRequests;
189189
QStringList deviceList;
190190

191191
QMap<ADevice*, QList<MRequest*> > pendingRequests;

core/wsservercommands.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ bool WSServer::isControlCommand(USB2SnesWS::opcode opcode)
5151
return ; \
5252
}
5353

54-
// TODO, need to delete req when async device list is done
5554

5655
void WSServer::executeServerRequest(MRequest* req)
5756
{
@@ -62,10 +61,11 @@ void WSServer::executeServerRequest(MRequest* req)
6261
case USB2SnesWS::DeviceList : {
6362
if (numberOfAsyncFactory == 0) {
6463
QStringList l = getDevicesList();
64+
sInfo() << "Server request " << *req << " executed in " << req->timeCreated.msecsTo(QTime::currentTime());
6565
sendReply(ws, l);
6666
} else {
6767
pendingDeviceListWebsocket.append(ws);
68-
pendingDeviceListRequests.append(req);
68+
pendingDeviceListRequests.append(*req);
6969
if (pendingDeviceListWebsocket.size() == 1)
7070
asyncDeviceList();
7171
}
@@ -100,8 +100,8 @@ void WSServer::executeServerRequest(MRequest* req)
100100
if (req->opcode != USB2SnesWS::DeviceList)
101101
{
102102
sInfo() << "Server request " << *req << " executed in " << req->timeCreated.msecsTo(QTime::currentTime());
103-
delete req;
104103
}
104+
delete req;
105105
}
106106

107107
void WSServer::executeRequest(MRequest *req)
@@ -540,8 +540,14 @@ void WSServer::asyncDeviceList()
540540
{
541541
if (devFact->hasAsyncListDevices())
542542
{
543-
if (devFact->asyncListDevices())
544-
pendingDeviceListQuery++;
543+
pendingDeviceListQuery++;
544+
}
545+
}
546+
for (auto devFact : qAsConst(deviceFactories))
547+
{
548+
if (devFact->hasAsyncListDevices())
549+
{
550+
devFact->asyncListDevices();
545551
}
546552
}
547553
}
@@ -551,16 +557,15 @@ void WSServer::onDeviceListDone()
551557
sDebug() << qobject_cast<DeviceFactory*>(sender())->name() << " is done doing devicelist";
552558
pendingDeviceListQuery--;
553559
if (pendingDeviceListQuery != 0)
554-
return;
560+
return;
555561
for (auto ws : qAsConst(pendingDeviceListWebsocket))
556562
{
557563
sDebug() << "Sending device list to " << wsInfos[ws].name;
558564
sendReply(ws, deviceList);
559565
}
560-
for (MRequest* req : qAsConst(pendingDeviceListRequests))
566+
for (const MRequest& req : pendingDeviceListRequests)
561567
{
562-
sInfo() << "Device request finished - " << *req << "processed in " << req->timeCreated.msecsTo(QTime::currentTime()) << " ms";
563-
delete req;
568+
sInfo() << "Device request finished - " << req << "processed in " << req.timeCreated.msecsTo(QTime::currentTime()) << " ms";
564569
}
565570
pendingDeviceListQuery = 0;
566571
deviceList.clear();

0 commit comments

Comments
 (0)