Single user mode looses connection
When you set a database to single_user, you don't have any control over which session gets to connect, unless you're already connected to the database.
So simply connect before setting single_user to eliminate the race condition:
USE AdventureWorks2017
ALTER DATABASE CURRENT SET SINGLE_USER WITH ROLLBACK IMMEDIATE