Glad it ended up ok, and if it helps at all, it's something many of us have done.
As I read your post, something that I tend to harp on when teaching technical, public safety and Instructor programs (including cave, sidemount and a bunch of other instructor programs) is that I have seen FAR too many accident reports and near miss posts like yours where something easily handled becomes truly a life threatening situation. I have no question that you have done valve drills many times in training. But without the construct of expecting to have to do one, it rocked your world, and as you said "nearly killed you".
This is a comfort issue, more specifically it is a lack of the confidence that you can deal with whatever happens using your skills you learned along the way. The why may be because once you "mastered" a skill in training you moved forward and never developed what we commonly call "muscle memory" but when it comes to specifically a sudden unexpected lack of gas delivery it's most often simply panic.
If you want to fix this so you can handle this with zero panic and not even any drama, you need to learn and be confident that you ALWAYS have at least full minute to sort out the issue even with no gas. In other words, that you can hold your breath a minute after you exhaled.
Go take a freediving course, even if you aren't the image of a young athlete, you don't need to be, you will be an amazingly more safe diver. You will develop the skills and experience of breath holding that gives you the confidence that with a fully redundant system such as sidemount/doubles you will be easily able to hold your breath as you sort the issue if one reg stops giving you gas.
The other thing that will make you a safer diver is take a real equipment repair class, once you understand the equipment you are using better, your decision making and comfort level will greatly increase
I ask my students to learn this, and ask my instructors to teach this, IT WILL save lives.
I grew up on a lake with an older brother trying to drown me almost daily during the summer, on the swim team thru Jr High and first year of HS and been diving since 13. Now, 41 years later, older, fatter and in beat up and put away wet shape with a mostly on smoking habit (life stress and I smoke) can still hold my breath for 2+ minutes. Virtually everyone without some lung condition can learn to comfortably hold breath for a minute.
I promise you, what you went thru regards panic and breathing some water would have not happened had you been more comfortable in the water with holding your breath