Canadian legislators have narrowly voted to extend the country's combat mission in Afghanistan by two years, until February 2009.
Prime Minister Harper fought hard to get his motion through
Prime Minister Stephen Harper's motion passed by 149 votes to 145, despite opposition complaints of being rushed.
Canada currently has 2,300 soldiers in Afghanistan, mainly in the south where the Taleban-led resistance is strong.
The vote came after news that a female Canadian soldier had been killed in combat in the war-torn country.
Public opinion polls suggest that popular backing for the deployment, which had been due to expire in February 2007, is slipping.
Mr Harper told legislators before the vote: "Our men and women need to know that we share their goals, support their efforts and are willing - regardless of polls that sometimes go up or down - to back them for the next few years."
Afterwards, the prime minister expressed relief that the vote had been approved.
The Conservative government had underlined its commitment to the mission by threatening to extend it unilaterally by one year if it had been defeated in parliament.
One by one the MPs took their turn to stand and be counted as either for or against the motion and the result was nail-bitingly close.
Two opposition parties, the Bloc Quebecois and the New Democrats voted against the motion, but it drew enough support from the former ruling Liberal Party to pass.
Canada may now take command of the Nato mission in Afghanistan
Many Liberal lawmakers complained that they were being rushed into a decision. They said the vote was hurried to shield the government from taking sole criticism for the mission if it goes wrong.
Growing dissent led to a decision by the Liberal leader, Bill Graham, to let his individual MPs vote freely - despite the fact that a Liberal government was originally responsible for sending the Canadian troops to Afghanistan. "We find this process abusive," said Mr Graham.
"We find it difficult in the course of a debate of a few hours in the House to make up our minds on an issue of this importance to Canada and Canadians and to our troops."
Death sharpens focus
The extension means Canada could take over command of the entire Nato operation in Afghanistan in 2008, which Mr Harper has offered to do.
Canadian ministers will be attending a series of Nato meetings next week.
The intense parliamentary debate was magnified by the news that a woman soldier, Capt Nichola Goddard, a female captain, had been killed in a gun battle with Taleban fighters - Canada's first woman soldier to die in combat since World War II.
She was killed in clashes some 25km (15 miles) west of the southern city of Kandahar, a centre for Taleban insurgents.
A roadside bomb killed four Canadian soldiers in April.