“Natasha, Pierre & the Great Comet of 1812” is in danger of closing within weeks, felled by a social-media backlash that hit the show when it was announced that Mandy Patinkin would replace a black actor in one of the the title roles.

Several production sources confirmed a closing was imminent. But Howard Kagan, the lead producer, would only say, “We are continuing to work around the clock to extend the life of this show.”

Patinkin withdrew from the production last week after several prominent actors of color tweeted their frustration. The “Homeland” star was set to replace Okieriete “Oak” Onaodowan, the former “Hamilton” actor, who joined the cast after J osh Groban left in July.

The Web site Broadway Black denounced Patinkin’s casting, saying it “raises questions about how Black actors are valued and supported within Broadway.”

‘We are devastated. This is a beautiful show, full of diversity on every level. To have this turned into something about race is completely unfair.’

The backlash caught the producers of “Great Comet” off guard. They maintain that their decision had nothing to with race, and that they were fighting to save their show by hiring a proven star. As good as Onaodowan was, he was not selling tickets. Sources say the advance had dwindled to $2 million — and the show costs about $700,000 a week to run. While “Comet” has never had an unprofitable week, projections showed that the musical would fall below the break-even point in the near future.

“Comet” has returned just 20 percent of its $14 million investment so far.

The closing would put some 35 members of one of the most racially diverse casts on Broadway out of work.

Company members were shocked — and furious — on Thursday night when they heard that the show might close in the coming weeks.

“We are devastated,” a production insider said. “This is a beautiful show, full of diversity on every level. To have this turned into something about race is completely unfair.”

Members of the company were also unhappy with Onaodowan. Several sources say he agreed to step aside for Patinkin, knowing the show needed help at the box office. A source says Onaodowan even agreed to do publicity with Patinkin and was prepared to say he would be happy to rejoin the show in the future.

“He was completely on board,” says a source. “We knew this could be tricky, and we didn’t want to do anything that would hurt him.”

But once the social-media storm erupted, Onaodowan stopped talking to the producers, sources say. They also say that he met privately in his dressing room last week with Rafael Casal, a performer who led the attack on the show.

“It just snowballed from there,” a production source says.

At press time, Onaodowan’s publicist had not returned an e-mail seeking comment.

Adding to Onaodowan’s frustration was what sources say was bad blood between him and the show’s director, Rachel Chavkin. They say the two clashed bitterly during rehearsals, often screaming at each other in front of company members.

“It got very ugly and very tense,” says one source. “Oak was clearly unhappy in the show.”

Chavkin declined to comment through the show’s press rep.