Hi,
Looking at a 2003 BA 40 wiring diagram, (it's all I could get off the net) you could try using a jumper wire to ground the black with red stripe (Bl/R) wire at the emergency stop switch when it won't turn off. There's a bullet style connector near the switch that you could use to open the circuit instead of piercing the wire or back probing the switch connector and risk messing something up.
If the engine shuts down then you will know that the fault is likely on the black wire (Bl) side that grounds the CDI ignition terminal and your search along that wire and ground path begins.
If the engine doesn't stop when doing that, you could try grounding the Bl/R wire at the CDI to see if the engine will stop. Again, there is (should be anyway) a connector nearby that you might use but it may be better to CAREFULLY back probe right at the CDI instead.
I say that because, if the engine won't stop grounding the wire right at the CDI, then it is very likely that there is a problem with that internal CDI circuit.
If that's the case, I don't know but I think you could work around that if need be.
If you don't have a wiring diagram or owners manual, you can Google HONDA BF40 OWNERS MANUAL and download one. The diagrams are on the last pages.
Look for one offered from Honda.
Not the greatest clarity but, with a magnifying glass they can be read.
Good luck and let us know what you find.