When he announced his retirement in April, House Speaker Paul D. Ryan pledged to “run through the tape” and remain in his post through the midterm elections.

Three months later, that sprint looks more like a slog: President Trump continues to upend the GOP on a near-daily basis, while both conservative and moderate lawmakers have caused Ryan headaches as new legislative and political pitfalls loom.

Still, Ryan (R-Wis.) has defied GOP colleagues who predicted it would be impossible for a lame-duck speaker to remain in power for months with control of the House on the line in November.

Interviews with numerous Republicans this week suggested that even the speaker’s critics have been placated after he maintained a brisk fundraising pace, deftly steered them through internal political minefields, and largely kept a lid on a potentially messy and distracting contest to choose his successor.

What he hasn’t done is make concrete progress overhauling immigration or mandatory programs such as Social Security and Medicare — key priorities he has pursued during his 20-year congressional career — or pushing back on Trump’s trade and foreign-policy moves. Instead, Ryan’s final months in office have been consumed with trying to keep his party in position to keep their majority.

House Speaker Paul D. Ryan (R-Wis.) listens as another Republican lawmaker speaks to reporters at the Capitol in Washington on July 17. (Pete Marovich/EPA-EFE/Shutterstock)

Veteran Rep. Frank D. Lucas (R-Okla.) said he can read the frustration on Ryan’s face in closed-door conference meetings.

“He’s not, I believe, enjoying his job. He’s not enjoying the circumstances,” he said. But, Lucas added: “He cannot leave until this issue of who succeeds him is settled. He has too strong a sense of responsibility to the conference, and to the House as a whole, to just step away and create chaos.”

Since April, Ryan has made only a handful of public appearances off Capitol Hill in friendly arenas carefully chosen to highlight signature issues. He was on comfortable turf Thursday bantering about conservative principles at the American Enterprise Institute, a prominent think tank soon to have a leadership vacancy that Ryan has been widely rumored as a top candidate to fill.

Ryan briefly spoke of “work to do” as the GOP has resisted Trump on trade, another example of his two years of sidelong criticism of a president who has taken a jackhammer to the bedrock of the Republican Party.

But Ryan has continued to shy away from publicly confronting Trump. After the president’s Helsinki summit with Russian President Vladi­mir Putin, which ended in a news conference that shocked many fellow Republicans, Ryan issued a tough statement backing intelligence officials and declaring that Trump “must appreciate that Russia is not our ally.”

At a news conference this week, he declined an opportunity to criticize Trump directly. Ryan has said he generally did not believe in having public clashes with Trump: “I do find that having good, strong private exchanges are far more effective than going out and wailing on TV.”

That posture is in keeping with the wishes of most of the Republicans who elected him speaker — who see their constituents standing squarely behind Trump and are wary of challenging him. And, since announcing his retirement, Ryan has continued to abide by the political demands of his rank and file rather than chart a more independent course.

That dynamic was on display in May and June. A renegade group of moderates threatened to force debate on a bipartisan immigration bill to decide the fate of young undocumented immigrants. It would have passed with mostly Democratic votes — an outcome that threatened to alienate the GOP’s conservative base even if it would have imposed policies that Ryan personally supports.

Ryan and other GOP leaders succeeded in short-circuiting the effort, setting up votes on conservative bills that did not pass, but allowing frustrated lawmakers to make the case to voters that they had attempted to address a key issue.

Rep. Tom Graves (R-Ga.), who had previously criticized Ryan’s decision to stay in office, said Republicans were now “in a good spot” and gave Ryan credit for it. He pointed to the relatively unsettled Democratic leadership landscape, thanks to last month’s primary loss of Rep. Joseph Crowley (D-N.Y.), the No. 4 Democrat, and the decision of more than a dozen Democratic candidates to break with House Minority Leader Nancy Pelosi (D-Calif.).

“He has not let up, and I applaud him for that,” Graves said. “Currently we’re watching the minority party implode and divide themselves, and we’re staying focused on our agenda and moving into the fall in strong fashion.”

Ryan has navigated House investigations into how law enforcement agencies and the intelligence community have handled inquiries involving Trump.

He has repeatedly backed committee chairs who have requested sensitive documents pertaining to those probes over the objections of administration officials, which have helped deliver ammunition for a conservative campaign to undermine the pending investigation into Trump’s orbit led by special counsel Robert S. Mueller III. But this month, as first reported by Politico and confirmed by Republicans familiar with his actions, he moved to forestall an effort to impeach Deputy Attorney General Rod J. Rosenstein, who oversees Mueller.

In both cases, Republicans familiar with Ryan’s thinking say, the imperatives have been to avoid distractions and “unforced errors.” A Rosenstein impeachment vote, for instance, would hand lawmakers a tough choice between pleasing conservative voters who oppose the Mueller probe and more moderate and independent voters who support it.

Said a GOP congressional aide: “It’s like, the kids are 18, they’re going to college. You’re just trying to keep them from crashing the car.”

Meanwhile, concerns that Republican fundraising would be hindered by having a lame duck shaking the money tree have dissipated. Ryan raised nearly $10 million in the past quarter, his political team announced last week, bringing his total for the election cycle to more than $70 million.

House Majority Leader Kevin McCarthy (R-Calif.) and Majority Whip Steve Scalise (R-La.), both jockeying to succeed Ryan, have also had record hauls for the quarter — $12.1 million and $2.5 million, respectively — and a leading Republican super PAC closely tied to Ryan, the Congressional Leadership Fund, is on track to shatter a $100 million fundraising goal. GOP strategists see those checks written by megadonors at leaders’ behest as crucial to offset record fundraising by Democratic candidates in battleground districts around the country.

The coming three months will pose legislative challenges as well as political ones. The House has to finish a massive farm bill that has opened divides between conservatives and moderates over the Supplemental Nutrition Assistance Program, also known as food stamps. Immigration remains a live issue that quickly could end up back in Congress’s lap if judges rule on Trump administration policies. And many Republican lawmakers hope to take votes on new tax and infrastructure bills that, while unlikely to pass, will give them fodder for their reelection campaigns.

But the biggest pitfall could be a Sept. 30 government funding deadline, which threatens to spark a shutdown a month before voters go to the polls. Trump has indicated he is determined to secure funding for his promised border wall, while Democrats have made it clear they will stand in the way unless the administration compromises on immigration and other policies.

At stake is the public’s perception of Republican governance, not to mention the smooth operations of government agencies and the military.

“We’re going to need some damn leadership on that,” said Rep. Scott W. Taylor (R-Va.), a freshman lawmaker who has called for a leadership overhaul. “I think he’s [Ryan] done a good job — not perfect. No one’s perfect. However, moving forward in the next month or two, we need to make sure that we shepherd that through.”

As the final chapter of Ryan’s congressional career is judged on what he avoids rather than what he accomplishes, he has started to look to his future.

For ethics reasons, Ryan said, he was “just going to wait until I’m done” to pursue a new job, reiterating that he no longer wanted to be a “weekend dad” to his teenage children. A person familiar with the AEI search said the group is unlikely to name a successor for outgoing President Arthur C. Brooks before next year.

Ryan declared he was not inclined to leave the arena: “The vocation of public policy, making a difference, is what I’m going to miss the most. . . . By no means,” he said, “am I going to retire.”