Fixed the location of semaphore release.