One of the best ways to prevent an ugly situation from ever occurring with your dog is by addressing any shift in behavior at its lowest level of interest, (ears perked forward, closed mouth, crinkled forehead, a glance away from you), before it has chance to escalate into something that cannot be reversed.
Balancing the relationship between leadership/rules, and affection is where the magic is at!