Because a lot of people seem to be confused: A Deus Ex Machina is a plot resolving event which happens for no reason whatsoever. It's the largest indicator of bad writing all around, because it's so simple to avoid. Merely foreshadow it. Foreshadow your twists long before they happen. No ifs, no buts. Foreshadowing. Learn it, apply it, and you'll never pull a stupid stunt like a Deus Ex Machina ever again.
A lot of people seem to be interpreting the deus ex machina as any random action occurring outside the character's control. But something like this is perfectly acceptable if it is properly set up, seems believable in context, and fits the story.
I don't get why someone wouldn't want to foreshadow unless they're writing a TV comedy, in which case my advice would still be to use randomness like that sparingly. Even when an event is beyond the control of the characters, you can still create suspense around it. There are many ways to do this, though the easiest is when writing from a 3rd person omniscient viewpoint. In that case, you can just shift focus to, say, a chandelier about to fall on the main character's head, describe how it slowly loosens from the ceiling. Switch back and forth a few times to increase the feeling of urgency. If writing from the viewpoint of the character, perhaps the main character has already noticed the chandelier above him, dangeling dangerously, while he is otherwise occupied so he can't get out of the way immediately. That way, you can add another dimension to a scenario and put it on a clock, raising the feeling of immediate threat to the health of the characters involved. The crafty writer will have the characters visit this location earlier and have an occupant of the building point out the age of the chandelier and how he never has the money to have someone fix it. Add that in as an offhand remark.
This is to say: the reader should never feel lost. He/she should never think: "Where did that come from? What just happened?"
(The example is of course, notably chosen for its simplicity. Applying the same mechanisms to complex plots is what really counts.)
That was a main concern of mine. Even with Foreshadowing, I worried that some stupid thing I introduced during the beginning of the chapter would result in a Deus Ex Machina moment. Biggest thing I did for the second chapter was having the main character show how enthralled he was with the Library. The purpose this serves is that the library becomes a battle ground in the end of the chapter where an object is used as a weapon while another is used as a device to restrain an individual. Final end comes from the library being engulfed in flames and the chandelier falling down just as the antagonist escapes the fiery blaze, ending the battle. It's not climatic for the enemy to run away, however, after thinking about it, I do think it's best.
Though it's probably not alright, I have the protagonist thinking the library will be his grave as he feels the heat of the blazing inferno growing around him. I want this particular moment to serve as an anti-Gary Stu counter-measure to show the main character can't do everything on his own, requiring assistance. Though his companion flew off, I do have her return to save the main character. It's at the end of the chapter both of them share a "tender moment" while the protagonist takes a moment to reflect upon his actions of causing the fire,and, unfortunately in his mind, killing the antagonist's living specimens in his lab.
If you're worried you're not foreshadowing enough, don't. It's good to keep it in mind as a question to ask your beta readers. They're the ones giving you your most valuable feedback, after all. If they felt it was properly foreshadowed, then you've done enough foreshadowing. If they don't, then you haven't and should revise to add more or make the foreshadowing more clear or memorable. In any case, readers aren't morons, they're quite capable of remembering details.
Your character simply shouldn't be a Gary Stu, and shouldn't need a particular event to reaffirm that. If it makes sense for your character to react that way, and learn from it in this way, then that's been part of your character all along.
Deus ex machinas are pretty much always unacceptable by their very definition; it's resolving a conflict in a way that doesn't fit the story and makes the writer seem lazy. The problem is that people disagree on what counts as a deus ex machina and what doesn't. I've seen online writers claim that the end of their story was planned from the beginning and therefore can't be a deus ex machina, when just because it was planned doesn't mean they actually thought it out properly and made it fit with the rest of the narrative.
"is it ever acceptable in any type of medium to have a deus ex machina moments?"
See greeks. All their literature has deus ex machina.
In real life, shit happens. Not all stories end due to the actions of the characters. A lot of things in real life are completely out of our control. We have a mon and her daughter fighting, then bam, car accident/hurricane/meteor kills both of them.
If shit happens in real life, it can happen to fiction. It looks lazy, of course, but that doesn't mean it has no merit.