Thanks for the patch, I have verified that the fix works well by following the steps mentioned to reproduce the problem.
Reviewing the patch, it seems good and is well documented. Just one minor comment I had was probably to change the name of the variable table_states_valid to table_states_validity. The current name made sense when it was a bool, but now that it is a tri-state enum, it doesn't fit well.