Server crashes with stack overflow when disabling a bot that owns itself
Summary
Disabling a bot whose OwnerId points back at itself sends the server into unbounded recursion and kills the whole process with fatal error: stack overflow. It is not just the request that fails — the server dies and restarts.
This is reachable with default settings and Mattermost's own tooling: mmctl user convert <user> --bot leaves the resulting bot as its own owner (Bots.OwnerId == Bots.UserId), and ServiceSettings.DisableBotsWhenOwnerIsDeactivated defaults to true.
Environment
| Server version | 11.9.0 (Enterprise) |
| Commit | 583461af10fc9b2aef2871c722fc4376db695832 |
| Build date | 2026-07-07 |
| mmctl | 11.9.0 |
| DB | PostgreSQL |
DisableBotsWhenOwnerIsDeactivated |
true (default) |
Steps to reproduce
mmctl user create --username selfbot --email [email protected] --password '...'
mmctl user convert selfbot --bot # leaves OwnerId == UserId
mmctl bot disable selfbot # server diesConfirm the precondition:
SELECT u.username FROM bots b JOIN users u ON u.id = b.userid WHERE b.ownerid = b.userid;Actual behaviour
runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0x2f1641c06380 stack=[0x2f1641c06000, 0x2f1661c06000]
fatal error: stack overflowwith these four frames repeating for the entire stack:
channels/app.(*App).UpdateActive(...)
channels/app.(*App).UpdateBotActive(...)
channels/app.(*App).disableUserBots(...)
channels/app.(*App).userDeactivated(...)
channels/app.(*App).UpdateActive(...)
channels/app.(*App).UpdateBotActive(...)
channels/app.(*App).disableUserBots(...)
channels/app.(*App).userDeactivated(...)A side effect worth noting: the user account does get deactivated before the recursion runs away, while Bots.DeleteAt stays 0. The caller never receives a response — mmctl simply hangs until the server dies. Afterwards the bot cannot be addressed by name any more, because GetUserByUsername does not return deactivated accounts, which makes the state look more confusing than it is.
Expected behaviour
bot disable either succeeds or returns an error. It must not terminate the server.
Root cause
UpdateBotActive calls UpdateActive before it marks the bot as deleted:
if _, err := a.UpdateActive(rctx, user, active); err != nil {
return nil, err
}
bot, nErr := a.Srv().Store().Bot().Get(botUserId, true)
...
} else if !active && bot.DeleteAt == 0 {
bot.DeleteAt = model.GetMillis()
}Deactivating the user runs userDeactivated → disableUserBots, which enumerates the owner's bots with IncludeDeleted: false:
options := &model.BotGetOptions{
OwnerId: userID,
IncludeDeleted: false,
...
}
...
for _, bot := range userBots {
_, err := a.UpdateBotActive(rctx, bot.UserId, false)At that moment the bot's DeleteAt is still 0, so a self-owned bot is returned again and handed straight back to UpdateBotActive. The cycle never terminates.
Note on longer cycles
A guard like if bot.UserId == userID { continue } in disableUserBots fixes the self-owned case but not an ownership cycle (bot A owned by bot B, bot B owned by bot A). I modelled the call graph and measured all three variants:
| setup | current code | bot.UserId == userID guard |
mark before UpdateActive |
|---|---|---|---|
| bot owns itself | stack overflow | terminates | terminates |
| A owns B, B owns A | stack overflow | stack overflow | terminates |
| bot owned by a human | terminates | terminates | terminates |
Such a cycle is constructible today, because UpdateBotOwner performs no validation at all on newOwnerId — it neither rejects the bot itself nor another bot:
func (a *App) UpdateBotOwner(rctx request.CTX, botUserId, newOwnerId string) (*model.Bot, *model.AppError) {
bot, err := a.Srv().Store().Bot().Get(botUserId, true)
...
bot.OwnerId = newOwnerIdSuggested fix
Two parts:
In
UpdateBotActive, move theBot().Getand theDeleteAtupdate above theUpdateActivecall. On re-entry the bot is then filtered out byIncludeDeleted: falseand the recursion terminates — for any cycle length, not just self-ownership.In
UpdateBotOwner, rejectnewOwnerId == botUserIdand reject an owner that is itself a bot, so the cycle cannot be created in the first place. This needs two new message ids inserver/i18n/en.json.
Trade-off of (1) worth reviewing: if UpdateActive fails, the bot is already marked while the user is still active — the reverse of today's ordering. For a disable operation that seems acceptable, but it is a change in error semantics.
I am happy to open a PR with this if the approach looks right.
Workarounds
- Give every bot a real (non-bot) owner:
mmctl bot assign <bot> <user>. Existing self-owned bots are then safe to disable. - Or set
ServiceSettings.DisableBotsWhenOwnerIsDeactivatedtofalse, which takesdisableUserBotsout of the path — at the cost of losing the feature.
Source: mattermost/mattermost