Skip to content

Commit

Permalink
fix: [workspace]refresh crash issue
Browse files Browse the repository at this point in the history
the dfm crash when press the F5 key to refresh view, because the travsersal token map used between mutli threads.

Log: fix crash issue
Bug: https://pms.uniontech.com/bug-view-221483.html
  • Loading branch information
Lighto-Ku authored and Johnson-zs committed Oct 9, 2023
1 parent eae055d commit b503b26
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ bool RootInfo::initThreadOfFileData(const QString &key, DFMGLOBAL_NAMESPACE::Ite
QDir::AllEntries | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden,
QDirIterator::FollowSymlinks));
traversalThread->traversalThread->setSortAgruments(order, role, isMixFileAndFolder);
traversalThread->traversalThread->setTraversalToken(key);
initConnection(traversalThread->traversalThread);
traversalThreads.insert(key, traversalThread);

Expand Down Expand Up @@ -274,7 +275,7 @@ void RootInfo::handleTraversalResult(const FileInfoPointer &child, const QString
{
auto sortInfo = addChild(child);
if (sortInfo)
Q_EMIT iteratorAddFile(currentKey(travseToken), sortInfo, child);
Q_EMIT iteratorAddFile(travseToken, sortInfo, child);
}

void RootInfo::handleTraversalResults(QList<FileInfoPointer> children, const QString &travseToken)
Expand All @@ -290,7 +291,7 @@ void RootInfo::handleTraversalResults(QList<FileInfoPointer> children, const QSt
}

if (sortInfos.length() > 0)
Q_EMIT iteratorAddFiles(currentKey(travseToken), sortInfos, infos);
Q_EMIT iteratorAddFiles(travseToken, sortInfos, infos);
}

void RootInfo::handleTraversalLocalResult(QList<SortInfoPointer> children,
Expand All @@ -304,19 +305,19 @@ void RootInfo::handleTraversalLocalResult(QList<SortInfoPointer> children,
addChildren(children);
traversaling = false;

Q_EMIT iteratorLocalFiles(currentKey(travseToken), children, originSortRole, originSortOrder, originMixSort);
Q_EMIT iteratorLocalFiles(travseToken, children, originSortRole, originSortOrder, originMixSort);
}

void RootInfo::handleTraversalFinish(const QString &travseToken)
{
traversaling = false;
emit traversalFinished(currentKey(travseToken));
emit traversalFinished(travseToken);
traversalFinish = true;
}

void RootInfo::handleTraversalSort(const QString &travseToken)
{
emit requestSort(currentKey(travseToken));
emit requestSort(travseToken);
}

void RootInfo::handleGetSourceData(const QString &currentToken)
Expand Down Expand Up @@ -540,13 +541,3 @@ FileInfoPointer RootInfo::fileInfo(const QUrl &url)
info = InfoFactory::create<FileInfo>(currentUrl);
return info;
}

QString RootInfo::currentKey(const QString &travseToken)
{
assert(!travseToken.isEmpty());
for (const auto &traversalThread : traversalThreads) {
if (travseToken == QString::number(quintptr(traversalThread->traversalThread.data()), 16))
return traversalThreads.key(traversalThread);
}
return QString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public Q_SLOTS:
void enqueueEvent(const QPair<QUrl, EventType> &e);
QPair<QUrl, EventType> dequeueEvent();
FileInfoPointer fileInfo(const QUrl &url);
QString currentKey(const QString &travseToken);

public:
AbstractFileWatcherPointer watcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void TraversalDirThreadManager::setSortAgruments(const Qt::SortOrder order, cons
}
}

void TraversalDirThreadManager::setTraversalToken(const QString &token)
{
traversalToken = token;
}

void TraversalDirThreadManager::start()
{
auto local = dirIterator.dynamicCast<LocalDirIterator>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class TraversalDirThreadManager : public TraversalDirThread
QObject *parent = nullptr);
virtual ~TraversalDirThreadManager() override;
void setSortAgruments(const Qt::SortOrder order, const dfmbase::Global::ItemRoles sortRole, const bool isMixDirAndFile);
void setTraversalToken(const QString &token);

void start();

public Q_SLOTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,12 @@ TEST_F(UT_RootInfo, Bug_195309_fileInfo)
// rootInfoObj->fileInfo() did not call fileinfo.refresh() anymore.
// EXPECT_TRUE(calledRefresh);
}

TEST_F(UT_RootInfo, Bug_221483_traversalToken) {
QString key("threadKey");
ItemRoles role = ItemRoles::kItemFileDisplayNameRole;
Qt::SortOrder order = Qt::AscendingOrder;
rootInfoObj->initThreadOfFileData(key, role, order, false);

EXPECT_EQ(rootInfoObj->traversalThreads.value(key)->traversalThread->traversalToken, key);
}

0 comments on commit b503b26

Please sign in to comment.