ArcGIS Server 10.0 SP4 SDK MapServer.clearLayers() somehow clears everything in the context so that if method is called like mapServer.refreshServerObjects the following exception is returned.
"AutomationException: A request to obtain a free servercontext has failed because the wait timeout interval has lapsed."
It only happens when you clear the layers from the default map. There is no delay. It immediately puts out the error. Clearing the layers shouldn't force an automation exception with refresh server objects because the layers are associated to the default map. There could be other maps associated with that map server. If you individually delete each layer from the map it works great. Its the clearLayers that is doing something to the underlying context.
Replace // map.clearLayers();with Stack<ILayer> iLayers = new Stack<ILayer>(); for (int i = 0; i < map.getLayerCount(); ++i) { iLayers.push(map.getLayer(i)); } while (!iLayers.empty()) { map.deleteLayer(iLayers.pop()); }