Avoid having infinite loops

Yes it will give you infinite loop, because you’re calling the WP_User::set_role method within the set_user_role action that’s again fired within the the WP_User::set_role method.

Not sure what the setup is but you can try to run it only once, with

remove_action( current_action(), __FUNCTION__ );

as the first line in your callback, or use another hook.

Update: I just noticed that I miswrote filter instead of action, but that would have worked the same though 😉 It’s now adjusted.