In a dangerous profession, it is among the most dangerous of jobs. The police officers who go undercover to arrest illegal firearms dealers can quickly find themselves at the mercy of their targets, conducting big-money transactions in close quarters with jumpy, suspicious criminals quick to draw guns.

One wrong step, and bullets will fly.

The hazards of the job were made tragically clear on Friday, when an illegal-firearms sting involving an undercover New York City officer erupted in gunfire on a suburban street in Mount Vernon, N.Y., resulting in the death of an innocent bystander named Felix Kumi.

The New York Police Department said that in the midst of the sting, a suspect pulled a gun on the undercover officer, stole the cash changing hands in the transaction and ran. When the officer opened fire, police officials and witnesses said, he shot the suspect, but he also hit Mr. Kumi, 61, who was walking to retrieve his van from a nearby repair shop.

When it comes to undercover gun buys, the only thing that is certain is unpredictability, according to police officials. Dealers must be met on their terms and turf, often in isolated, sparsely populated areas where a close-trailing backup unit can immediately be spotted, Stephen Davis, the department’s chief spokesman, said.