The Swiss star had racked up 57 unforced errors against Leonardo Mayer in the second round and fought off five match points before finally pulling through well after midnight.
Four days after his narrow escape, however, he captured his fourth title of the year with a 7-6 (6), 7-6 (2) win over France's Gilles Simon in Sunday's final.
"I got very, very lucky," Federer said. "I know what a privilege it is to be here today."
It's been a banner year for Federer despite the fact he's fallen just short at the majors, including a heart-breaking loss to Novak Djokovic in the Wimbledon final. Federer leads the tour this season in match wins (61), top-10 wins (13) and final appearances (nine). And on Monday, he will leap past Rafael Nadal to No. 2 in the rankings for the first time in more than a year.
Though Djokovic is still far ahead in No. 1, Federer also got his third win of the year against the Serb in the semifinals. He put on such a dominant display that Djokovic said in an unusually brief post-match news conference that it was the best Federer has ever played against him.
A year ago, it was a different story. Bothered by back problems, he fell to a number of lower-ranked players and only won one title.
"The biggest concern was to be healthy this year," Federer said. "That's something I'm just so pleased about, to see that the hard work I put in last year is paying off. Not only is it paying off, but I'm feeling good, I'm waking up normal. I'm not half-broken like I was last year."
"I'm enjoying myself on the tennis court," he added. "I'm playing the way I was hoping I could play again."
Federer didn't play the cleanest match on Sunday, though, even with the roof on the stadium closed due to the extremely windy conditions.
The 17-time major winner finished with 36 unforced errors to 21 winners, but he was the more aggressive player, constantly attacking the net behind crisp approach shots and forcing Simon to try to pass. He won 24 of 35 points at net.
"He still has the same strengths, this fantastic serve and this huge forehand," Simon said. "Now I know him, so I'm not scared anymore when I have to play him. But it's still so hard to beat him."
Federer played a very loose service game to start the match, misfiring badly on his forehand to give Simon an immediate break for 1-0. Before the final, he had only dropped serve three times all week.
Simon couldn't capitalize on Federer's mistakes, though. Serving for the set at 5-4, his steady baseline game suddenly fell apart and he made several consecutive errors to allow Federer to break back and level the score.
The Frenchman had chances to take the match to a third set, getting two break points on Federer's serve at 5-6, but he missed a return wide and then hit a forehand well long to squander both.
"Every time he has an opportunity, he's really good at taking it," Simon said. "When you play a close match with him, maybe he just has this feeling that he will somehow survive the match every time."
The No. 29-ranked Simon was attempting to become the lowest-ranked player to win a Masters title since No. 50 Tomas Berdych captured the Paris title in 2005.
The Frenchman fell out of the top 20 in February for the first time since 2011, but after a solid week in which he defeated two top-10 players -- No. 4 Stan Wawrinka and No. 7 Berdych -- he'll vault back up to No. 19.
Federer Claims Shanghai Title
Roger Federer beat Gilles Simon 7-6 (6), 7-6 (2) to win the Shanghai Masters and jump Rafael Nadal for the No. 2 ranking in the world.