author ffxbld
Thu, 07 Apr 2016 19:51:06 -0400
changeset 1612 426bd32d5aa7549e518aca5e3b75465ab400d551
parent 560 bd4812420e639f5d19cebc2c1d9f50ad7fbac409
permissions -rw-r--r--
Added FIREFOX_45_0_2esr_RELEASE FIREFOX_45_0_2esr_BUILD1 tag(s) for changeset production-0.8. DONTBUILD CLOSED TREE a=release

from twisted.python import log
def monkeypatch_twisted_cbLogin():
    Work around for, which results in
    leaking one RemoteReference every time a slave disconnects.
    import twisted
    from twisted.python.versions import Version

    # This patch is known to work in versions 8.2.0 -> 11.0.0
    # If your version is outside this range, but this monkey patch still
    # applies, please update this version check!
    # If your version of twisted doesn't need this, returning early is the
    # right thing to do.
    assert Version('twisted', 8, 2, 0) <= twisted.version <= \
            Version('twisted', 12, 0, 0), \
            "monkeypatch failed version check; see note in"

    import twisted.spread.pb
    from twisted.spread.interfaces import IJellyable
    from twisted.spread.pb import AsReferenceable

    def monkeyed_cbLogin(self, (interface, avatar, logout)):
        Ensure that the avatar to be returned to the client is jellyable and
        set up disconnection notification to call the realm's logout object.
        if not IJellyable.providedBy(avatar):
            avatar = AsReferenceable(avatar, "perspective")

        puid = avatar.processUniqueID()

        # only call logout once, whether the connection is dropped (disconnect)
        # or a logout occurs (cleanup), and be careful to drop the reference to
        # it in either case 
        logout = [ logout ]
        def maybeLogout():
            if not logout: return
            fn = logout[0]
            del logout[0]
            fn()[puid] = maybeLogout

        return avatar

    twisted.spread.pb._JellyableAvatarMixin._cbLogin = monkeyed_cbLogin
    log.msg("monkeypatch_twisted_cbLogin applied")