In a stunning action Thursday, a federal appellate court gave the NYPD a much-needed reprieve from internal chaos. It slammed the brakes on last summer's ham-handed stop-and-frisk order and -- in remarkably blunt language -- yanked District Court Judge Shira Scheindlin off the case.

This is an extraordinary break for all New Yorkers. The appellate court didn't address the policy's constitutionality; it scheduled arguments for early next year. And that should give the next mayor -- in all likelihood, Bill de Blasio -- a welcome chance to make a deal.

Best case: The new mayor could tell the new judge that the city will voluntarily fix constitutional problems in the NYPD's embattled stop-and-frisk program, provided that the court allows the city to run the nation's largest police department without the intrusion of a federal overseer.

The overseer position Scheindlin created to ride herd on reforms threatened to split the NYPD leadership and saddle police with excessive paperwork and other issues.

The timing is perfect for a change in course.

Mayor Michael Bloomberg and the other stop-and-frisk die-hards in his inner circle who failed to see some excesses will be gone by the time the case reappears in district court in early 2014. In all likelihood the NYPD will have a new commissioner then, too.

And assuming de Blasio wins next week's election, City Hall and 1 Police Plaza should be far less angry about demands they that design a better stop-and-frisk protocol which appears less random and more concerned with strict constitutional principles.

As for Scheindlin, the appellate panel had nothing good to say. Through media interviews, and from the bench, she compromised the appearance of impartiality, it said. In the end, the remedy she imposed on the NYPD exceeded the offense. As the original case went on, Bloomberg and Police Commissioner Ray Kelly came across as stubborn and irritated in defending the practice. That was unfortunate. But the NYPD shouldn't have been wrenched from their hands.

The next mayor has a chance to repair this mess.