Fix double free bug when server is shut down

Thanks @oleastre