After more than six hours — and a testy debate that escalated dramatically when unusual alliances formed between a few Democrats and a group of Tea Party-backed Republicans — the Texas Senate approved a measure loosening state restrictions on handguns Friday.

The legislation allowing Texans with licenses to carry handguns openly eventually passed on a final 20 to 11 vote.

But before it did, the chamber plunged into rare unscripted territory, as Democrats and Republicans battled members of their own parties over an amendment from state Sen. Don Huffines, R-Dallas, that would prohibit police officers from stopping someone solely because they are visibly carrying a handgun.

The provision, which eventually passed, attracted support from Democrats who said it would help prevent racial profiling and conservatives who said it was necessary to protect the Fourth Amendment rights against unreasonable search and seizure of handgun license holders.

“This should not be complicated; it should not be controversial. This is a bipartisan issue,” Huffines said.

Opponents of the provision called it nothing more than an effort to sneak in a repeal of licensing requirements altogether.

"This is just a back door to constitutional carry because really any person could just carry a gun without a license because they know the police can't inquire of them if they have a license," said state Sen. Joan Huffman, R-Houston, who unsuccessfully attempted to heighten penalties for carrying handguns without a license during the debate.

The amendment, approved overwhelmingly when the House passed the open carry bill, was taken out in committee when the legislation reached the Senate.

Democrats Rodney Ellis of Houston and Royce West of Dallas threw their support behind Huffines, saying that — while they oppose open carry itself — with the bill all but certain to pass, they wanted protection from racial profiling by police.

"I have a long history of opposing open carry legislation and a strong record of supporting sensible reforms to reduce gun violence and keep firearms out of the hands of criminals," Ellis said in a statement he released after the vote. "But if open carry is going to become law, law enforcement shouldn't be able to circumvent constitutional rights and detain someone solely because they are open carrying in compliance with the law."

Five other Democrats voted with Huffines: José Rodríguez of El Paso, Carlos Uresti of San Antonio, Eddie Lucio of Brownsville, Juan "Chuy" Hinojosa of McAllen and Sylvia Garcia of Houston.

Besides Huffman, Republicans including bill sponsor Craig Estes of Wichita Falls, Donna Campbell of New Braunfels and Robert Nichols of Jacksonville, argued fiercely against the amendment, which they said would create unnecessary confusion for law enforcement and threaten overall support for the bill.

Huffman, a former judge and prosecutor who grilled Huffines on his amendment for almost an hour on the Senate floor, said she thought the provision was a “poison pill” for the legislation.

“I want to go on record saying this is a mistake. This is bad for Texas,” she said. “You are just plain wrong on this.”

The measure also faced heated opposition from state Sen. John Whitmire, the Houston Democrat who chairs the Criminal Justice Committee, who argued that numerous police departments and law enforcement organizations were staunchly opposed to the measure.

Whitmire, who said the amendment was “a game-changer” that would endanger the lives of police officers, pleaded with Huffines to consider the consequences of his proposal on law enforcement.

“We are really, really playing with a dangerous matter,” he said. “It’s not something that we can afford to be wrong about.”

The legislation will return to the House for a final vote before it heads to Gov. Greg Abbott, who has said he will sign any open carry legislation that reaches his desk.