Switching sources with lua script from vision to GPS

Hi,
I am testing to switch sources with lua script. I have Visual odometry set to EK3_SRC1 and GPS set to EK3_SRC2. My visual odometry drifted in flight, hence with my lua script, I switched from Vision odometry to GPS. after switching, to avoid positional correction, I am switching to brake and LAND ode with lua script. after switching, The copter crash landed. Below, I’ve attached the logs. all the help is appreciated.
@rmackay9 your help is really appreciated. I tried understanding the issue but I cant pin point the cause. it would be great if you can share your insights. thanks

here’s the crucial part of the log
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|Lua: Time: 19 Mem: 32226 + 36|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|SOURCE CHANGED|
|15:58:43|Lua: Time: 41 Mem: 32226 + 36|
|15:58:43|VisOdom: yaw shifted 358 to 4 deg|
|15:58:43|GPS Glitch|
|15:58:43|EKF3 IMU0 MAG0 in-flight yaw alignment complete|
|15:58:43|EKF3 IMU1 MAG0 in-flight yaw alignment complete|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|SOURCE CHANGED FROM: 0 TO: 1|
|15:58:43|Mode Changed to BRAKE|
|15:58:43|Mode Changed to LAND|
|15:58:43|Lua: Time: 271 Mem: 32543 + 353|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|Lua: Time: 18 Mem: 32226 + 36|
|15:58:43|EKF variance|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|Lua: Time: 19 Mem: 32226 + 36|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|Lua: Time: 55 Mem: 32226 + 36|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|Lua: Time: 19 Mem: 32226 + 36|
|15:58:43|EKF3 lane switch 1|
|15:58:43|EKF primary changed:1|
|15:58:43|Lua: Running /APM/scripts/source_switch_vis_gps.lu|
|15:58:43|Lua: Time: 19 Mem: 32226 + 36|

full logs

This type of scripting should be doe with ArduCopter 4.1.5 or newer.

yes I’m using 4.1.5, sorry for the mistake in tag