I would try first to run the script either without threads by modifying the code or with the debugger with the option -dt instead of -d.
I had a similar issue recently and it was related to different return codes in a network module. the script even ran under Solaris and Windows