Connection to CosmosDB through Mongo API fails after idle

The problem went away after we added &maxIdleTimeMS=1500000 to the connection URI in order to set the maximum connection idle time to 25 minutes.

The cause seems to be a timeout of 30 minutes for idle connections on the Azure server, while the default behaviour for Mongo clients is no idle timeout at all. The server does not communicate the fact that it is dropping an idled connection back to the client, so that the next attempt at using it fails with the above error. Setting the maximum connection idle time to a value less than 30 minutes makes our server close idle connections before the Azure server kills them. Some sort of keep-alive or check before using a connection would probably also be possible.

I haven't actually been able to find any documentation about this or other references to this problem for CosmosDB, although it may be caused by or related to the 30 minute idle timeout for TCP connections for Azure Internal Load Balancers (see e.g. https://feedback.azure.com/forums/217313-networking/suggestions/18823588-increase-idle-timeout-on-internal-load-balancers-t).