Much of this falls under the public duty doctrine, or duty to rescue. Essentially, the police cannot be held liable for failing to protect unless a special relationship exists between the governmental entity and individual that sustained injury. There is quite a bit of case law outlining the principle.
Which essentially means, whenever a law enforcement officer runs to gunfire, or puts their life on the line to intervene and protect people, they do so completely because they want to and because they are invested in the cause. There is no legal duty or obligation requiring them to do that, they do it because they want to help and protect people, and are willing to assume risk to themselves to do it.