will the server closes the client connection automatically if it's idle for a while?

1,145 views
Skip to first unread message

gmz

unread,
Jul 12, 2012, 12:34:21 AM7/12/12
to mongod...@googlegroups.com
hi
A thread is associated with each connection from clients to the database. 
will the mongod/mongos close the thread automatically if it's idle? 
and if yes, how long is the idle time?
thanks for any help

gmz
7/12 2012

gmz

unread,
Jul 12, 2012, 12:38:32 AM7/12/12
to mongod...@googlegroups.com
I am using mongodb 2.0.4

在 2012年7月12日星期四UTC+8下午12时34分21秒,gmz写道:

Nat

unread,
Jul 12, 2012, 12:43:29 AM7/12/12
to mongod...@googlegroups.com
Nope. The connection lifetime is managed by the client side.
From: gmz <gen...@gmail.com>
Date: Wed, 11 Jul 2012 21:38:32 -0700 (PDT)
Subject: [mongodb-user] Re: will the server closes the client connection automatically if it's idle for a while?
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

gmz

unread,
Jul 12, 2012, 12:48:34 AM7/12/12
to mongod...@googlegroups.com, nat....@gmail.com
if setting the socket timeout with 1800 seconds, but unluckily my client is down without closing the connection.
now the thread on the server will run forever?

在 2012年7月12日星期四UTC+8下午12时43分29秒,Nat写道:

gmz

unread,
Jul 12, 2012, 12:49:41 AM7/12/12
to mongod...@googlegroups.com, nat....@gmail.com
or will be closed by the server automatically after 1800 seconds?

在 2012年7月12日星期四UTC+8下午12时48分34秒,gmz写道:

gmz

unread,
Jul 13, 2012, 3:53:14 AM7/13/12
to mongod...@googlegroups.com, nat....@gmail.com
anybody? 

在 2012年7月12日星期四UTC+8下午12时49分41秒,gmz写道:

markh

unread,
Jul 13, 2012, 1:44:53 PM7/13/12
to mongod...@googlegroups.com, nat....@gmail.com

Have you made this change permanently in /etc/sysctl.conf or simply in /proc?

Mongo will not close this server connection, nor should it. The socket state is handled by the TCP stack on the system and it is its responsibility to manage the state of the socket. Based on the settings in /proc, the state will be change  connection. Here's a diagram of the typical changes that a socket will go through, throughout the TCP transaction (initial handshake to closing handshake).

If the port is open, the server is in LISTEN state on the 2-tuple (ip:port). When a connection is made, a socket is open based on a 4-tuple essentially and moved into ESTABLISHED state. From memory, in your case, after 1800 seconds, the socket will begin its transition from ESTABLISHED state, then go into CLOSE WAIT, followed by TIME WAIT and finally CLOSED. The transitions per state are different per o/s as every TCP stack is implemented differently. So the thread will be there for quite a long time until it's cleaned up (I've done some simple testing using netcat).

The best thing to do would be to reproduce the issue with the client dying and then watch the state of the socket with 

  • netstat -an | grep 27017
  • lsof -i :27017

markh

unread,
Jul 13, 2012, 3:03:59 PM7/13/12
to mongod...@googlegroups.com, nat....@gmail.com
Sorry, I left some confusion in my second-last paragraph. As soon as the client connection is ended, the socket immediately moves from ESTABLISHED to CLOSE WAIT, not after 1800 seconds. The 1800 should cover the whole time to CLOSED when the thread disappears. 

On MAC OS, I've seen the connection go straight to TIME WAIT and almost immediately to CLOSED byt Linux typically takes longer. Like I said it depends on the TCP stack as their designs all differ and you should really test it on your system. It's fairly easy with just the mongo shell as the client and the mongod as the server.
Reply all
Reply to author
Forward
0 new messages