There is no need to wait 2 seconds after detaching the servo. As soon as the detach function returns, it's done.
I am wondering if for some reason .. the board was unable to supply required power to run both servo and sensor.
It's not that. The servo library uses a timer that interferes with the pulseIn function. Detaching the servo frees the timer, so pulseIn can use it.