C# Driver exception - "The operation is not allowed on non-connected sockets"

3,041 views
Skip to first unread message

mciureanu

unread,
Jun 24, 2011, 4:23:22 AM6/24/11
to mongodb-user
Hello. I'm getting from time to time this exception on the production
servers. We are using the C# driver (officual one, version 1.0).

System.InvalidOperationException: The operation is not allowed on non-
connected sockets.
at System.Net.Sockets.TcpClient.GetStream()
at MongoDB.Driver.Internal.MongoConnection.GetNetworkStream()
at
MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage
message, SafeMode safeMode)
at
MongoDB.Driver.Internal.MongoCursorEnumerator`1.GetReply(MongoRequestMessage
message)
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.GetFirst()
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1
source)

What does this mean? Are there infrastructure problems with the
connection to the server, and the TCP connection is lost, and the
driver doesn't handle this gracefuly by removing that connection from
the pool? Thanks.

jimmy romanticdevil

unread,
Jun 24, 2011, 10:14:57 AM6/24/11
to mongod...@googlegroups.com
you must run as administrator ...because u use socket ....

Robert Stam

unread,
Jun 24, 2011, 10:17:49 AM6/24/11
to mongodb-user
That doesn't sound right. I run the unit tests all the time without
running as administrator.

I've never seen this particular exception in development. Is it
reproducible?

On Jun 24, 10:14 am, jimmy romanticdevil

mciureanu

unread,
Jun 24, 2011, 10:43:59 AM6/24/11
to mongodb-user
It isn't reproductible, the only pattern which I observed is that
after restarting IIS I don't get such errors for a while. Is it
possible that if not using RequestStart() and RequestStop() right it
is possible to get such errors? Or this should be a infrastructure
generated problem?

Robert Stam

unread,
Jun 24, 2011, 10:49:29 AM6/24/11
to mongodb-user
Are you using RequestStart/RequestDone?

The main potential issue with RequestStart is that if you neglect to
call RequestDone the connection will never be returned to the
connection pool, which could cause the connection pool to grow to its
maximum size (but with all connections lost to orphan RequestStarts).

I can't tell if it's infrastructure related.

Are there any other errors that are occurring before this one? Perhaps
a previous error has left the driver in a state that then results in
this error?

jimmy romanticdevil

unread,
Jun 24, 2011, 10:49:25 AM6/24/11
to mongod...@googlegroups.com
What os do you use ? if u use windows xp you can run without administrator ..but if u using windows7 its need to run as administrator ...i have a develope many aplication with socket ..i always need to run all my aplication as administrator ...same in linux ...if you using socket you must run as root ..


2011/6/24 Robert Stam <rstam...@gmail.com>
--
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.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


jimmy romanticdevil

unread,
Jun 24, 2011, 11:23:34 AM6/24/11
to mongod...@googlegroups.com
yes it can run without run as administrator if u mean running a mongodb ..but we friend mciureanu have include a socket library



you can see the error in here :

System.
InvalidOperationException: The operation is not allowed on non-
connected sockets.
  at System.Net.Sockets.TcpClient.GetStream()

see " The operation is not allowed on non-
connected sockets." ....this is i mean he must running as administrator .. :)

Robert Stam

unread,
Jun 24, 2011, 11:01:22 AM6/24/11
to mongodb-user
That doesn't sound right. I run the unit tests all the time without
running as administrator (on Windows 7).

On Jun 24, 10:49 am, jimmy romanticdevil
<romanticdevil.ji...@gmail.com> wrote:
> What os do you use ? if u use windows xp you can run without administrator
> ..but if u using windows7 its need to run as administrator ...i have a
> develope many aplication with socket ..i always need to run all my
> aplication as administrator ...same in linux ...if you using socket you must
> run as root ..
>
> 2011/6/24 Robert Stam <rstam10...@gmail.com>

Robert Stam

unread,
Jun 24, 2011, 11:38:54 AM6/24/11
to mongodb-user
I think the error message means something else, like that the
connection somehow was disconnected since it was first connected (or
that it was never connected in the first place).

I never run as administrator and I don't see this error message. I run
both mongod and the client code (unit tests, test applications,
etc...) as non-administrator.

On Jun 24, 11:23 am, jimmy romanticdevil

rasitha

unread,
Jun 24, 2011, 5:59:27 PM6/24/11
to mongodb-user
If you are using RequestStart make sure to use it in a 'using' block
or in a try/finally block.
> > > you must run as administrator ...because u use socket ....- Hide quoted text -
>
> - Show quoted text -

Tomas Mirezko

unread,
Jun 25, 2011, 4:13:35 AM6/25/11
to mongod...@googlegroups.com
Hi,

as I wrote in another thread, I used to get these kind of exception reproducible with these steps:

1) start mongod
2) connect and read something from any collection
3) stop+start mongod
4) read something from any collection <-- here it throws the errror

Or it could be that you are not finalizing RequestStart as someone said above.

Regarding sockets, I have no problem (win7) running without admin rights, but I know that for example Flash can't open socket for ports below 1024.

Good luck,
Tomas

Stuart Johnson

unread,
Jun 25, 2011, 7:25:36 AM6/25/11
to mongod...@googlegroups.com
This is a known problem. What you have to do is wrap every operation in
a try{}catch block, and then repeat the operation. What is worse, is if
your using a replica set and the primary server switches you get the
same thing. A dropped IP connection is not dropped from the pool, until
it's too late, which seems like a strange design decision to me.

> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/nsDhdGBIv1QJ.

Tomas Mirezko

unread,
Jun 25, 2011, 7:33:46 AM6/25/11
to mongod...@googlegroups.com
I've created simple interceptor for retry after this kind of error. It would be great if Mongo throwed something derived from MongoException or so... but...

This can be used with Ninject.Extensions.Interception + DynamicProxy of your choice (ex. LinFu.DynamicProxy)

    class RetryAfterExceptionInterceptor : IInterceptor
    {
        private ILog log;

        public int MaxRetries { get; set; }

        private Exception _lastException;

        public RetryAfterExceptionInterceptor(int maxRetries)
        {
            MaxRetries = maxRetries;
        }

        public void Intercept(IInvocation invocation)
        {
            log = LogManager.GetLogger(invocation.Request.Target.GetType());

            var retries = 0;

            while (retries < MaxRetries)
            {
                if (Retry(invocation))
                {
                    return;
                }
                retries++;
                Thread.Sleep(0);
            }

            throw _lastException;
        }

        private Boolean Retry(IInvocation invocation)
        {
            try
            {
                invocation.Proceed();
                return true;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                _lastException = ex;
                return false;
            }
        }
    }
Reply all
Reply to author
Forward
0 new messages