Skip to content

Commit

Permalink
Fix possible crash on MTProto instance shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Nov 28, 2023
1 parent 17fa50b commit f442d69
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Telegram/SourceFiles/mtproto/mtp_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,9 +1134,10 @@ void Instance::Private::processCallback(const Response &response) {
QString::number(error.code()),
error.type(),
error.description()));
if (rpcErrorOccured(response, handler, error)) {
const auto guard = QPointer<Instance>(_instance);
if (rpcErrorOccured(response, handler, error) && guard) {
unregisterRequest(requestId);
} else {
} else if (guard) {
QMutexLocker locker(&_parserMapLock);
_parserMap.emplace(requestId, std::move(handler));
}
Expand All @@ -1156,12 +1157,15 @@ void Instance::Private::processCallback(const Response &response) {
"RESPONSE_PARSE_FAILED",
"Error parse failed.")));
} else {
if (handler.done && !handler.done(response)) {
const auto guard = QPointer<Instance>(_instance);
if (handler.done && !handler.done(response) && guard) {
handleError(Error::Local(
"RESPONSE_PARSE_FAILED",
"Response parse failed."));
}
unregisterRequest(requestId);
if (guard) {
unregisterRequest(requestId);
}
}
} else {
DEBUG_LOG(("RPC Info: parser not found for %1").arg(requestId));
Expand Down Expand Up @@ -1192,8 +1196,11 @@ bool Instance::Private::rpcErrorOccured(
const FailHandler &onFail,
const Error &error) { // return true if need to clean request data
if (IsDefaultHandledError(error)) {
const auto guard = QPointer<Instance>(_instance);
if (onFail && onFail(error, response)) {
return true;
} else if (!guard) {
return false;
}
}

Expand All @@ -1208,7 +1215,11 @@ bool Instance::Private::rpcErrorOccured(
? QString()
: QString(": %1").arg(error.description())));
if (onFail) {
const auto guard = QPointer<Instance>(_instance);
onFail(error, response);
if (!guard) {
return false;
}
}
return true;
}
Expand Down

0 comments on commit f442d69

Please sign in to comment.