HONOLULU — The deaths of six U.S. soldiers in a suicide attack near Bagram Air Base in Afghanistan on Monday underscore the risks inherent in President Obama’s decision this year to keep as many as 9,800 troops in the country through much of next year.

A deeply conflicted Obama had warned when he extended the American troop presence in Afghanistan that he did not support “the idea of endless war.”

Now, a war that Obama had pledged to end before he left office is increasingly looking endless. That war followed him to his native Hawaii, where he is on a two-week vacation with his wife and daughters.

The six U.S. service members were killed and three others were wounded when a suicide attacker rammed an explosives-laden motorcycle into a joint NATO-Afghan patrol in Parwan province. The wounded included two U.S. troops and an Afghan. The Taliban claimed responsibility for the deadliest attack on international forces since August.

The incident occurred north of the capital, Kabul, about 1:30 p.m. in a province best known for Bagram Air Base, a sprawling military complex from which U.S. forces fly F-16 fighter jets and other aircraft. While the U.S. military no longer carries out offensive operations in Afghanistan following the end of its formal combat mission, it patrols around the base daily to protect it against the Taliban and other enemy groups.

“Our heartfelt sympathies go out to the families and friends of those affected in this tragic incident, especially during this holiday season,” U.S. Army Brig. Gen. William Shoffner, head of public affairs at NATO’s Resolute Support base in Kabul, said in a statement.

In New York City, Police Commissioner William Bratton said Monday that a New York City police detective, Joseph Lemm, was killed in the attack.

Lemm was a 15-year veteran of the New York Police Department and worked in the Bronx Warrant Squad. Bratton said Lemm served in the National Guard and, while a member of the police force, he had been deployed twice to Afghanistan and once to Iraq. He leaves behind a wife and three children.

Obama has spoken bluntly of the emotional toll that American military deaths have taken on him as he has dispatched troops to Iraq, Afghanistan and most recently Syria. Last month, he lashed out at critics urging him to do more militarily in Iraq and Syria, saying he wouldn’t send U.S. forces into combat just to look “tough.”

“Part of the reason is because every few months I go to Walter Reed, and I see a 25-year-old kid who’s paralyzed or has lost his limbs, and some of those are people I’ve ordered into battle,” Obama said. “And so I can’t afford to play some of the political games that others may.”

The remarks prompted a backlash from some Republicans, who accused Obama of politicizing American troops’ sacrifices.

The most recent deaths, coming just before Christmas and just a few months after Obama’s decision to extend the longest war in American history into his successor’s presidency, are likely to stick with the president.

He didn’t speak directly about the losses on Monday; instead, his press secretary, Josh Earnest, issued a statement.

“The United States condemns this cowardly attack on members of the U.S. and Afghan forces, and we remain committed to supporting the Afghan people and their government,” the statement said.

It was the deadliest attack on foreign troops in four months. On Aug. 22, three American contractors with the RS base were killed in a suicide attack in Kabul. On Aug. 7 and 8, Kabul was the scene of three insurgent attacks within 24 hours that left at least 35 people dead. One of the attacks, on a U.S. special operations forces base outside Kabul, killed one U.S. soldier and eight Afghan civilian contractors.

In the year since the international drawdown, the Taliban insurgency has intensified. Although the combat mission ended last year, around 9,800 U.S. troops and almost 4,000 NATO forces remain in Afghanistan. They have a mandate to “train, assist and advise” their Afghan counterparts, who are now effectively fighting a battle-hardened Taliban alone.

Monday’s attack came as Taliban fighters and government forces fought for control of a strategic district in the southern province of Helmand after it was overrun by insurgents, delivering a serious blow to the government’s thinly spread and exhausted forces.

Mohammad Jan Rasulyar, Helmand’s deputy governor, said insurgents took control of Sangin district late Sunday.

Rasulyar had taken the unusual step of alerting Afghan President Ashraf Ghani to the dire security situation and requesting urgent reinforcements through an open letter posted on Facebook on Sunday, saying that he had not been able to make contact through other means.

“We had to take to social media to reach you as Helmand is falling into the hands of the enemy and it requires your immediate attention,” Rasulyar wrote in his Facebook post to Ghani.

On Monday, Defense Ministry spokesman Dawlat Waziri said Afghan army commandos and special forces had arrived in Sangin to push a counteroffensive. He told reporters the Afghan air force had conducted 160 combat and transport flights over Sangin in the past 48 hours.

Helmand is an important Taliban base as it produces most of the world’s opium, a crop that helps fund the insurgency.

The deaths come at a particularly fraught moment in the long and often overshadowed Afghan war. In October, a U.S. warplane launched a barrage of strikes against a Doctors Without Borders hospital in the northern Afghan city of Kunduz, killing as many as 30 people and drawing widespread condemnation.

More recently, Taliban forces have made big gains throughout Helmand province, the site of some of the heaviest American casualties during the height of the U.S. troop presence. Afghan civilian casualties are at an all-time high for the 14-year war, according to the United Nations, and the Afghan government’s internal squabbling has prevented it from filling key positions, such as the defense ministry.

These were exactly the sort of problems that led Obama to abandon his initial pledge to end the American war in Afghanistan and bring home the vast majority of U.S. troops by the end of his presidency.

Under the current plan, Obama will keep about 9,800 troops in Afghanistan through much of 2016 and reduce the size of the American force to about 5,500 before he leaves office in 2017. There could, however, be heavy pressure from his successor to keep forces at the larger level.

Obama had hoped to leave office with only about 1,000 U.S. troops based at the U.S. Embassy complex in Kabul.

In October, he described the change in plans as a “modest but meaningful extension of our presence” that could make “a real difference” in Afghanistan and emphasized that U.S. troops would not be taking part in direct combat or patrolling Afghan villages. Instead Obama said they would focus on “two narrow but critical missions”: advising Afghan forces and launching targeted counterterrorism strikes against groups that threatened the United States or its allies.

The Taliban’s growing strength and the Afghan army’s struggles have drawn U.S. troops deeper into a combat role than many expected. In Kunduz, American special operations teams on the ground called in the mistaken strike that destroyed the Doctors Without Borders hospital. In Helmand province, special operations soldiers have been working alongside Afghan troops on the front lines.

The troops killed Monday outside Bagram Air Base were taking part in one of the core missions that Obama outlined for U.S. forces in October: They were patrolling the area outside of the sprawling base to push back insurgents and protect the facility and airfield from insurgent attacks.

In his announcement extending the U.S. presence, Obama acknowledged the inherent peril of the remaining work, even if he declined to call it combat.

“Afghanistan remains dangerous; 25 brave Americans have given their lives there this year,” Obama said in October. “I do not send you into harm’s way lightly. It’s the most solemn decision I make.”

Twice in response to a shouted question from a reporter, Obama insisted that his decision not to end the war as planned was not a “disappointment.

But the six deaths underscore Obama’s growing sense that he will leave office with one of his toughest and most important missions unfulfilled.

The Associated Press contributed to this report.