An audit of the San Francisco Housing Authority has uncovered a deficit that could reach as high as $30 million, a shortfall city officials say they are scrambling to cover by December to protect thousands of low-income families who would face displacement if the funding gap is not filled.

The deficit was discovered last week during an audit conducted by the accounting firm BDO and a “quality assurance team” from the federal Department of Housing and Urban Development, said Kate Hartley, director of the Mayor’s Office Of Housing and Community Development.

Hartley said HUD had informed the city in September that the Housing Authority had a shortfall, but the amount — between $20 million and $30 million — was much higher than expected. It is common for HUD to cover shortfalls for local housing authorities at the end of the year, but typically the amount is in the hundreds of thousands of dollars, not the tens of millions.

Hartley said the Housing Authority would be able to contribute some of the needed money, about $7 million, by reallocating funds intended for other uses. She said that HUD could also help and that the city would be responsible for the rest. In addition to covering the deficit, the city will take steps to tighten up and better monitor the Housing Authority’s finances, Hartley said.

“We are working with HUD and the Housing Authority to make significant changes so this kind of episode stops happening,” said Hartley.

“This is a serious situation, but fortunately the city has the resources to protect its residents and the project owners, and that is what we are going to do,” Hartley said.

The San Francisco Housing Authority administers about 11,000 Section 8 vouchers, rental subsidies that for October totaled about $23 million, according to Barbara Smith, the agency’s acting director. The vouchers pay private landlords as well as the nonprofit housing organizations that have taken over most of the city’s public housing during the past decade.

The problem is that, because of lax bookkeeping, the authority inaccurately thought it had more than $20 million in reserves. Smith said the Housing Authority was aggressive in spending that money to rehabilitate units needed to house homeless families in neighborhoods like Chinatown and the Tenderloin.

“It was all good stuff happening,” Hartley said. “There were no improprieties. We were doing all this great work and thinking it was funded because the Housing Authority had these reserves.”

Mayor London Breed, who was raised in public housing, said her “primary concern is keeping all of these tenants safe and secure in their housing, and the city will do everything necessary to keep them housed.”

But she added that there is “a major accountability crisis within the Housing Authority.”

“They can’t keep operating the same way, and by stepping in, the city can provide better oversight and management practices so we can better spend this funding and keep tenants protected in the long term,” Breed said.

The Housing Authority was underfunded and plagued with management and maintenance problems for decades, the result of federal budget cuts and poor local oversight. Tenants complained of decrepit plumbing, broken elevators, bug infestations — the situation was bad enough in 2013 that then-Mayor Ed Lee fired and replaced almost all of the Housing Authority commissioners.

Eventually, Lee worked with affordable-housing operators to take over the city’s public housing stock. That program, the Rental Assistance Demonstration, or RAD, resulted in the ownership of 3,500 public housing units in 29 buildings being transferred from the authority to nonprofits like Mercy Housing and the Tenderloin Neighborhood Development Corp.

The Housing Authority still owns the land the former public housing sits on, but no longer the buildings. In addition to administering the Section 8 voucher program, the Housing Authority still owns two affordable complexes, one on Potrero Hill and one in Sunnydale. Both of those are scheduled to be transferred to private groups next year.

In the short term, the bailout could delay rehabilitation work needed on housing units at Potrero Hill and Sunnydale, Hartley said.

“We are going to have to redeploy some of the funds we had on hold and use it for this,” she said.

It could also eventually delay some pre-construction funding for new affordable developments, although Hartley said that none of the projects set to break ground in this fiscal year would be stalled.

“We are acting fast to take care of this,” said Hartley. “The good news is that we are taking care of it and can take care of it. In some cities in this kind of situation, people lose their vouchers and get evicted. That is not going to happen here.”

Tenderloin Neighborhood Development Corp. Executive Director Don Falk said the “Housing Authority plays a crucial role in affordable housing for low-income people” as well as the construction and financing of new affordable housing.

He said he was “aware the housing authority was working through some struggles, but I was surprised how bad” the deficit was.

“That everyone pull together to resolve this problem is important for the whole San Francisco community,” he said.

J.K. Dineen is a San Francisco Chronicle staff writer. Email: jdineen@sfchronicle.com Twitter: @sfjkdineen