[BUG] Extremely Rare But Fatal Active TPBS Bug(Crashing The Game)

To reproduce it(in practice it's very, very hard to reproduce so it's better to talk about this theoretically):
1. Play with Active TPBS
2. Select an actor with very low hp and be ready to input actions at any time
3. Right when an enemy starts to execute an action, immediately open the skill window for the actor that's about to die
4. Repeat 3 until the enemy action does hit and kill that actor
5. If you're making a command(attack, guard, confirm targets, select skill/item) right at the moment that actor just died(the exact same frame), the game will just crash due to null BattleManager.inputtingAction() in Scene_Battle.prototype.commandAttack, Scene_Battle.prototype.commandGuard, Scene_Battle.prototype.onActorOk, Scene_Battle.prototype.onEnemyOk, Scene_Battle.prototype.onSkillOk, Scene_Battle.prototype.onItemOk and Scene_Battle.prototype.onSelectAction and these methods demand BattleManager.inputtingAction() to return a valid action.

Unfortunately, there's really only 1 easy, simple and small way to fix the bug: For all those Scene Battle methods calling BattleManager.inputtingAction(), check whether it returns null and stop executing the rest of those methods if it's indeed null.
Any other fix will only lead to even more troubles
, because of the following sequence per frame:
1. The input window states(whether it's active and/or open) update -> Action execution effects update(on targets) -> Player input command update(what pressed keys are handled by which Scene_Battle methods)
2. If the action execution effects and player input command take place in the exact same frame, and the currently selected inputting actor dies because of those effects, there's no way to stop the Scene_Battle methods from entering invalid states(they need an existing inputting actor but that actor just died)
3. Therefore, the only way to fix the bug is to stop executing the rest of the method as soon as invalid states are detected, and in this case, it's to check whether BattleManager.inputtingAction() returns null
Obviously, it makes no sense to change this frame execution sequence just to fix this extremely rare but fatal active TPBS bug, so the aforementioned BattleManager.inputtingAction() nullability check should be used 😛

Nevertheless, it's still a minefield waiting to explode, and I'm sure I won't be the only one stepped on this minefield in the default RMMZ codebase, so this bug should still be fixed before more and more users report bugs seemingly coming out of nowhere 🙂
On a side note: I wanted to make some screenshots/videos on this, but I think they're nearly useless in this very special case anyway, as this bug is a result of very unlucky timing combinations, which can't be effectively or efficiently shown by any screenshot/video 🙂
In general, whenever you see either of the following upon game crash:
1. Cannot call method "setAttack" of undefined
2. Cannot call method "setGuard" of undefined
3. Cannot call method "setTarget" of undefined
4. Cannot call method "setSkill" of undefined
5. Cannot call method "setItem" of undefined
And the stack trace points to either of the following Scene_Battle method:
1. commandAttack
2. commandGuard
3. onActorOk
4. onEnemyOk
5. onSkillOk
6. onItemOk
Then you've most likely just faced this extremely rare but fatal bug, especially when you've just selected either of the following and the game instantly crashed for apparently no reason:
1. The attack command in the actor command window
2. The guard command in the actor command window
3. An actor when selecting an actor target
4. An enemy when selecting an enemy target
5. A skill in the skill list window
6. An item in the item list window
Do note that this bug's only possible in the active TPBS 🙂
This actually happened to me with "Cannot read property 'setAttack' of null and my actors were in full health. And you're right, wow. I've been playing with this a lot and this came up.

@DoubleX Thanks for clearing that part up. And I kept pressing Attack just to play around with Active Time Progress. Well, not really "Attack" since it's replaced with a skill through a weapon's Attack Skill trait.

I hope this bug report gets out there, seen, and worked on.
Well, that sucks. I hope this is fixed soon.
While I'd hope this to be fixed on the RMMZ side, for now you can fix it on your side.
Find all places in Scene_Battle accessing BattleManager.inputtingAction(), then immediately check if the inputting action is null, and immediately return(stop executing the rest) if it's indeed null.

In a more theoretical standpoint, the essence of the issue is that, because RMMZ uses a MVC architecture, and for every frame, it(in general but with exceptions) first updates the View states(like input windows in Scene_Battle), then updates the Model states(like BattleManager and Game_Battler), and finally updates the Controller states(like calling input handlers).
Although updating some Model states can easily cause some View states to be invalid, those invalid View states will become valid again upon the next frame after the View state updates.
However, if, in extremely rare cases, the Controller state updates access the invalid View states before the next frame(i.e., those Controller state updates are at the exact same frame of those Model state updates causing those invalid View states), the game will simply crash 🙂
On a side note: While this kind of issue will be inevtiable when the frame update sequence is View -> Model -> Controller, god knows just how many default functionality(the battle log window busy check in BattleManager is a very obvious example here) will be broken if it's changed to, like, Model -> View -> Controller, so my proposed fix can never fix the root cause, but fixing the root cause is already too late either 😉

Original thread: https://forums.rpgmakerweb.com/threads/bug-extremely-rare-but-fatal-active-tpbs-bug-crashing-the-game.126144/