While writing this description, I've realized there are some unresolved issues, so this should not be considered final

In trunk
changeset 3266 I added the ability to cleanly kill a running script, or return an error if you try to execute a script while one is already running.
This is a minor protocol change (2.6), backwards compatible with existing clients.
The corresponding chdkptp changes are in
changeset 467In addition allowing you to kill a script remotely, this allows compatible to clients to run a script without first doing a ScriptStatus query, which should improve performance slightly.
Up to now, PTP_CHDK_ExecuteScript param2 has been defined as "script language" which currently only accepts 0 for lua. In 2.6, only the lowest byte indicates language, and the remaining bits are flags.
If no flags are set, the behavior is equivalent to the old behavior, so earlier clients can connect to 2.6 cameras without any change. As in prior versions, calling PTP_CHDK_ExecuteScrip will cause any running script to be killed. However, in 2.6 this is safe, and does not cause memory corruption. Setting any of the flags for a pre-2.6 camera will result in an error, because it will be interpreted as an unknown scripting language. 2.6 aware clients should only send flags cameras running 2.6 or greater, since the error for a bad script language does not handle the data phase.
The added flags are
PTP_CHDK_SCRIPT_FL_NOKILL
If set, this causes PTP_CHDK_ExecuteScript to return PTP_CHDK_S_ERRTYPE_INIT in param2, and add a script message of the same type with the string "script running".
PTP_CHDK_SCRIPT_FL_FLUSH_CAM_MSGS
If set this causes the camera to discard any existing unread cam->host messages before starting the new script.
PTP_CHDK_SCRIPT_FL_FLUSH_HOST_MSGS
Discard unread host->cam messages before starting.
The flush flags only take effect if the new script is started, if PTP_CHDK_SCRIPT_FL_NOKILL finds a script running, it's messages will not be affected.
The flush flags are added because the most common case is to not want any messages from old scripts floating around, and getting rid of them would otherwise require additional PTP calls to check state and read the messages (for cam->host) or leave them in memory until the script reads messages (for host->cam).
Issues:
Unsafe for ptp task to add a message when a script is running
Script ID will get incremented in the PTP_CHDK_SCRIPT_FL_NOKILL case