Repository navigation
fix(minimax-bot): 新建失败时不再留下幽灵对象 - #137
Open
Apageoflove wants to merge 1 commit into
Open
Apageoflove wants to merge 1 commit into
Apageoflove wants to merge 1 commit into
Conversation
建 Bot、建群、建定时任务都是先改内存里的 state 再 await save()。save 抛错 时新对象已经在内存里,接口回 500,用户以为没建成,可它还在;之后任何一次 无关保存都会把它写进 state.json,重启后仍留在队伍里。 updateBot 的注释里已经写明这条原则(a rejected request must leave no trace), startHelper 也有同样的回滚,只有这三个 create 路径没做。 save 失败时把状态退回去:createBot / createGroup 摘掉刚 push 的对象后重新 抛错;saveSchedule 新建就摘掉,更新就把改之前的字段值盖回去。 两条新测试在 0.7.28 上红、在切片上绿。全量 415 条通过。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
建 Bot、建群、建定时任务,这三个动作在写盘失败的时候会留下一个幽灵。
它们都是先往内存里的 state 塞新对象,再 await save()。save 抛错的时候对象已经在内存里了,接口回 500,用户以为没建成。可它还在。之后用户做任何别的事,改个语言设置、删个别的会话,只要有一次保存成功,这个幽灵就跟着写进 state.json,重启之后还在队伍里。
复现是拿真实路由跑的。先把 state.json 的位置换成一个目录让写入失败,然后分别走 POST /api/bots、POST /api/groups、POST /api/schedules。在 0.7.28 上:
三个都是拒绝之后还在,而且被后面的无关保存落盘。改完之后三行都变成内存里=false、磁盘上=false。
判据不是我发明的,是项目自己写的。team.mjs 里 updateBot 那段注释说得很清楚,a rejected request must leave no trace, or a later unrelated save() would persist the half-applied change。它也确实这么做了,先在一份 draft 上把整个 patch 校验完,全部通过才 Object.assign 到真对象。startHelper 在 engine.mjs 里也有同样的回滚。只有这三个 create 路径没做。
改法是 save 失败时把状态退回去。createBot 和 createGroup 把自己刚 push 进去的对象摘掉,重新抛错,和 startHelper 的写法一致。saveSchedule 有两条路,新建就摘掉,更新就把改之前的字段值盖回去,因为用户看到的是原来那版。
测试加在 storage-reliability 里,两条。一条把三个 create 都打一遍,断言内存里没有、恢复存储后再保存磁盘上也没有。一条测改定时任务失败时,内存里的对象要回到磁盘上还留着的那版值,而不是保留被拒绝的新值。两条在 0.7.28 上都红,在切片上绿。全量 415 条过。
版本号到 0.7.30,README 补了一节。0.7.29 被 #133 占着。
这两个路径我还没细看,所以没动:updateBot 之外的其他修改类入口,回滚是不是完整,得逐个看才知道。
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.