NEW YORK, April 23 (Reuters) - Skyrocketing lumber prices threaten to thwart the momentum of the U.S. housing market, which for months has been one of the brightest stars of the recovery from the pandemic recession.

At the onset of the health crisis, "the mills stopped producing," said Dustin Jalbert, senior economist and lumber industry specialist at Fastmarkets in Burlington, Massachusetts. "As soon as they saw 20 million unemployed, they shut down production."

But COVID turned out to be a boon for the sector. Demand for low population density and home office space spiked, with historically low mortgage rates acting as an accelerant.

Surging demand pushed housing inventories to record lows. Homebuilders got to work, and lumber producers have struggled to catch up, which might take some time. Meanwhile, lumber prices have jumped more than 300% year-on-year to record highs.

"The logging operation, the shipping of the logs to the mill, the shipping of the finished product, getting workers back on the job, it's not like flipping a switch to bring those back online," Jalbert said.

With lumber prices sky high and a slim supply of housing stock, median home prices of existing homes jumped by a record-breaking 17.2% last month.

The chart below shows lumber prices and inventories over the last two years, along with various housing indicators home prices, residential construction spending, building permits and homebuilder sentiment:

While homebuilder sentiment remains optimistic, as indicated by the National Association of Homebuilders' (NAHB) Housing Market index, headwinds due to rising building costs have pulled the index down from recent highs.

"The supply chain for residential construction is tight, particularly regarding the cost and availability of lumber, appliances, and other building materials," wrote Robert Dietz, chief economist at NAHB.

On Thursday, homebuilder DR Horton Inc reported its quarterly profit nearly doubled, and said on its earnings call that lumber prices might see some relief as mills re-open and the international trade picture improves.

Pultegroup is due to report on April 27, while Lennar Corp and Toll Brothers Inc are expected to post results on May 5 and 25, respectively.

In the meantime, "the builders' margins have done fine," Jalbert added. "What does that tell us? The costs are being passed on to the homebuyer."

Whether those costs will dampen demand is an open question, but a report from the Commerce Department released on Thursday showed sales of newly constructed U.S. homes jumped by 20.7% last month to an annualized rate not seen since 2006, the zenith of the housing bubble.

The stock market appears to be looking beyond current price surges.

The S&P 1500 Homebuilding index and the Philadelphia SE Housing index, which also includes home improvement retailers and building supply firms, have handily outperformed the broader S&P 500 over the last 12 months, as seen in the chart below:

In the short term, mills are moving toward meeting the demand boom.

"The lumber industry is going to hit production capacity this summer, but things will calm down in 2022," Jalbert says.

While lumber prices should come back to earth, headwinds remain. Resources remain constrained and Washington has to conduct trade negotiations with Canada, which provides the United States with about 30% of its lumber, according to Jalbert.

"There's going to need to be more investment in the industry to meet this demand."

(Reporting by Stephen Culp; Editing by Alden Bentley and David Gregorio)