Is it when you try to hold a constant speed? 2-stroke motors are made to accelerate, holding at a non-accelerating RPM without enough load to keep the engine cycling smoothley will make the motor 4 stroke, kind of mis-fire. It sounds to me from the little description provided this is what you may have going on, but I'm guessing your running a little lean to get more of an exagerated "surge". I'd get a can of starting fluid & spray around your motor to check for an air leak, pull the ignition cover when you do this so you can get a good spray around the main seal, spray around the carb boot too. If the idle goes up at all you have an air leak.
The powervalve thought is a good one as well though, on all 89-04 kx500's they leave the powervalves cracked slightly to help act as a decompression for easier starting. When you let of the throttle it causes a lean surge, because the open powervalves are over scavenging the engine. If you modify the powervalves so they close all the way the surge goes away.
Check for an external air leak first though to be safe. Then you might just need to service dirty powervalves particularly if you run a cheap mix oil. My experience with modern ignitions is they work or they dont, maybe a loose ground now & then but where its throttle related & not just plain cutting out or no fire at all....
One more whats your jetting & elevation? Dont just say stock either there's numbers on the pilot & main