I found a way to temporarily solve the problem.
Use NBT editors (Or VS code with NBT viewer add-on) to open the level.dat file in the world folder, set cheatsEnabled to 1 and keepinventory to 1. Then no matter how you turn on or shut down the server, the keepinventory gamerule will not be closed. You can have a try.
This issue may only happen if you upgrade the world from old versions to 1.21.50. Worlds created after that version have no problem.