Interesting, I didn't know there was a CVar for that.
(tfw Blizzard adds hidden options that never really get officially documented)
Yeah, I've managed to reproduce the error. I don't know why it happens and since Blizzard didn't assign a name to the actual progress bar frame inside the cast bar I can't begin to try and prevent it from happening (that, and it doesn't consistently happen so it's hard to troubleshoot.)
Somebody else who knows more about the cast bar frame (or who knows how to deal with unnamed frames) would have to pitch in.
In the meantime, this doesn't seem to happen if Cast Bar Underneath is enabled, in the right-click menu on your character portrait. This changes the appearance of the cast bar so it's possible you may not like it, but I've not seen that bug happen when using this. You may have to add some code to prevent the cast bar from sometimes re-anchoring itself under the character portrait, though (it happens for a split second when I mount and fly away too quickly, but haven't seen it happen any other time.) The code for that is in
PlayerFrame.lua
Also, I think I should have suggested
NAME_PLATE_UNIT_ADDED and
NAME_PLATE_UNIT_REMOVED instead; those make a lot more sense. That way the castbar should more reliably re-anchor itself to the personal resource display when it appears (and vice-versa when it disappears) instead of only anchoring itself when you begin casting. Otherwise I can start casting something at an enemy and it isn't until the third cast that the castbar anchors itself to the Personal Resource Display.