The 49th-minute incident was missed at the time, but soon flagged up to referee Jaco Peyper by the television match official and shown on the stadium's big screen. The crowd gasped, Vahaamahina was shown a red card, and the Welsh were given hope from nowhere.
It took until about five minutes from time for them to take advantage, though, with Ross Moriarty barging over from close range for a converted try after a French scrum was shoved back near their own line.
Wales led for the first time in the match at 20-19 and held on to the final whistle, which was met by French bodies slumping to the ground in despair. "Cruel," said France captain Guilhem Guirado, shaking his head.
The Welsh know the feeling. Eight years ago, in the teams' only previous meeting at a World Cup, Wales had captain Sam Warburton sent off controversially for a tip-tackle and went onto lose 9-8 in the semifinals.
Call it belated payback. "That was nowhere near our best," Wales winger George North said, "but it's knockout rugby. You have to be in it to win it. We had self-belief to get it done, but that was too close to comfort for our liking."
The Welsh advanced to the semifinals for just the third time and will meet South Africa — a 26-3 winner against Japan in Tokyo hours later — in Yokohama on Sunday. They'll likely need to play much better.
Meanwhile, the French and their departing coach Jacques Brunel leave Japan wondering how victory slipped from their grasp. Brunel had no qualms about the red card — "I think it's a reflex on his part," he said — but was unhappy about the game-defining scrum that produced the clinching try.
As France's retreating pack splintered, Wales' scavenging scrumhalf Tomos Williams ripped the ball out of the grasp of flanker Charles Ollivon, one of three French try-scorers in the first half. The ball popped into the air — marginally forward — and into the hands of Justin Tipuric, who was held up just before the tryline. Moriarty gathered and barged over for a try converted by Dan Biggar after a review by the TMO.
"The referees said it was a rip so there was no forward pass," Guirado said, "but we should have killed off the game before that." Indeed, this game saw France at its classic best, showing flair in a dominant first-half performance before self-destructing in the second half.
"France were excellent today and very unlucky," said Wales coach Warren Gatland, whose 12-year tenure gets two more weeks as he looks to guide the rugby-mad country to a first World Cup title. "But I'm proud of my players. They never give up."
An eighth Welsh win in the last nine meetings between the teams did not look on when France scored two tries in the opening eight minutes and then pushed ahead 19-10 in the 31st minute while Wales was briefly down to 14 men following Ross Moriarty's yellow card.
France's first two tries mixed bludgeoning power with gracefulness, Vahaamahina burrowing over in the fifth minute before Ollivon finished off an 80-meter move that started with a break by the excellent Virimi Vakatawa just outside his own 22.
Wales was then gifted 10 points despite hardly having any territory or ball. A crunching tackle from Jake Ball on Guirado saw the ball fall loose and Wainwright picked it up to sprint over from 40 meters. Biggar converted, added a penalty, and suddenly Wales was only 12-10 behind.
France got back on the front foot again and took advantage of Moriarty — on for the injured Josh Navidi — getting sin-binned for a high tackle. A minute later, Vakatawa sidestepped in from an offload by winger Damian Penaud and dived over for a brilliantly try.
Flyhalf Romain Ntamack struck the post with a penalty and two more try-scoring openings were botched at the end of the first half by France's players, who looked frustrated to only turn around leading by nine points.
Then came their self-inflicted implosion, sparked by Vahaamahina's moment of madness. Even then, Wales hardly entered the French 22 despite having a man extra and its attacking play was one-paced and unimaginative.
"The better team lost today," Gatland said. "We just hung in there."
More AP Rugby World Cup: https://www.apnews.com/RugbyWorldCup and https://twitter.com/AP_Sports
Steve Douglas is at www.twitter.com/sdouglas80