[Twisted-Python] Returning Failure when requestAvatarID to indicate a failed login is not working for me

Glyph glyph at twistedmatrix.com
Wed Jun 5 23:35:53 MDT 2019



> On Jun 5, 2019, at 4:52 AM, Thomas Westfeld <thomas.westfeld at currenta.de> wrote:
> 
> Dear all,

Hi Thomas!

> I am experiencing a problem with a custom credential checker for twisted cred based on an external server.
> 
> My problem is, that the Failure in the last line does not trigger the Errback of the deferred of the calling method to signal that the login failed.

This is surprising, because unless something else is broken, the example you gave definitely behaves like that.  Which suggests to me that something else is broken.

> I also tried to raise the error but this does not work for me.

This should also work.

> I also tried to return a defer.fail(error.UnauthorizedLogin()).

This, however, is a mistake; you can't returnValue(someDeferred()) in inlineCallbacks; you have to wait for deferreds with 'yield'.  So this failing is expected.

> I am now a bit puzzled that it could be problematic, because the @inlineCallbacks decorator makes the function itself return a deferred already. It would be great to have an inlineCallbacked method as getAvatarID method, because I am doing some networking calls in it which are all returning deferreds.
> 
> Do I have to rewrite the getAvatarID implementation to not use inlineCallbacks?

No, this should indeed work as you describe.

It would be helpful if you could construct the most minimal possible example of your issue.

For example, here's a super minimal illustration of the fact that the two techniques that should work, do in fact work:

from twisted.internet.defer import inlineCallbacks, returnValue, fail
from twisted.python.failure import Failure
from twisted.cred.error import UnauthorizedLogin

@inlineCallbacks
def returnFailure():
    if 0:
        yield
    returnValue(Failure(UnauthorizedLogin()))

@inlineCallbacks
def returnFailResult():
    if 0:
        yield
    returnValue(fail(UnauthorizedLogin()))

@inlineCallbacks
def raiseException():
    if 0:
        yield
    raise UnauthorizedLogin()

returnFailure().addErrback(lambda eb: print(eb))
# returnFailResult().addErrback(lambda eb: print(eb))
raiseException().addErrback(lambda eb: print(eb))

Could you reduce your checker to something suitably minimal that can be run directly without actually talking to your auth backend?  My guess is that you'll discover a bug as you're doing this, but the possibility definitely remains that there's an issue with Twisted; I just can't guess what it might be without more information.

Thanks for using Twisted,

-g
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20190605/b4cfb2e6/attachment-0002.html>


More information about the Twisted-Python mailing list