[Twisted-Python] self.channel.writeHeaders(version, code, reason, headers)\nbuiltins.AttributeError: \'NoneType\' object has no attribute \'writeHeaders\'\

Waqar Khan wk80333 at gmail.com
Mon Aug 5 12:26:03 MDT 2019


Hi Jean,
  Yeah, actually that is the example that I looked into and implemented my
code.

The issue I have is.. still some instances of interrupted responses are not
trapped.
Copying pasting my minimal implementation for your convinence

def print_json_response(resp, request):
         request.write(json.dumps(resp))
         request.close()
class FooResource(resource.Resource):
       def render_GET(request):
              future = asyncio.ensure_future(self.fetch_response(request))
// some async await functions
              d = Deferred.fromFuture(future)
              d.addCallback(print_json_response, request) // this is
actually where the error is triggered.
              d.addErrback(lambda failure:
failure.trap(defer.CancelledError))
              finished_errback = request.notifyFinish()
              finished_errback.addErrback(self.handle_cancel, d) // simple
logs and cancels d by d.cancel()
              return NOT_DONE_YET

       async def fetch_response(self, request):
        future = {}
        try:
           future = await some other async def.. which returns future
        except asyncio.CancelledError as e:
           print("Error..", e)
        return future
    def handle_cancel(failure, d):
          d.cancel()
          print(failure.getTraceback())





On Mon, Aug 5, 2019 at 2:21 PM Jean-Paul Calderone <
exarkun at twistedmatrix.com> wrote:

> On Mon, Aug 5, 2019 at 2:15 PM Waqar Khan <wk80333 at gmail.com> wrote:
>
>> False alarm. Seems like there are some sneaky conditions when I get the
>> error message.
>> Like before, I use to get notifyFinish error everytime. Now, it seems
>> that 6/10 times  things are "clean" but then 4/10 times there are
>> notifyFinish errors.
>> Wondering on your suggestion. How do I ensure whether the notifyFinish
>> error deferred has been fired or not.
>> Could I have like a vanilla "HelloWorld" example?
>> Thanks
>>
>>
> You can find an example of this here -
> https://twistedmatrix.com/documents/current/web/howto/web-in-60/interrupted.html
>
> Jean-Paul
>
>
>> On Mon, Aug 5, 2019 at 2:24 AM Waqar Khan <wk80333 at gmail.com> wrote:
>>
>>> Hi Glyph,
>>>     I am not sure I understand.
>>>
>>> Is there a method/variable in request which keeps a track whether
>>> notifyFinish has been fired..
>>> So, I can do something like.
>>>
>>> if not request.hasFiredNotifyFinish:
>>>        request.finish()
>>> ??
>>>
>>>
>>> I have sort of able to get around this issue.. though I can't put a
>>> finger on what actually worked.
>>> Here is what I did.. First change to 19.7rc01 version.. and just fix the
>>> channel issue.
>>>
>>> Next  self.fetch_response(request).. This is an async def.. So what I
>>> did was..
>>>
>>> async def fetch_response(request):
>>>         future = {}
>>>         try:
>>>            future = await some other async def.. which returns future
>>>         except asyncio.CancelledError as e:
>>>            print("Error..", e)
>>>         return future
>>>
>>> I basically ended up doing this everywhere where there is async/await.
>>> Next, I added this:
>>> d.addErrback(lambda failure: failure.trap(asyncio.CancelledError))
>>>
>>>
>>> So.. now.. I don't see the notifyFinish error anymore. And I am bit
>>> terrified not to touch anything.. :-D
>>>
>>> But, I want to try out your suggestion as that seems like a more solid
>>> way to handle the issue. But I am not sure I quite understand, how do I
>>> figure out whether notifyFinish has been called before calling finish?
>>>
>>>
>>> On Mon, Aug 5, 2019 at 1:56 AM Glyph <glyph at twistedmatrix.com> wrote:
>>>
>>>>
>>>>
>>>> On Aug 4, 2019, at 9:04 PM, Waqar Khan <wk80333 at gmail.com> wrote:
>>>>
>>>> Ah yes.. That is true..
>>>>
>>>> If I comment out request.finish() (Here is the doc which I tried to
>>>> followed:
>>>> https://twistedmatrix.com/documents/13.0.0/web/howto/web-in-60/interrupted.html
>>>> )
>>>> Then actually.. when I try to test out the code... (via curl or like
>>>> doing requests.get .. to the URI)..
>>>> it is just stuck..
>>>>
>>>>
>>>> Yep!  This makes sense; if you don't call `.finish()` *at all* then
>>>> your code will never tell the client that it's done.
>>>>
>>>> But if you call `.finish()` *after notifyFinish() has fired* then you
>>>> get that error.
>>>>
>>>> If you change your code to only call `.finish()` if the deferred
>>>> returned by notifyFinish() has not fired or failed yet, do you get the
>>>> error?  It's possible that you still do, in which case, there's a bug in
>>>> Twisted that needs to be fixed.
>>>>
>>>> I do want to convey my thanks for the help. Really appreciate it.
>>>>
>>>>
>>>> Thanks for using Twisted :)
>>>> -g
>>>>
>>>> _______________________________________________
>>>> Twisted-Python mailing list
>>>> Twisted-Python at twistedmatrix.com
>>>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>>>
>>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20190805/222f4248/attachment-0002.html>


More information about the Twisted-Python mailing list