Timed out while checking out a connection from connection pool: context canceled

Hi,

I am using Golang + Mongo in my application. I am using go.mongodb.org/mongo-driver/mongo mongo package/driver.

I am getting timed out while checking out a connection from connection pool: context canceled error very intermittently. I am getting this error when I perform the CRUD operation.

Can someone please help find the reason for this error? And how to fix this?

Regards
Prithvi

@Prithvipal_Singh Did you get this fixed?

I’m seeing this a lot as well -

timed out while checking out a connection from connection pool: context deadline exceeded; maxPoolSize: 100, connections in use by cursors: 0, connections in use by transactions: 0, connections in use by other operations: 3

wisdom_of_the_ancients

Is there a good guide to how to begin debugging this?

What type of context did you pass?
I was getting that when I passed context.WithTimeout but after changing it to context.TODO() it was working fine

1 Like

So, this isn’t as mysterious as the mongodb client is making it seem. You can get a lot of different error messages that all say the same thing like:
connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": connection(...) failed to write: context canceled
or
timed out while checking out a connection from connection pool: context canceled
The beginning part is just telling you what the client was trying to do when it encountered the problem, which isn’t interesting. The problem it encountered is the revealing bit: context canceled. It was trying to use a context that had been cancelled. Unless you put a timeout on a context and some mongodb request took too long and cancelled the context, this is likely a mistake on your part. You probably fed a cancelled context into the client. This can happen when you reuse contexts. Which is not necessarily a mistake. You should just probably check on the status of reused context after a long operation (like a database lookup) if you want to avoid these errors.

You can also just use context.TODO() or context.Background() which aren’t cancellable. But that isn’t a great solution because a problem might cause your request to hang for a long time effectively disabling your processing. That is why timeouts are useful. If you are getting these a lot, you might consider increasing your time out. Otherwise, you should be checking the status of contexts that get reused. Or don’t be afraid to construct a new context with timeout for every mongodb request you make. That is the purpose of contexts. It is easy to make more of them. For example, the net/http package will create a new context for each received request when starting an http server.

hi, do you know why pass context.WithTimeout not work?

It literally does not matter what kind of context you pass in is. What matters is that your contexts are getting cancelled. Either you have some other code that is cancelling the context early (i.e. YOU are calling the cancel function), or you are setting the timeout time to be too short. If the request takes longer than your time, it will fail the request. What timeout are you setting? How long do these requests take? Are they always longer than your timeout? Are they sometimes longer? Are you mistakenly using the same context with a timeout on multiple requests and so it is running out of time for all of them?