Massachusetts took a major step Thursday toward limiting the length of time homeless families can stay in the state's emergency shelters.

The House and Senate both passed a bill that would allocate another $251 million to the shelter network amid an influx of migrants and other families in need of a place to stay.

Lawmakers have described the situation as a humanitarian crisis, and the costs to keep the at-capacity shelters running have soared beyond the $325 million originally budgeted this fiscal year.

The final bill also authorizes Gov. Maura Healey to pull another $175 million from a surplus account to put toward the shelter system during the new fiscal year that begins in July.

The bill needs Healey's signature to become law. The governor has previously said she is “open to” shelter reforms.

Though Massachusetts law guarantees emergency housing to families with children and pregnant women, the bill would cap shelter stays at nine months starting June 1, with certain families eligible for up to two 90-day extensions.

Groups eligible for extensions include people with disabilities and medical conditions, caretakers for disabled family members, veterans, people at risk from domestic violence, people who are pregnant or who recently gave birth, and people with jobs or who are enrolled in job-training. Extensions would also be available if leaving shelter would disrupt a child's schooling, or for a parent or guardian who can't work because of insufficient child care.

The bill would also allow families to apply to state officials for a hardship waiver permitting additional time in shelter.

House Ways and Means Chairman Aaron Michlewitz said the cap is “the only viable way to keep the right-to-shelter system running,” absent help from the federal government.

Michlewitz, a North End Democrat, said the cap will still leave Massachusetts with “by far the most generous length of stay in the nation, with places like New York City and Chicago ... measuring caps in days, not months.”

House Minority Leader Brad Jones, a North Reading Republican, countered that policymakers could “lament the fact that we have the most generous situation in the country, because we’re paying the price for it.”

Kelly Turley of the Massachusetts Coalition for the Homeless said she appreciated the extra infusion of shelter funds from lawmakers, with the state’s existing pool of money set to soon run out. But she said she worries the extension and hardship waiver process will create “a lot of hoops for families to jump through in order to maintain shelter benefits.”

“I think there’s a sense that the state doesn’t want to kick families out, put them on the street when they haven’t found long-term affordable housing. And I believe that,” Turley told GBH News. “But at the same time, the law will be that we can do that, and so it’s hard to say what will happen once families run out of extensions and hardship waiver time.”

All 29 Republicans in the Legislature voted against the bill, as did five Democratic senators, 10 House Democrats and Beacon Hill's one independent lawmaker, Rep. Susannah Whipps.

The House this week is also debating a $58 billion annual budget for next fiscal year. GOP representatives have sought further shelter reforms through that bill, and Senate Minority Leader Bruce Tarr said he expects that push to continue when his chamber takes up the budget next month.

Tarr said the bill passed Thursday represents an acknowledgement that the right-to-shelter law needs changing. Republicans have so far met Democratic resistance in their pushes to limit shelter eligibility to Massachusetts residents or otherwise rework the existing law.

“Now the argument isn't about whether or not it needs to be changed,” Tarr said. “The argument is about how to best pursue the changes, and I think that's a significant step.”

Aside from the shelter provisions, the bill also makes permanent a pair of restaurant programs that originated in the early days of the COVID-19 pandemic, streamlining the permitting process for outdoor dining and allowing takeout cocktail sales with food orders. The original drinks-to-go program had also allowed beer and wine sales, but it will be limited to mixed drinks only moving forward.