Democrat Mark Herring, left, and Republican Mark Obenshain participate in a campaign debate hosted by the Loudoun County Chamber of Commerce on Oct. 2 in Leesburg. (Jahi Chikwendiu/The Washington Post)

After weeks of legal wrangling, a recount of the closest statewide election in Virginia history begins Monday. By the end of the week, Virginians should know who will serve as their next attorney general — unless the loser chooses to prolong the fight.

At 7 a.m. in Fairfax County and the cities of Alexandria and Chesapeake, election officials will begin tallying the votes — again — under the watchful eyes of observers from the campaigns of state Sen. Mark R. Herring (D-Loudoun) and Mark D. Obenshain (R-Harrisonburg).

On Nov. 25, the State Board of Elections declared Herring the winner of the attorney general contest by 165 votes out of more than 2.2 million cast, after both a local and statewide canvass to review the results.

That tiny margin entitled Obenshain — who led the race on election night, before the local canvass was done — to a government-funded recount. Even after the recount is complete, Virginia law allows the loser to contest the result in an unusual joint session of the General Assembly.

All other jurisdictions will begin their recounts Tuesday. Fairfax got extra time because of its size, while Alexandria and Chesapeake were given a head start to count ballots by hand because their voting machines can’t be programmed to do the recount properly.

A special three-judge panel will convene Wednesday in Richmond to begin reviewing challenged ballots and hearing disputes between the two parties, with the goal of completing the process by Friday.

In Fairfax, officials have spent days finalizing plans and testing equipment before the real action begins, even making sure that the Fairfax courthouse won’t blow a fuse when all of the voting machines are plugged in at once.

“I think we’re ready,” said Brian Schoeneman, the secretary of the Fairfax Electoral Board.

On Monday, 40 teams of Fairfax recount officials — each consisting of one Democrat and one Republican — will begin feeding ballots back through machines or hand-counting the ballots that can’t be read. Each team will be watched by observers from the two campaigns, while Schoeneman and Seth T. Stark, the electoral board chairman, oversee the process.

“I am hoping that everybody is calm and recognizes that we have a job to do,” Schoeneman said. “This is going to be a stressful environment regardless, by virtue of the fact that we’re squeezing as many people into these rooms as possible.”

Fairfax has been the site of controversy in the race because of its handling of voters who cast provisional ballots because they lacked ID, went to the wrong polling place or had requested but said they never used an absentee ballot.

The county gave more time than other jurisdictions to allow provisional voters to appear in person to explain their ballots. Republicans objected to that extra time, though the board said it was acting legally.

And last week, Obenshain’s campaign said it was “distressed by the lapse in ballot security” in Fairfax because some ballots were not delivered to the county clerk for safekeeping as quickly as they should have been. Schoeneman has responded that there was “no evidence that there was any breach of security” of the ballots.

Such complaints could come into play if the loser chooses to take the race to the legislature. Obenshain’s lawyer, William H. Hurd, said in court last week that the Republican “may wish to consider that possibility,” though he later called such talk “premature” and “hypothetical.”

In Alexandria, officials had their final planning meeting Friday, reviewing every form that would be used during the recount.

“I feel very confident we’ll have a very smooth recount operation,” said Susan B. Kellom, chair of the Alexandria Electoral Board.

Despite all the preparation, it’s almost inevitable that the campaigns will clash over how the ballots should be counted, according to Joshua Douglas, an election law expert and assistant professor at the University of Kentucky College of Law.

“In essence, if the candidate is behind, the candidate and his lawyers will argue for a more lenient standard, as they want to count more ballots and hopefully take the lead,” Douglas said. “If the candidate is ahead, his team will argue for a stricter standard regarding which ballots to count. Either way, it is almost always acrimonious and partisan when the election is this close.”