Skip to content

Commit

Permalink
Merge branch 'task/cht-1300-userattrcache-crash' into 'develop'
Browse files Browse the repository at this point in the history
CHT-1300. EXC_BAD_ACCESS KERN_INVALID_ADDRESS at UserAttrCache::invalidate

See merge request megachat/MEGAchat!1960
  • Loading branch information
jgandres committed Jul 9, 2024
2 parents c19cd77 + cdd2a11 commit f502884
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
117 changes: 74 additions & 43 deletions src/chatClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ void Client::onRequestFinish(::mega::MegaApi* /*apiObj*/, ::mega::MegaRequest *r
api.sdk.pauseActionPackets();
mInitStats.stageStart(InitStats::kStatsPostFetchNodes);

auto state = mInitState;
auto oldState = mInitState;
char* pscsn = api.sdk.getSequenceNumber();
std::string scsn;
if (pscsn)
Expand All @@ -1695,52 +1695,84 @@ void Client::onRequestFinish(::mega::MegaApi* /*apiObj*/, ::mega::MegaRequest *r
#endif

auto wptr = weakHandle();
marshallCall([wptr, this, state, scsn, contactList, chatList, sess = std::move(sid)]()
{
if (wptr.deleted())
return;

if (state == kInitHasOfflineSession)
marshallCall(
[wptr,
this,
lname = std::string{getLoggingName()},
oldState,
scsn,
contactList,
chatList,
sess = std::move(sid)]()
{
// disable this safety checkup, since dumpSession() differs from first-time login value
// // we loaded our state from db
// // verify the SDK sid is the same as ours
// if (mSid != *sid)
// {
// setInitState(kInitErrSidMismatch);
// return;
// }
checkSyncWithSdkDb(scsn, *contactList, *chatList, false);
setInitState(kInitHasOnlineSession);
mInitStats.stageEnd(InitStats::kStatsPostFetchNodes);
api.sdk.resumeActionPackets();

connect();
}
else if (state == kInitWaitingNewSession || state == kInitErrNoCache)
{
if (initWithNewSession(sess.get(), scsn, *contactList, *chatList))
{
setInitState(kInitHasOnlineSession);
mInitStats.stageEnd(InitStats::kStatsPostFetchNodes);
api.sdk.resumeActionPackets();
if (wptr.deleted())
return;

connect();
auto currentState = mInitState;
if (oldState != currentState)
{
KR_LOG_WARNING(
"%sOnrequestFinish(TYPE_FETCH_NODES): client state changed old: %d new: %d",
lname.c_str(),
oldState,
currentState);
}
else

switch (currentState)
{
setInitState(kInitErrGeneric);
KR_LOG_ERROR("%sFailed to initialize MEGAchat", getLoggingName());
api.sdk.resumeActionPackets();
case kInitHasOfflineSession:
checkSyncWithSdkDb(scsn, *contactList, *chatList, false);
setInitState(kInitHasOnlineSession);
mInitStats.stageEnd(InitStats::kStatsPostFetchNodes);
api.sdk.resumeActionPackets();

connect();
break;
case kInitWaitingNewSession:
case kInitErrNoCache:
if (initWithNewSession(sess.get(), scsn, *contactList, *chatList))
{
setInitState(kInitHasOnlineSession);
mInitStats.stageEnd(InitStats::kStatsPostFetchNodes);
api.sdk.resumeActionPackets();

connect();
}
else
{
setInitState(kInitErrGeneric);
KR_LOG_ERROR("%sFailed to initialize MEGAchat", lname.c_str());
api.sdk.resumeActionPackets();
}
break;
case kInitHasOnlineSession:
// a full reload happened (triggered by API or by the user)
checkSyncWithSdkDb(scsn, *contactList, *chatList, true);
api.sdk.resumeActionPackets();
break;
case kInitTerminated:
KR_LOG_ERROR("%sOnrequestFinish(TYPE_FETCH_NODES): client state terminated",
lname.c_str());
break;
case kInitErrCorruptCache:
case kInitErrGeneric:
case kInitCreated:
case kInitAnonymousMode:
case kInitErrSidInvalid:
KR_LOG_ERROR(
"%sOnrequestFinish(TYPE_FETCH_NODES): unexpected client state: %d",
lname.c_str(),
currentState);
api.callIgnoreResult(
&::mega::MegaApi::sendEvent,
99020,
"unexpected karere init state upon fetchnodes completion",
false,
static_cast<const char*>(nullptr));
break;
}
}
else // a full reload happened (triggered by API or by the user)
{
assert(state == kInitHasOnlineSession);
checkSyncWithSdkDb(scsn, *contactList, *chatList, true);
api.sdk.resumeActionPackets();
}
}, appCtx);
},
appCtx);
break;
}

Expand Down Expand Up @@ -5351,7 +5383,6 @@ const char* Client::initStateToStr(unsigned char state)
RETURN_ENUM_NAME(kInitErrGeneric);
RETURN_ENUM_NAME(kInitErrNoCache);
RETURN_ENUM_NAME(kInitErrCorruptCache);
RETURN_ENUM_NAME(kInitErrSidMismatch);
RETURN_ENUM_NAME(kInitErrSidInvalid);
default:
return "(unknown)";
Expand Down
7 changes: 1 addition & 6 deletions src/chatClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,13 +992,8 @@ class Client: public ::mega::MegaGlobalListener,
*/
kInitErrCorruptCache = 8,

/** The session given to init() was different than the session with which
* the SDK was initialized
*/
kInitErrSidMismatch = 9,

/** The session has expired or has been closed. */
kInitErrSidInvalid = 10
kInitErrSidInvalid = 9
};

enum
Expand Down
1 change: 0 additions & 1 deletion src/megachatapi_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7960,7 +7960,6 @@ int MegaChatApiImpl::convertInitState(int state)
{
case karere::Client::kInitErrGeneric:
case karere::Client::kInitErrCorruptCache:
case karere::Client::kInitErrSidMismatch:
case karere::Client::kInitErrSidInvalid:
return MegaChatApi::INIT_ERROR;

Expand Down

0 comments on commit f502884

Please sign in to comment.