In a day of sublime and ridiculous cricket, Jos Buttler halted a batting collapse while Moeen Ali claimed key wickets Sunday to keep England on track for victory in the third and final Test against Sri Lanka.
The home side were reeling at 53-4 at the close of the third day as they chased an unlikely 327 to avoid suffering only their third whitewash series defeat on home territory.
Dilruwan Perera took Keaton Jennings lbw with the first ball of the third day to shake England who at one stage looked in trouble on 39-4 after more failures by their top-order batsmen.
But after the scare, spinner Lakshan Sandakan made basic blunders that set the tone for England to score 230 and set their sights on a third straight win.
The left-arm spinner had Stokes caught at cover on 22, then at slip on 32, only for replays to show Sandakan had overstepped the bowling line on each occasion.
Stokes had to be called back from his march to the pavilion both times. He shook his head in disbelief as he walked off for lunch.
Buttler was given out lbw on 27 to part-time spinner Dhananjaya de Silva, but the decision was overturned by a review which deemed the ball was heading over the stumps.
The pair came back to complete an 89-run fifth wicket stand that rescued England and became the centrepiece of the second innings total.
Jos Buttler made 64, Stokes 42 and Ben Foakes an unbeaten 36. England scored just 11 boundaries, with the lower order batsmen sweeping singles and doubles to build the score.
Spinner Perera took a creditable 5-88 to pass 100 wickets in home Tests but Sri Lanka's batting crumbled in the hour they faced, and an England victory inside four days looks near certain.
Moeen Ali opened the bowling for England and took openers Danushka Gunathilaka for six and Dimuth Karunaratne for 23 on the spin paradise pitch.
Jack Leach claimed Dhananjaya de Silva, who failed to score, while Stuart Broad took a brilliant catch off Stokes to end Angelo Mathews' innings after nine balls which produced five runs.
Buttler said after that it would be "fantastic" for England to get a rare whitewash triumph in an overseas series.
India, whose position as number one Test nation is now at risk, did the same in their tour of Sri Lanka last year.
England scored 336 in their first innings and Sri Lanka 240.