Code Monkey home page Code Monkey logo

Comments (4)

pakedge avatar pakedge commented on July 4, 2024

There appears to be a bigger error here. Suppose I create a MongoConnection (which is really a ConnectionPool subclass) with 100 connections (pool_size=100). It looks like everything authenticates fine (_authenticated is True). But it then crashes sometimes in collection.py at line 318 in get_authenticated_protocol. Specifically, when another thread calls the an insert command, it gets to collection.insert, line 237 of insert:

    if self._database._authenticated :
        proto = yield self._database.connection.get_authenticated_protocol(self._database)
    else :
        proto = yield self._database.connection.getprotocol()

Since _authenticated is True, the call is made on line 237 to get_authenticatedProtocol. Here is what that looks like:

@defer.inlineCallbacks
def get_authenticated_protocol(self,database) :
    # Get the next protocol available for communication in the pool
    connection = self.__pool[self.__index]
    database_name = str(database)

    if database_name not in connection.auth_set :
        name  = self.cred_cache[database_name][0]
        password = self.cred_cache[database_name][1]
        yield self.authenticate_with_nonce(database,name,password)
    else :
        self.__index = (self.__index + 1) % self.__pool_size

    defer.returnValue(connection.instance)

It crashes with a KeyError on line 318:
name = self.cred_cache[database_name][0]

This means that database_name is not in self.cred_cache!
Surprisingly enough, if we restart our process a few times, this issue goes away. But I really don't know why this error would pop up in the first place.

from txmongo.

pakedge avatar pakedge commented on July 4, 2024

Possible solution: in connection.py around line 291. Instead of adding database_name to the auth_set of current_connection, why not add it to ALL connections in the thread pool? Or are some previously opened connections then having their auth_sets emptied in clientConnectionLost?

Something's not right here when we have multiple connections in a connection pool.

from txmongo.

IlyaSkriblovsky avatar IlyaSkriblovsky commented on July 4, 2024

Hi! Please check out latest changes to txmongo. I've reworked all authentication-related stuff and my pull request was merged yesterday. Old authentication code had race conditions and was unstable with connection pool with pool_size > 1. Please test latest master branch in your scenarios.

Returning status of authentication is not that easy. MongoConnection is actually a connection pool and each individual connection in the pool might be active or inactive at any moment of time. What should authenticate() return if all connections in the pool is not active (like just after creating MongoConnection instance in your example)? In this case authenticate() just remembers that it needs to authenticate with specified credentials when any connection is going online.

My new implementation of authenticate() behaves this way:

  1. If any connection in the pool is active when you call it, it will wait until all active connections will be authenticated (as long as you call it with yield of course)
  2. If there is no active connections in the pool, it will return immediately

But your original example is the case 2, so it won't help you in this scenario.

I suggest you to issue some useless find_one() right after authenticate() and catch exception about failed authentication.

from txmongo.

fiorix avatar fiorix commented on July 4, 2024

Sounds like @pakedge needs a git pull.
Thanks @IlyaSkriblovsky.

from txmongo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.