AMONG THE unfortunate consequences of Congress’s failure to come up with a permanent replacement for the Federal National Mortgage Association and the Federal Home Loan Mortgage Corporation, none carried more mischievous potential than the long-running attempt by hedge funds to gain control of the companies by court order — and make a taxpayer-subsidized killing in the process. To its great credit, however, the U.S. Court of Appeals for the D.C. Circuit has delivered what we can only hope is the final judicial blow to this opportunistic lawsuit.

To review: As Fannie Mae and Freddie Mac headed toward bankruptcy amid the housing meltdown of 2008, their federal regulator took them over pursuant to fresh legal authority from Congress. Over the next several years, the U.S. treasury, in effect, advanced the twins $187 billion to prevent them from collapsing and triggering what could have been a global financial catastrophe. In 2012, the regulator and Treasury rewrote the takeover to make sure that all future profits would accrue to the government, not the speculators who had meanwhile snapped up beaten-down Fannie Mae and Freddie Mac stock at pennies a share. This reflected the Obama administration’s reasonable view that bottom-feeding hedge funds should not reap a windfall from a recovery that massive investment by taxpayers made possible.

Amid much high-minded rhetoric about property rights and the rule of law, the funds took their case to federal court, arguing that Fannie Mae and Freddie Mac’s regulator lacked specific statutory authority to carry out the 2012 profit sweep. Proving that legal costs are no object in pursuit of a multibillion-dollar potential payoff, the hedge funds persisted in their lawsuit even after an adverse ruling by Judge Royce H. Lamberth of the U.S. District Court in Washington two and a half years ago.

It was that appeal whose most consequential argument the D.C. Circuit dismissed last Tuesday , in an opinion written by Judge Patricia Millett and Senior Judge Douglas H. Ginsburg that basically confirmed Judge Lamberth’s reasoning. Key to the case, they wrote, was Congress’s instruction, in the law establishing Fannie Mae-Freddie Mac ’s regulator, that “no court may take any action to restrain or affect the exercise of [its] powers or functions” over the distressed entities. A dissenting judge, Janice Rogers Brown, objected that this rule should not apply since Fannie Mae and Freddie Mac’s regulator exceeded its authority by turning over all the profits to the Treasury in the first place. This, she wrote, was the act of “a banana republic.”

What that hyperbole overlooks is the fact that there might not have been a republic — or at least an economy — left to argue about if the agency had not rescued Fannie Mae and Freddie Mac. Second-guessing that at the behest of speculators would be the height of injustice. Still, Judge Brown has half a point: The “extraordinary” situation at the two entities has gone on too long.

The chances that Congress will finally bring a fair and reasonable end to this situation may be better now that the D.C. Circuit has ruled against the one thing worse than endless limbo at the mortgage-finance giants — turning them over for a pittance to Wall Street gamblers.