TORONTO — A Canadian court on Wednesday threw out a long-standing deal that has allowed the country to send asylum seekers back to the United States, saying it violates their rights by exposing them to likely detention and possible removal on the U.S. side.

Under the Safe Third Country Agreement, which went into effect in 2004, Canada has been able to turn back asylum seekers attempting to enter from the United States at official crossings — and vice versa — because both countries recognized each other as safe places to seek refuge.

Justice Ann Marie McDonald wrote in her decision that “it is not the role of the Court to pass judgment on the U.S. asylum system.” But she said the applicants had provided “significant evidence of the risks and challenges” faced by asylum seekers who are returned to the United States.

“I have concluded that the actions of Canadian authorities in enforcing the [Safe Third Country Agreement] result in ineligible STCA claimants being imprisoned by U.S. authorities,” she wrote. “I have concluded that imprisonment and the attendant consequences are inconsistent with the spirit and objective of the STCA.”

McDonald said the treaty violated the rights to “life, liberty and security of the person” guaranteed by the Charter of Rights and Freedoms, part of Canada’s constitution. She suspended the ruling for six months to give Parliament time to respond.

The decision dealt a blow to Ottawa, which has long defended the pact as lawful and has recently sought U.S. help in tightening their shared border.

A spokeswoman for Public Safety Minister Bill Blair said the government was reviewing the ruling. The U.S. Department of Homeland Security did not respond to a request for comment.

The agreement has been under fire since the election of Donald Trump, who as president has sought to limit asylum, refugee resettlement and illegal immigration in the United States. The Trump administration has pursued safe-third-country agreements with several Central American countries.

The deal has also been at the center of intense debate in Canada because a loophole allows asylum seekers who cross the border at unauthorized points of entry to proceed into Canada and file their claims. More than 58,200 people have entered the country via such “irregular” crossings to claim asylum since Trump’s inauguration in 2017. The influx has created a backlog of 90,000 claims and left migrants stuck in bureaucratic limbo for years.

Canada has asked the United States to amend the treaty to make it easier to turn back those who enter at irregular crossings. When the countries closed their border to nonessential travel in March, they also agreed to bar asylum seekers at such crossings.

The case that was decided Wednesday was brought in 2017 by the Canadian Council of Refugees, the Canadian Council of Churches, Amnesty International and several individuals. They argued that the United States should not be considered a “safe” country for asylum seekers, because those who have been turned back have been detained and subjected to the risk of removal to their home countries and the persecution they were trying to flee.

The Canadian government argued the process included safeguards such as the ability for asylum claimants to seek judicial review of decisions made by border agents. The government argued that Canada’s Charter of Rights and Freedoms does not apply to U.S. law or actions and pointed out that most of the litigants were eventually released.

McDonald cited the experience of Nedeira Mustefa, a Muslim asylum seeker from Ethiopia, as particularly “compelling.” After Canada returned her to the United States, she was detained and placed in solitary confinement, where she believes she was fed pork despite having told guards she could not eat it, for religious reasons. She wrote in an affidavit that she “felt scared, alone and confused” and endured a “psychologically traumatic experience.”

“The evidence establishes that the conduct of Canadian officials in applying the provisions of the STCA will provoke certain, and known, reactions by U.S. officials,” McDonald wrote. “Canada cannot turn a blind eye to the consequences that befell Ms. Mustefa in its efforts to adhere to the STCA. The evidence clearly demonstrates that those returned to the U.S. by Canadian officials are detained as a penalty.”

Other applicants included a Muslim family from Syria who sought asylum in Canada after President Trump issued an executive order in early 2017 barring entry to refugees and citizens from seven majority-Muslim countries, and a woman from El Salvador and her children who said they faced rape and extortion threats from MS-13 gang members back home.

The fact that the detainees were released, McDonald wrote, “does not establish that there is a fair review process available.”

Lawyers for the asylum seekers argued that the agreement violated equality rights under the Charter of Rights and Freedoms because it disproportionately affected women. They cited a U.S. decision in 2018 to generally deny asylum claims pertaining to domestic violence or gang violence.

McDonald wrote that she would not rule on that argument, because she had already found the pact to contravene the charter.

The case is not the first time Canadians have challenged the pact. In 2007, advocacy groups successfully petitioned a court to declare the United States unsafe for refugees, but that decision was overturned on appeal.

The organizations that brought the legal challenge welcomed the ruling.

Alex Neve, secretary general of Amnesty International Canada, called on the Canadian government to revoke the order that closes the border to asylum seekers attempting to enter at irregular crossings during the pandemic.

“The Safe Third Country Agreement has been the source of grave human rights violations for many years, unequivocally confirmed in this ruling,” he said in a statement. “That cannot be allowed to continue one more day.”