Wow, glad to hear your ok, and sorry to hear about your 2. I shoot a 40 caliber Glock also (G27).
My first thought when you mentioned that you had several failures to fire preceding this also made me think about a squib round. I am not sure if a squib that leaves the bullet stuck in the barrel would be strong enough to cycle the gun and load another round.
Let me tell you my squid story though and then give you something to thing about.
Several years ago I bought an AK-47. I managed to site it in partially at my mothers property but lacked a 100 yard range to really verify sight in. I took the gun to my Dad's deer hunting property to verify the sights. I was shooting 5 round groups, then walking to the target to check POI adjusting and repeating. I was pretty much setup and wanted to shoot one last group of 5 to verify. I loaded 5 rounds and shot the first two, the third trigger resulting in nothing. I assumed it was a dud. I ejected the casing which landed in tall weeds. I noticed the chamber looked full of crud, but proceeded to load another few rounds in the mag. I reinserted the mag, then that little voice in my head said you should check the gun closer. Sure enough when I took the mag back out the last bullet fired was stuck one inch down the barrel. Had I pulled the trigger on that next shot I would have blown the gun up, and been stranded in the woods, quite possible injured and alone. I never heard the pop of the primer because I was wearing hearing protection but the crud in the barrel was the unburned powder. The primer alone was powerful enough to drive bullet down the barrel.
Lesson learned, when it comes to guns, treat every unexpected result as a dangerous incident.
Now on to my "something to think about."
I am not trying to berate you, or second guess your story, but lets play what if,
What if for some reason, either machine or personal, there was an issue with the assembly line that this ammo was being produced on, and sometimes the ammo was under or over charged, perhaps the charge of powder failed to go into the case, but a double charge went into the next case. Or perhaps the primer machine was having problems dispensing the correct amount or primer, sometimes adding none, then adding a little.
What if, after having several rounds fail to fire, you sort of started to get used to the event, so that when a round failed your natural reaction was to rack the slide to clear the round and continue shooting. The first several times this happened the rounds did indeed not fire at all, then the round before the explosion, what if you had a primer go off with enough force to drive the bullet out of the case and into the barrel. What if the next round did indeed fire into the stuck bullet causing the gun to explode.
You said yourself you were in shock and taking by surprise by it. Perhaps in the moment you failed to realize that the preceding round had been a dud also. I'm just thinking out loud here, so please don't take any offence.