In fact, it is a race condition already in close() below, since this is protected by a mutex while the shutdown is not. It also seems like a convoluted way of structuring this (with the two separate defer statements).