gui: fix slow agent switching by closing fid on stop
When switching agents, the old state/chat streams would block until data arrived (up to 500ms for statewait, indefinitely for chat). Now requestStop() closes the fid from the calling thread, which unblocks the worker's blocking read immediately. This makes agent switching instant instead of waiting for the read timeout.
This commit is contained in:
parent
0c30ec71d2
commit
65014395b7
|
|
@ -10,10 +10,24 @@ Lib9pStreamer::Worker::Worker(const QString &path)
|
|||
|
||||
Lib9pStreamer::Worker::~Worker()
|
||||
{
|
||||
QMutexLocker lock(&m_fidMutex);
|
||||
if (m_fid >= 0 && m_client) {
|
||||
m_client->closeFid(m_fid);
|
||||
m_fid = -1;
|
||||
}
|
||||
delete m_client;
|
||||
m_client = nullptr;
|
||||
}
|
||||
|
||||
void Lib9pStreamer::Worker::requestStop()
|
||||
{
|
||||
m_stopRequested = true;
|
||||
// Close the fid to unblock any blocking read
|
||||
QMutexLocker lock(&m_fidMutex);
|
||||
if (m_fid >= 0 && m_client) {
|
||||
m_client->closeFid(m_fid);
|
||||
m_fid = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void Lib9pStreamer::Worker::run()
|
||||
|
|
@ -25,19 +39,34 @@ void Lib9pStreamer::Worker::run()
|
|||
return;
|
||||
}
|
||||
|
||||
m_fid = m_client->open(m_path);
|
||||
if (m_fid < 0) {
|
||||
int fid = m_client->open(m_path);
|
||||
if (fid < 0) {
|
||||
emit finished(false, QStringLiteral("Failed to open ") + m_path + ": " + m_client->lastError());
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
QMutexLocker lock(&m_fidMutex);
|
||||
if (m_stopRequested) {
|
||||
// Stop requested while opening
|
||||
m_client->closeFid(fid);
|
||||
emit finished(true, QString());
|
||||
return;
|
||||
}
|
||||
m_fid = fid;
|
||||
}
|
||||
|
||||
// Read loop
|
||||
char buf[8192];
|
||||
while (!m_stopRequested) {
|
||||
int n = m_client->readFid(m_fid, buf, sizeof(buf));
|
||||
if (n < 0) {
|
||||
// Error
|
||||
// Error — could be from requestStop closing the fid
|
||||
if (m_stopRequested) {
|
||||
emit finished(true, QString());
|
||||
} else {
|
||||
emit finished(false, m_client->lastError());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (n == 0) {
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ public:
|
|||
explicit Worker(const QString &path);
|
||||
~Worker() override;
|
||||
|
||||
void requestStop() { m_stopRequested = true; }
|
||||
void requestStop();
|
||||
|
||||
public slots:
|
||||
void run();
|
||||
|
|
@ -83,6 +83,7 @@ private:
|
|||
volatile bool m_stopRequested = false;
|
||||
Lib9pClient *m_client = nullptr;
|
||||
int m_fid = -1;
|
||||
QMutex m_fidMutex; // protects m_fid during close
|
||||
};
|
||||
|
||||
#endif // LIB9PSTREAMER_H
|
||||
|
|
|
|||
Loading…
Reference in New Issue