core: set z_pid cookie only when set_persistent is used. Hand moved from master branch