server: implement proper exiting