Server selection error: server selection timeout

I have a go routine which is running continuously every 5 seconds where I am trying to connect to mongodb, then create a cursor and use it to iterate through mongodb doc.
My code is working fine for some time but it always goes to panic say after 45 mins. on this line:

cur, err := coll.Find(context.Background(), filter)

and the panic says:

server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr:, Type: Unknown, Last error: dial tcp: lookup on server misbehaving }, ] }

Any idea if I am missing somthing??

Hey @Rishav_Kumar_Jha,

Welcome to the MongoDB Community forums :sparkles:

The server misbehaving error comes from the MongoDB Agent (here Go Driver) and it means the name server was unable to process this query due to a problem with the name server. Please refer to RFC 1035 for more details.

It appears to be a DNS issue, as the Go driver does not cache DNS and instead relies on the OS and its resolvers. Please refer to this JIRA ticket for more details.

Having said that, to better understand this issue, could you please provide us with the code snippet from your script, the MongoDB version, and the Go Driver version you are using?

Also, can you confirm that every 5 sec you are creating a new connection with the database server? If yes, can you keep the connection alive rather than building a new one every 5 seconds?