Fix uid management and websocket closure before shell exit