Speaker Paul D. Ryan is losing his grip on the feuding House Republican conference just months before pivotal midterm elections, caught between dueling factions vying for power inside the party and facing scattered calls for his departure ahead of a planned year-end retirement.

The unrest comes in the wake of a humiliating defeat for Ryan and other GOP leaders last week, when conservatives sank a farm bill amid a broader dispute over immigration policy, and threatens to spark months of bitter infighting as Republican lawmakers try to make the case that they should be returned to power in Washington.

But there is no clear way out for the party. Numerous aides and lawmakers said Tuesday there is not a viable alternative to Ryan who can win enough support within the GOP for a clean transition before November — and there is little stomach at the moment for the messy battle that would ensue when Ryan departs.

“Whoever comes in is going to walk right into a buzz saw,” said Rep. David Joyce (R-Ohio), a Ryan supporter. “Who better than Paul, who came in under these circumstances, to continue to calm the waters?”

Ryan’s preferred successor is Majority Leader Kevin McCarthy (R-Calif.), who helped recruit dozens of sitting Republican lawmakers and enjoys a close relationship with President Trump. Two senior Republicans who spoke on the condition of anonymity to describe their view said it would be preferable if McCarthy could take the reins immediately and move to assert more control over the party’s legislating and fundraising.

Talk of a leadership change also got an unusual public airing this week when White House budget director Mick Mulvaney said at a Colorado Springs policy conference that he had spoken to McCarthy about the prospect of replacing Ryan before the midterms.

House Speaker Paul D. Ryan (R-Wis.) said on May 17 that a discharge petition to force debate on immigration bills would "guarantee that nothing goes into law." (Reuters)

Ryan’s hand has weakened in the legislative realm even as his party sees signs of optimism for November. Some new public polls have shown the Democratic advantage in congressional races eroding, while Republicans have taken heart as more liberal — and, in their view, beatable — candidates have emerged from some key Democratic primaries.

Still, the immigration fracas and Friday’s related farm bill defeat has unleashed raw expressions of frustration from Republicans who are befuddled that they have been unable to stay united while in full control of Congress and the White House.

“Last week, if I saw that stuff shaping up, I’d say, ‘You two, go in a room . . . go figure it out and don’t leave that [expletive] room until you come out with a solution,’” said Rep. Scott W. Taylor (R-Va.), who is in a competitive race for reelection.

He declined to blame Ryan for the infighting and said he was not certain whether electing another leader would get the GOP back on track before November: “I’m totally frustrated, but I’m not sure that’s all on him.”

In the Sunday remarks by Mulvaney, first reported Monday by the Weekly Standard, the former South Carolina congressman suggested that a House speaker vote could serve as a welcome referendum on Democratic House leader Nancy Pelosi (Calif.).

“I’ve talked with Kevin about this privately but not as much publicly,” Mulvaney said. “Wouldn’t it be great to force a Democrat running in a tight race to have to put up or shut up about voting for Nancy Pelosi eight weeks before an election? That’s a really, really good vote for us to force if we can figure out how to do it.”

But all indications are that a speaker vote would be much more divisive for Republicans than for Democrats, who are defending relatively few competitive House seats.

McCarthy faces persistent doubts from the GOP’s hard-right, who want more influence in the party leadership, if not a leadership spot of their own. And they are hardly eager to pave the way for his ascension to the speakership.

Rep. Mark Meadows (R-N.C.) — the House Freedom Caucus chairman who has frequently fomented drama in the GOP leadership ranks, to the point of sparking the resignation of former speaker John A. Boehner — sought instead Tuesday to tamp it down. “It’s reporter rhetoric,” he said of Ryan’s potential early exit. “It is not based on facts.”

Rep. Warren Davidson (R-Ohio), a Freedom Caucus member, told reporters Tuesday that it was “premature” to have leadership elections when dozens of new Republicans could be elected in November. Many conservatives, in the House and outside it, are promoting Rep. Jim Jordan (R-Ohio), a co-founder of the Freedom Caucus, as an alternative to McCarthy.

“I think the real question is, are we going to play offense while we’re on offense?” Davidson said. “We’ve got the ball right now. We’ve got things that we campaigned on and promises broadly we made.”

Lawmakers on the other end of the GOP spectrum are just as skeptical of a pre-election leadership scramble.

“If we have a speaker’s race, then it takes everyone’s eye off the ball of what’s most important, and that is keeping the majority,” said Rep. Rodney Davis (R-Ill.), who chairs the centrist Republican Main Street Caucus. “It would be the most short-lived time in the speaker’s chair that anyone could have asked for.”

Inside a closed-door meeting of Republican lawmakers Tuesday, according to attendees, Ryan expressed frustrations with the unraveling of the farm bill and argued that the political winds appeared to finally be blowing in the GOP’s direction. Sticking together, he told the group, is the only way to keep it going — prompting a standing ovation that could be heard in the hallways outside the room.

Afterward, Ryan told reporters that he was focused on passing legislation like the House farm bill, which includes a new policy favored by House conservatives that would require some food stamp recipients to look for work to receive benefits. He also twice noted that he had not sought the speakership, but rather had been drafted to run after Boehner’s resignation.

“Our members realize what we want to do is act on our agenda, improve people’s lives,” Ryan said. “And having a divisive leadership election at this time would prevent us from doing that.”

Multiple Republicans said this week that the only factor that could accelerate Ryan’s departure is intervention from Trump, who is caught between his friendship with McCarthy and conservative allies who want to force a race.

For now, the White House is not convinced that Ryan staying as speaker through the end of this term is a tenable situation, one senior White House official said. But the White House has made a concerted effort to stay out of the race to replace Ryan since he announced his retirement last month, believing there is no upside for Trump to weigh in on the matter.

Mulvaney did not consult the White House before making his remarks over the weekend, the official said. A spokeswoman for Mulvaney called the comments “purely hypothetical” and that he supports Ryan remaining as speaker.

White House press secretary Sarah Huckabee Sanders said Tuesday that it was up to Ryan and his fellow GOP lawmakers to determine whether he continues and “not something that the White House has weighed into at this point.”

The next front in the internal GOP battle will play out of the coming weeks as the immigration issue comes to a head. Twenty Republicans have signed a “discharge” petition to force a debate, which could come as soon as June 25, on a series of immigration bills — including some bills that Democrats support and conservatives hate. Its backers, which include Republicans in Democratic-friendly districts, said Tuesday that they expect to have enough signers by the end of the week.

To sidestep that possibility, top GOP leaders said this week that they plan to bring up immigration bills the week beforehand, but there are widespread doubts that they will be able to craft a bill that could pass the House with a majority of Republicans supporting it.

“I have not seen a bill at this point that can magically bring everybody together,” said Davis, who has been involved in meetings on the issue with Meadows and the leaders of other GOP factions.

Jordan, meanwhile, said “a little friendly persuasion” from Ryan and other leaders could produce a passable bill.

Several Republican lawmakers said Tuesday that there would be no discernible difference if McCarthy were in charge rather than Ryan and instead wished that the party could focus on passing more substantial bills into law before the midterms.

“We just want results,” said Rep. Joe Barton (R-Tex.), a veteran lawmaker who is retiring. “There is almost no scenario where we’re going to be in better shape next year — probably going to lose some seats in the House, maybe gain a Senate seat or two, maybe not. If we really want to accomplish things, there is no better time than right now.”

Paul Kane and Seung Min Kim contributed to this report.