How to handle removing a semaphore while it's being waited on by a different task?

the fault occurrs because you delete a semaphore that another task waits on. Do not do that.

Edit: One option would be to re-signal the semaphore after setting some kind of shared indication that allows the waiting task to clean up, ie delete the semaphore.