U.S. stock benchmarks on Tuesday staged a recovery from heavy selling earlier in the session that came after a North Korean missile test over Japanese airspace rattled investors and sent Wall Street trawling for assets perceived as safe.

The Dow Jones Industrial Average DJIA, +0.51% rose 56.97 points, or 0.3%, to close at 21,865.37. Blue chips had been off by as much as 135 points, or 0.6%, at its session low. A rise by shares of Boeing Co. BA, +0.28% and United Technologies Corp. US:UTX helped power the rebound.

The S&P 500 index SPX, +1.05% ticked 2.06 points, or 0.1%, higher to 2,446.30. The broad-market gauge had been down by about 16 points, or 0.7%, at its low.

Meanwhile, the Nasdaq Composite Index COMP, +1.71% advanced 18.87 points, or 0.3%, to close at 6,301.89.

U.S. equity futures sold off late Monday, after North Korea launched a ballistic missile over Japan, seen as another direct provocation that could destabilize the region, but staged a turnaround in late-afternoon trade.

Japanese Prime Minister Shinzo Abe called the missile test an “unprecedented, grave and serious threat that seriously damages peace and security in the region.” President Donald Trump has previously said the U.S. would react with “fire and fury” if Pyongyang stepped up threats against the U.S. and its allies. On Tuesday morning, he said that “all options are on the table” following the North Korean missile launch.

Some traders told MarketWatch that the “measured” statement by Trump and the rest of the global community so far has provided a modicum of comfort to investors. “The response around the world, it was measured as of now, and I think it was probably helpful to the market,” said Mark Kepner, managing director of sales and trading at Themis Trading.

Kepner said the market wasn’t being dismissive of the latest military threat out of Pyongyang but said seasonally low volumes for the summer also make equity benchmarks more prone to swings.

“North Korea is drawing another line in the sand, a little further out than the last line. This has real psychological ramifications for the markets; this might not end in a pretty fashion,” said Tim Ghriskey, chief investment officer of Solaris Group.

Kristina Hooper, global market strategist at Invesco, said: “There’s some level of optimism in the market.” Hooper said there is hope that the tragedy playing out in the Houston area in the fallout of Hurricane Harvey will be used to establish common ground amid the tense relationship between Trump and congressional leaders on budget negotiations and the debt ceiling.

A lot will depend on the “ability of Congress and the president to find some common ground borne of their concern for victims of Hurricane Harvey,” Hooper said.

The up-and-down trading action comes at a time when major U.S. stock-market indexes are solidly higher for the year, and trading near record levels. The S&P is up more than 9% in 2017, and is less than 2 percentage points off all-time highs. Beyond geopolitical concerns, investors have also been worried about Wall Street valuations.

Read: Overseas stocks are still where the bargains are for U.S. investors

“Valuation is rarely the only factor behind a market selloff, but it can exacerbate a market downturn when other factors are present,” Ghriskey said. “We’re not concerned about the market at these levels, but the Korea situation could get out of hand, which is the last thing anyone wants from a human or a market perspective.”

Gold futures US:GCZ7 rose $3.60, or 0.3%, to settle at $1,318.90 an ounce, marking its highest settlement value since Sept. 29, but the contract ended off its highest levels. In other haven trading, the yield on 10-year U.S. Treasury notes TMUBMUSD10Y, 0.674% fell 2.7 basis points to 2.13%, off its lows of the session.

See also: Gold’s 2016 peak now looks easy to reach after the metal’s big breakout

Stocks in Europe and Asia were also hit, with most benchmarks mired in red. Stocks in Europe suffered the biggest fall, with the Stoxx Europe 600 index SXXP, +0.20% closing down 1%.

Meanwhile, the ICE Dollar Index DXY, -0.00% was flat at 92.24, coming off its lowest level since January 2015. The greenback recovered after an early downturn against the Japanese yen, trading at ¥109.42, compared with ¥109.26 late Monday in New York.

Read:Will Japanese yen remain a haven amid North Korea tensions?

Harvey pounds flooded Houston with rain

The dollar was trading flat amid uncertainty over Hurricane Harvey’s impact on the U.S. economy and future Federal Reserve rate decisions. The storm system is expected to make landfall again this week and add another 20 inches of rain, for an total of 50 inches in the Houston area.

Insurance companies were hurt by the storm, and the full extent of damage is still unknown. The SPDR S&P Insurance ETF KIE, -0.28% fell 0.5%, while the iShares U.S. Insurance ETF IAK, -0.46% ended off 0.4%. The PowerShares KBW Property & Casualty Insurance Portfolio KBWP, +0.81% finished 0.2% lower on the day. All three are down more than 1% thus far this week.

ReadCaroline Baum: No, hurricanes aren’t good for the economy

And see:Hurricane Harvey highlights biggest impact of shale-oil revolution

Gasoline prices jumped for a second day on Tuesday, with the September contract US:RBU7 rising 7.1 cents, or 4.2%, to $1.783 a gallon, the highest close for a front-month contract since July 31, 2015.

Stock movers: Shares of oil refiners were among biggest movers Tuesday, with trade volatile following the fallout from Harvey. The storm is estimated to have reduced refining capacity along the Texas Gulf Coast by more than 2 million barrels a day, which could ultimately help to lift margins, analysts said.

Shares of Marathon Petroleum Corp. MPC, -0.67% were down 2.1%, and Anadarko Petroleum Corp. US:APC lost 1.4%. The overall energy sector XLE, -1.03% was 0.1% lower as one of the biggest decliners of the day.

Acorda Therapeutics Inc. ACOR, +2.22% plunged 46% after the U.S. Food and Drug Administration rejected the company’s application for a drug to treat the symptoms of Parkinson’s disease.

J. Jill Inc. JILL, +1.22% tumbled about 16% after it reported its second-quarter results and gave an outlook, the midpoint of which was below analyst consensus expectations.

Shares of Finish Line Inc. US:FINL tumbled by 18.4% after the athletics-wear company late Monday issued a profit warning and approved a plan aimed at blocking any individual stockholder from owning more than 12.5% of the shares outstanding.

Best Buy Co Inc. BBY, +1.56% sank 12% despite reporting earnings that beat forecasts.

Economic news:The S&P/Case-Shiller 20-city index rose a seasonally adjusted 5.7% in the three-month period ending in June, compared with a year ago, the same rate of change as in May.

Consumer confidence strengthened in August, and remains just below a 16-year high. The modest recovery in stocks followed the release of the data.

—Sara Sjolin contributed to this article