When Chicago Public Schools announced on June 24 that it would borrow $1 billion to make a $600 million-plus pension payment due June 30 an eerie feeling spread across bond investors and taxpayers alike.

It was the same feeling that gripped investors when Moody’s Investors Service downgraded Chicago’s credit rating to junk based almost entirely on the city’s pension problems.

The fear was that elevated pension costs, in cities like Chicago, might push these public entities into insolvency, wiping out much of the holdings of municipal-bond investors.

Once a sleepy corner of the municipal bond market — often not even properly reflected on cities’ balance sheets — public pensions have recently turned into the biggest headache for taxpayers and municipal-bond investors, threatening to bring down the finances of U.S. cities and states.

In some places, like Puerto Rico, Illinois, New Jersey and Chicago, entire balance sheets of cities or states hang in the balance.

Detroit, as well as three Californian cities — Vallejo, Stockton and San Bernardino — had to declare bankruptcy because of their overwhelming pension costs.

In those cases, the courtroom turned into a brutal battlefield pitting bond investors trying to save the money they invested in those cities’ municipal bonds on one side. And on the other side have been public employees trying to save the dwindling pensions that were promised to them.

Recent cases have shown that bond investors are clearly losing this battle.

In the bankruptcies of Detroit, Vallejo, Stockton and San Bernardino, bondholders have faced losses of up to 99% of their holdings, according to a Moody’s report dated May 18. Meanwhile all three California cities chose to preserve full pensions for their employees, while Detroit only cut pensions by approximately 18%.

Read:Puerto Rico poses bigger threat to U.S. investors than Greece

As the following chart shows, bond values have taken haircuts that far exceeded those of pension benefits:

Part of the reason bondholders have been taking it on the chin is the process of so-called “Chapter 9 bankruptcies.” A Chapter 9 is the type of bankruptcy in the Federal bankruptcy code regulating the bankruptcy of cities and other municipal governments.

The way Chapter 9 works, a city has to present an outline of its assets and liabilities to a bankruptcy court and propose a plan, known as a “plan of debt adjustment,” essentially saying how much it will pay each creditor, such as bondholders, pensioners and employees.

But unlike other bankruptcies, where creditors can also put forward plans — including the proposal to liquidate assets — in a Chapter 9 bankruptcy, the city council is in control of the process and the judge can only determine whether the plan is “fair and equitable,” explains Ty Schoback, a municipal bond analyst at Columbia Threadneedle Investments.

This practically means that once the bankruptcy begins, creditors find themselves “at the mercy of the city’s proposed treatment,” Schoback added.

Pension supremacy

Though some have pointed to political ties between unions and governing officials for the favorable treatment of pensions, the legal reality may be far more complex.

In many states, public pensions are protected by state constitutions or statutory law, and as a result are afforded many privileges, according to a Center for Retirement Research report.

In legal circles, this has come to be known as “pension supremacy” and it is a real headache for bond investors.

In Chicago, the state’s constitution dictates that pension benefits for current workers “shall not be diminished or impaired.” New York carries a similar clause, while Hawaii, Louisiana, and Michigan have constitutional provisions that have been interpreted as protecting all pension benefits earned to date.

Some Wall Street investors have become quite familiar with this so-called pension supremacy. In Stockton’s bankruptcy, mutual giant Franklin Templeton, suffered withering losses of up to 59% of its investments. And in San Bernardino, bond insurer Ambac Assurance Corp. and Luxemburg-based EEPK are facing losses of 99% of their holdings.

Most states have some legal type of pension protection, including the so-called “promissory estoppel” which is the protection of a promise even where there is no contract. Center for Retirement Research

In California, on the other hand, the law provides that if a public employee leaves a job with one municipality and gets a job with another within a set time period, the worker can retain her pension rights under the California Public Employees’ Retirement System (Calpers). This essentially means that if a worker lost his pension in Stockton’s bankruptcy, he could quit and get a job in another California city to save his pension rights.

Using this legislation, the bankrupt city of Stockton argued in court that if it cut pensions, it wouldn’t be able to retain its best employees, thus threatening its ability to provide essential services for residents, such as police and fire protection, said Mark Berman, a bankruptcy attorney at NixonPeabody who specializes in municipal bankruptcies.

Stockton convinced the judge. Even though he ruled that pensions could theoretically be impaired, he allowed the city to exit bankruptcy leaving pensions intact, while bond fund Franklin Templeton lost 59% of its holdings.

Pensions are underfunded all over the nation

Combine the legal supremacy of pensions with low funding levels all over the nation and you may have a recipe for a national crisis.

A report by the Center for Retirement Research that came out last week and surveyed 150 state and local pension plans showed that their average ratio of assets to liabilities was 74%. In other words, for every dollar those funds owe their pensioners, they only have 74 cents in assets.

The estimated ratio of assets to liabilities for a sample of 150 state and local pension plans was 74% in fiscal year 2014. Center for Retirement Research

If 74% sounds bad, consider this: Chicago’s six pension plans are 40% funded, New Jersey’s are at 51.5%, while Puerto Rico’s, which was facing its own crisis moment Monday, is woefully underfunded at 3%.

Because their pensions are underfunded, cities are forced to spend more of their payroll on pension contributions:

The increase in required pension contributions over the past several years began just as the recession eroded state and local government revenues. Center for Retirement Research

Pension obligation bonds

Many cities have turned to a special type of risky bonds called pension obligation bonds or POBs to fund their pensions without taking unpopular measures like raising taxes.

But these bonds only provide “short-term budget relief, a strategy to kick the can down the road and pass difficult choices on to future decision makers,” according to a Janney report dated May 1.

Oakland, Calif., was the first to sell $222 million of tax-exempt POBs back in 1985. Illinois issued $17.16 billion between 2003 and 2011, Puerto Rico issued $2.8 billion in 2008 and New Jersey issued $2.9 billion between 1997 and 2003.

Over the last 30 years state and local government issued pension obligation bonds to shore up the unfunded portion of their pension liabilities without raising taxes. Janney

In simple terms, POBs allow a city or state to borrow money to make its pension payments and issue bonds that will be repaid by future city revenues.

But here’s the problem:

“When you buy POBs, you’re exposing yourself to the pension fund and essentially lending money to leverage its portfolio,” said Kenneth Potts, a principal at Samson Capital Advisors who specializes in muni bonds.

In other words, there is neither a real asset backing these bonds, nor a specific revenue stream to guarantee repayment.

As the Moody’s report points out, “in essence, pension obligation bond is a misnomer because the bonds are simply a vehicle to fund pensions.”

So when push comes to shove and there are not enough funds to go around, a bankrupt city can choose to give the little money it has to its pension funds but not to its pension obligation bondholders.

The latest fiasco of this type happened this May, when the city of San Bernardino, Calif., offered to pay only 1% to its POB investors, while committing to pay 100% of its pension liability.

This showed the importance of “knowing what you own” in your bond portfolio and “what public revenues back your muni bonds”, Potts added.

Pension-obligation bonds are trading in the market and are still appealing to yield-hungry investors — including bond guru Jeff Gundlach who recommended Puerto Rico’s POBs for their rich yield during a private event in New York on May 5.

But with great rewards comes great risk — in this case the risk to be completely wiped out in case of a Chapter 9 bankruptcy.