David is frequently under threat in the Psalms, usually from enemies. In Psalm 40, though, the enemies seem to be different.
“Evils beyond number have surrounded me; my iniquities have overtaken me, so that I am not able to see” (v. 12). David’s own sins are his enemies, circling, setting up siege works, looking for an opportunity to strike and kill.
When he asks for deliverance (v. 13), these are the enemies that he wants to escape. When Yahweh intervenes to judges David’s enemies, David will be saved from himself.