A man walked into his estranged wife’s elementary school classroom in San Bernardino and opened fire without saying a word, killing her and an 8-year-old student before shooting himself in a murder-suicide that spread panic across a city still recovering emotionally from a terror attack just 15 months ago.

A 9-year-old student also was critically wounded. He and the boy who died were behind their special-education teacher, Elaine Smith (53), the target of the man she had married months earlier, police said.

The shooting left hundreds of distraught parents waiting for hours to reunite with their children.

School staff knew Cedric Anderson, who had been estranged from his wife for about a month, and he got into the school by saying he had to drop something off for Ms. Smith, officials said.

“No one has come forward to say they saw this coming,” police Chief Jarrod Burguan told reporters.

Anderson had a history of weapons, domestic violence and possible drug charges that pre-dated the short marriage, authorities said.

Fifteen students ranging from first to fourth grade were in the special-education classroom at North Park School, along with two adult aides and Ms. Smith, when Anderson emptied a large-calibre revolver and reloaded. Then, he turned the gun on himself.

Jonathan Martinez, the 8-year-old, was airlifted to a hospital and died soon after arriving, Mr. Burguan said. The 9-year-old boy, whose name was not released, has been stabilised at a hospital.

The 600 other students at the school were bused to safety at California State University’s San Bernardino campus, several miles away, after many walked off campus hand-in-hand, escorted by police.

Panicked parents had to wait hours before being reunited with them at a nearby high school.

San Bernardino, a city of 2,16,000 people about 60 miles east of Los Angeles, was the site of a December 2015 terror attack that killed 14 people and wounded 22 others at a meeting of San Bernardino County employees. Husband-and-wife shooters Syed Rizwan Farook and Tashfeen Malik were later killed in a gunbattle with authorities.

Once a major rail hub and citrus producer, San Bernardino filed for bankruptcy in 2012 after struggling to pay its employees despite steep cuts to the budget.

An outlying suburb of Los Angeles, it was hit hard when the Great Recession sent housing prices tumbling. As the city struggled with economic problems that forced layoffs of police and other government workers, violent crime, particularly homicides, began to rise.

In the past year, however, the city seemed to be making a recovery. Burguan, who won national praise for the way his department responded to the 2015 attack, announced last year he was hiring additional officers.