This Saturday night, the crowd will gather on El Rio’s leafy ramshackle patio to celebrate the bar’s 41st birthday. There will be a Dolly Parton-themed jam session and impersonation contest, and awards for the best T-shirts inspired by the country music icon.

While El Rio’s birthday is always cause for celebration at the Mission Street night spot — its motto is “your dive” — this year’s event is extra special after the business faced the prospect of closure a few months ago.

Owner Dawn Huston learned that the building that houses El Rio was about to go on the market, and she worried that the new owners would not extend the bar’s lease.

“A few months ago, we had to face the possibility that El Rio wouldn’t exist anymore,” she said.

Now, not only does Huston have a new lease, but she has a new landlord, the nonprofit Mission Economic Development Agency, known as MEDA.

That’s because the city stepped in to buy the building for $8.6 million, part of its ongoing “small sites program.” On Wednesday, Mayor London Breed is expected to share the news of the purchase of 3156-3158 Mission St., which houses the historic LGBTQ dive bar known for its progressive politics and prolific schedule of fundraisers.

The city’s small sites program has financed the acquisition of 278 rental apartments in 34 buildings since 2014. All the buildings are filled with low-income residents in neighborhoods where gentrification is fueling rapid change. The properties are typically purchased with a loan and then transferred to a local nonprofit affordable housing group.

The building on Mission Street near Precita Avenue has eight units above El Rio filled with residents who might have faced displacement had a market-rate buyer come in and purchased the property, according to the city.

The small sites program tries to protect rent-controlled buildings from speculators who buy the buildings and sometimes use a variety of tools — ranging from buyouts to lengthy renovations — to get rid of low-rent paying tenants and replace them with higher-income professionals.

The average income of the residents in the Mission Street building is about $47,500 for an individual. Huston called the transfer of the property into MEDA’s hands “a colossal relief.”

“I can’t express enough just how immensely grateful we are,” she said.

So far, the city has committed $83 million for the acquisition and preservation programs, and over 500 San Franciscans have been stabilized to date. The city is in the process of buying another 12 buildings with 110 total units.

“We know the challenges that many bars and restaurants face to stay open, but it is places like El Rio that draw people together and make our communities and our city unique,” Breed said in a statement. “I'm proud that we have found a solution to support El Rio, as well as the tenants who live in the building.”

The El Rio building acquisition was financed with a loan from the San Francisco Housing Accelerator Fund, which draws on city money as well as participation from Dignity Health, Citi Community Development and the San Francisco Foundation. The fund has raised $109 million for the program. The loan for the El Rio building includes $800,000 for repairs and building upgrades.

El Rio opened in 1978 as a “leather Brazilian gay bar,” according to the history of the bar written by Huston. In addition to the multi-level patio, the bar is known for wooden paintings of Carmen Miranda and Marilyn Monroe, its indoor shuffleboard tables and backyard barbecues. On election night, it is typically a late-night gathering spot for candidates and campaign workers celebrating victories or comforting one another after losses.

El Rio is avoiding the fate of several other San Francisco LGBTQ establishments. Esta Noche on 16th Street, the city’s oldest Latino gay bar, closed in 2014. The Lexington Club, a lesbian bar, closed in 2015. In 2018, the Gangway on Larkin Street, the city’s oldest gay bar, closed after 108 years in business.

Supervisor Hillary Ronen, who represents the neighborhood, said she was happy “the most historic queer bar in my district is going to be around for decades to come.”

“I’m at El Rio all the time,” said Ronen. “It’s one of my go-to, favorite places in the city. There was no way we or the community was going to let that bar disappear and go the way of so many other beloved businesses.”

Huston said El Rio will continue to do what it’s always done — throw fundraisers and bring San Franciscans together. The bar holds over 200 fundraisers a year, sometimes more than one in a night.

“El Rio is a bar run like a nonprofit,” said Huston. “Our whole thing is to throw fundraisers that don’t feel like fundraisers. They just feel like parties. It’s always been that way.”

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