I had departed WVI for a long cross-country trip to PHX in a Cessna 172. On the climb from WVI, the oil temp was just below the red line, and I had to level out at 7500 and 9500 for cooling. Even so, the OAT was 15 degrees C at 9500 (very hot). I leaned the mixture to 50 degrees rich of peak EGT and had the throttle full forward at 2500 RPM. It was the middle of a hot afternoon flight. About twenty minutes of cruise had passed when the engine started to vary from 2100 to 2500 RPM. So fuel selector left, mixture rich, carb heat on (helped a little, 200 RPM), mag check normal (a little backfire when off), primer locked, mixture all the way out (engine stops), mixture full in (engine back to 2200), throttle all the way back (engine stops), throttle forward (engine back to 2200). By now the engine has stopped surging and stabilized at 2200 RPM with the carb heat on and 2000 with carb heat off. I checked for the closest runway. New Coalinga was about 15 miles away, so I continued to it. I knew I could glide to the airport, so I advised Oakland Center I was making a precautionary landing at C80 due to engine trouble. They asked if there was anything else they could do, and I replied I would let them know. I had at least ten minutes to diagnose the problem. It wasn't carb ice, but pulling carb heat helped, so it could have been a clogged air filter OR richening the mixture with carb heat helped. I think the richening the mixture was what was helping. Two things make an engine run - fuel and spark. Since the mag check yeilded normal 100 RPM drops, spark wasn't a problem. Of course many things can make an engine stop. Low oil pressure, redline oil temperature, broken cylinder rod, stuck exhaust or intake valve, blown gaskets, etc. In flight, these are hard to diagnose, and as far as I know, impossible to fix (OK so some endurance record holders have an in-flight catwalk, but not on a 172). Since I had good oil pressure and the oil temperature was steadily decreasing, there was no oil problem. It could still be a valve problem, but I had no way to diagnose or fix that in the air. So I tried to fix a possible fuel problem. Water or other contaminants in the fuel were possible, so I switched from right and left tanks (leaving each for a few minutes) and had no change. Since there was no water in the tanks (I shake them frequently and have new seals for the fuel sending units and the gas caps) when I drained them, gas wasn't a problem, at least not a fuel problem in the lines before the fuel selector. In the hot weather, fuel may have boiled and vapor locked after the fuel selector, but would that allow some fuel to get through for 2200 RPM? I don't know. Fuel is gas AND air. Maybe something had clogged the air filter. Carb heat is also alternate air, so that would solve that problem. But carb heat only partially restored power, so that didn't make sense. I've heard of labels falling off the carb intake box and clogging the air intake, but this is something I can't fix in flight, and if such a label blocked the air intake, carb heat would cause a reduction in RPM, not an increase. At about 4000 MSL, I called Center and asked for an 800 number to call on the ground. They gave me a number to call collect, and I told them to send out a search team if I didn't call in 15 minutes. I was directly over the airport spiraling down, but I wasn't going to take any chances. They gave me squawk VFR and good luck. At about 3000 MSL, the engine magically started running normally again. I continued down for a normal landing, and was able to radio Center from the ground to tell them I was safe. On the ground, I checked oil, plugs (externally), fuel, oil cap, and everything seemed normal. I drained normal fuel samples from both sides. I couldn't find anything wrong. After some hemming and hawing, I decided to depart back to WVI. Coalinga has two runways in a T, so I did several runups and fast taxis on the runway, and then set up for short field takeoff into the wind. At any point I was in a position to land on a runway if the engine failed. I circled the airport all the way to 10,500 feet, and then headed to King City KIC. Based on the distances and 0 winds aloft, the GPS showed I could glide to either airport at any point of the trip. I continued uneventfully over KIC, Clark (pvt), Chalone Vineyard (pvt), Salinas (SNS), and then cut power at a point that would put me 1000 feet over WVI. I cleared the throttle occasionally, and the glide was perfect. I landed right on the numbers and taxied in. I had 6 gallons of gas left (I kept the plane light for better climb). I tied down the plane, and then pulled the cowling off. The left exhust pipe had come loose from the friction fitting. The pipe clamp was installed wrong, and I rotated it 90 degrees and put the pin through the pipe and outlet. Problem easily solved. The problem happened when the pipe was improperly re-installed after the annual. I had thought it was a stuck exhaust valve, but to prevent future valve problems: I won't allow the oil temperature to get close to redline. I will use 15W-40 multi-viscosity oil instead of 40 weight. I will use lower lead fuels (80/87) and get an autogas STC. I will not lean as aggressively. 100 to 150 rich of peak. I will get an STC oil cooler to keep the temperatures down.