Primoz Roglic of the Jumbo-Visma team won an uphill battle to virtually assure himself a Giro d'Italia title and earn some redemption for a meltdown in similar circumstances three years ago.
Roglic overtook overnight leader Geraint Thomas on an uphill individual time trial from Tarviso to Monte Lussari despite the horror of a late chain slip.
The penultimate day time trial has haunted the Slovenian since he lost the 2020 Tour de France in the same manner as Tadej Pogacar overtook and stormed to triumph.
On Saturday, Ineos Grenadiers leader Thomas was already trailing by 16 seconds when Roglic suffered a heart-stopping mechanical problem, which he dealt with only for team mechanics to waste time demanding he switch bikes.
Roglic held his nerve and powered up the steep final incline engulfed by screaming fans waving Slovenian flags.
The three-time Vuelta a Espana winner even accelerated with everything he had to the finish line to finish on 44min 23sec.
Thomas needed to finish within 26 seconds to retain the lead but as he edged agonisingly over the final 400m it became clear that victory belonged to Roglic and the crowds on the mountaintop barely 20km from the Slovenian border appeared relieved.
Roglic cut a picture of happiness after his nerve-racking feat.
"In the end, it's not about the win, but the people, the energy, beautiful moments to experience and cherish," he said.
"One more day to go, one more focus, because I think the lap (inside Rome) is quite technical. So it's not over until it's finished. But yeah, it looks good."
The 37-year-old Thomas has never been dominant in the mountains and despite digging into deep reserves his efforts would only earn second place.
The 2018 Tour de France winner did a good job hiding his horror at what had happened.
"If you'd offered me second at the start of the Giro I'd have bitten your hand off," said Thomas.
"But at least he smashed me. It would have been worse if it had been a second or something."
In a tour blighted by bad weather and Covid, with 51 of the riders pulling out along the way, Thomas lost three teammates early on, but Roglic ended the Giro with a complete team.
Sunday's 21st and final stage is a flat run around Roma. Roglic leads Thomas by 14 seconds with Portugal's Joao Almeida in third.
Italy's Jonathan Milan of Bahrain Victorious leads the points competition for the Ciclamino jersey.
Thibaut Pinot of Groupama-FDJ sealed the King of the Mountains blue tunic and ended Saturday's stage with a flourish as he accelerated to the line to finish fifth on the day and rise to fifth overall.
Almeida leads the best under-25's competition. (AFP)