You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azerothcore自定义存款命令:世界进程竞态条件可能性问询

关于.deposit命令的竞态条件疑问

场景说明

我正在开发一条.deposit $Character $moneyDelta命令,用于给离线角色增加金币,对应的处理函数如下:

static bool HandleDepositCommand(ChatHandler *handler, PlayerIdentifier player, uint32 moneyDelta)
{
    ObjectGuid::LowType accountId = player.GetGUID().GetCounter();

    // 玩家必须处于离线状态
    if (player.IsConnected()) {
        return false;
    }

    // 要求玩家在该函数执行完成前不能登录
    // TODO 执行增加金币的SQL查询

    return true;
}

为了避免引擎缓存覆盖SQL写入操作,我要求操作时玩家必须处于未登录状态。

核心问题

玩家是否有可能在HandleDepositCommand()执行完成前登录?

我认为世界进程是单线程循环,新登录这类操作会在HandleDepositCommand()执行完成后才会被处理,世界循环的核心逻辑代码如下:

///- 未触发World::m_stopEvent时持续更新世界
while (!World::IsStopped())
{
    ++World::m_worldLoopCounter;
    realCurrTime = getMSTime();

    uint32 diff = getMSTimeDiff(realPrevTime, realCurrTime);
    if (diff < minUpdateDiff)
    {
        uint32 sleepTime = minUpdateDiff - diff;
        if (sleepTime >= halfMaxCoreStuckTime)
            LOG_ERROR("server.worldserver", "WorldUpdateLoop() waiting for {} ms with MaxCoreStuckTime set to {} ms", sleepTime, maxCoreStuckTime);
        // 休眠直到有足够时间更新所有计时器
        std::this_thread::sleep_for(Milliseconds(sleepTime));
        continue;
    }

    sWorld->Update(diff);
    realPrevTime = realCurrTime;
}

如果实际情况和我的理解不符,就会出现竞态条件漏洞:玩家在SQL写入完成前登录,导致数据库状态被覆盖。

内容的提问来源于stack exchange,提问作者nryrk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:03:29