module memory malloc imrovements

fixes a race condition in module_malloc and improves the locking in cmd_command_timeout