The North Korean government released this undated photo showing what it said was the test launch of an intermediate range missile. (AP)

The stock market on Thursday celebrated the 30th anniversary of the 1987 crash — wherein the Dow Jones industrial average declined 23 percent in a day — by smashing the gates of Dow 23,000 on a seemingly relentless march upward.

As of the market close Thursday, the Dow is up 17 percent this year and a whopping 21,000-plus points — more than 1,200 percent — since Black Monday in 1987. It’s up 260 percent since its market low during the Great Recession. It is up nearly 5,000 points, more than 25 percent, since President Trump’s election Nov. 8, 2016.

But happy times don’t last forever. This bull market, too, will end. And we recall that some of the biggest blowups besides ’87 — 1929, 2008 — have also struck right around this time of year.

So investors are nervous. And the great question driving pundits, policy wonks and Wall Street money types is: How will this one end?

The difference this time is that the fuse may come not from the economy but from somewhere else.

Aside from whatever the unpredictable U.S. president might do, the North Korean dictator (whom Trump calls “Rocket Man”) is hurling hydrogen bomb threats. The Iran nuclear deal is in play. There is the ever-present Middle East conflict. China. More Middle East conflict. Brexit. Foreign and domestic terrorism. Ukraine. Venezuela. Pick your poison.

Barron’s weekly investment magazine recently polled 140 money managers, and one of the multiple-choice questions was, “What is the biggest threat that the U.S. stock market will face in the next 12 months?”

“I answered geopolitics. Everything else is going swimmingly,” said David Hartzell, who manages $50 million as president of Cornell Capital Management near Buffalo. “One of the problems is the new team in Washington. You have a president with very little political experience, and a lot of his Cabinet has very little political experience. And that’s dangerous.”

Twenty-nine percent, by far the biggest chunk, agreed with Hartzell in the Barron’s poll. That was more than twice the percentage that chose any of the economic options, such as slowdown/recession, stalled fiscal policy and so on.

“In terms of the fundamentals, this far into a recovery, they still seem pretty good,” said Scott Baker, a Northwestern University professor who has studied the effects of uncertainty.

“Inflation is still low. Job numbers are good. Productivity is pretty good. Wage growth is picking up maybe a little. So if you think there is one thing that could derail the recovery, then you think of unexpected political events. Brexit. North Korea. Northern Iraq. Russia. Ukraine. Terrorism.

“Each of these maybe has a slightly higher risk than normal of exploding into something bigger. All these things could fizzle, but there is a possibility than any one of them could explode,” Baker said.

Rising stock prices in the defense sector provide more evidence that investors are uneasy about world affairs.

“Defense stocks are currently at a 35 percent premium [on a price-to-earnings adjusted basis] to the overall market,” said Carter Copeland, the lead aerospace and defense analyst at Melius Research. That is even higher than defense stock prices following Sept. 11, 2001.

“The common refrain we hear from investors on defense is something like, ‘I’m not gonna bet on peace breaking out,’ ” Copeland’s analyst team wrote in an Oct. 3 research report.

Investors can’t get enough of these shares. Inflows into defense and aerospace exchange-traded funds are up 73 percent this year, compared with a 5 percent decline in the industrial sector.

“You can tie a lot of this to Korea,” Copeland said, also pointing to signs of unrest in Saudi Arabia and Qatar, as well as the ever-evolving relations between the United States and Iran.

Copeland said there is a direct correlation between rising defense stocks and the “back and forth” between Trump and North Korean leader Kim Jong Un, especially since North Korea launched a missile over Japan.

Other safe-haven stocks such as utilities and consumer staples have not kept up with the defense sector.

“Defense has maintained this position as the ultimate safe haven because the geopolitical risk spectrum has presented a new thing to worry about,” he said.

Copeland echoed the views of several other finance experts who said that geopolitics trumps most other concerns because it is one place that cries uncertainty at the moment.

“There’s quite a bit of data that global macroeconomic conditions are okay,” Copeland said. “Global economies are synchronized for the first time in several years. It’s tough to point to emerging markets, oil or rising interest rates. So what do I worry about? I worry about what I see a lot, which is the president worrying about ‘Rocket Man.’ ”

Some strategists compartmentalize geopolitical risk so it does not interfere with their investing calculus.

“Does North Korea worry me as an individual? Yes,” said Jon Mackay, an investment strategist at Schroders. “Should it worry me as an investor? No.”

He continued: “Geopolitics has always been a factor since markets began. You can model cash flows. You can model inflation. You can model economic growth. You cannot model geopolitics.

“What’s going to happen in geopolitics is going to happen. Don’t get caught up in it. Be aware of it, but don’t let it drive your investment decisions.”

Instead, he devotes his attention to the economic factors that affect corporate earnings.

“Over the long term, what drives the market up is earnings growth,” Mackay said. “What drives earnings growth is economic growth. If the economy treads water, trades sideways or goes down, that’s going to drive earnings growth down.”

Iman Brivanlou runs the high-income equities group for TCW, a Los Angeles-based money management firm. He is in Mackay’s camp when it comes to worrying about international events versus the economy.

“You have this elevated tension between us and North Korea,” Brivanlou said. “We are mindful of that. But the bigger concerns are an aging credit cycle and [equity] valuation levels that are near all-time highs. I would categorize both of those as more worrisome than geopolitics. Geopolitics has been something present with us on and off since I’ve had a career.

“Perhaps someone could argue that [geopolitical risk is] a little more elevated with Trump in power and North Korea approaching nuclear weapons. But things like signs of weakness and cracks in the credit market are atypical. They haven’t been with us all along. They tend to be good predictors to market returns.”

Despite the daily tweets, name-calling and musical chairs in the Trump administration, there is no denying that the stock market has surged since Trump’s election.

“He is definitely making the process of policy less certain. There are a lot more discrepancies between what the Senate and policymakers and Congress say and what he says,” Baker said.

“To the extent Wall Street may be pricing in a higher possibility of a break in corporate tax rates, that’s probably helping the market, depending on how you translate his statements and tweets into likely policy.”