Re: self.channel.writeHeaders(version, code, reason, headers)\nbuiltins.AttributeError: \'NoneType\' object has no attribute \'writeHeaders\'\

Waqar Khan <[email protected]> Mon, 5 Aug 2019 14:30:52 -0400
Newsgroups gmane.comp.python.twisted
Message-ID <CAJuJkHM4q1LGFvLABkJPRCx0_Fbv1-y36-aqKrkBs-LUapZvqw@mail.gmail.com>
--===============4540812947757986767==
Content-Type: multipart/alternative; boundary="000000000000102204058f62e912"

--000000000000102204058f62e912
Content-Type: text/plain; charset="UTF-8"

To add on this.. Glpyh's suggestion was to not call `request.close()` if
notifyFinish() deferred has been fired.
But I am not sure how do I check if that deffered is available or not.

On Mon, Aug 5, 2019 at 2:26 PM Waqar Khan <[email protected]> wrote:

> 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 <
> [email protected]> wrote:
>
>> On Mon, Aug 5, 2019 at 2:15 PM Waqar Khan <[email protected]> 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 <[email protected]> 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 <[email protected]> wrote:
>>>>
>>>>>
>>>>>
>>>>> On Aug 4, 2019, at 9:04 PM, Waqar Khan <[email protected]> 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
>>>>> [email protected]
>>>>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>>>>
>>>> _______________________________________________
>>> Twisted-Python mailing list
>>> [email protected]
>>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>>
>> _______________________________________________
>> Twisted-Python mailing list
>> [email protected]
>> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
>

--000000000000102204058f62e912
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">To add on this.. Glpyh&#39;s suggestion was to not call `r=
equest.close()` if notifyFinish() deferred has been fired.=C2=A0<div>But I =
am not sure how do I check if that deffered is available or not.=C2=A0</div=
></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr"=
>On Mon, Aug 5, 2019 at 2:26 PM Waqar Khan &lt;<a href=3D"mailto:wk80333@gm=
ail.com">[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gma=
il_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,2=
04,204);padding-left:1ex"><div dir=3D"ltr">Hi Jean,<div>=C2=A0 Yeah, actual=
ly that is the example that I looked into and implemented my code.</div><di=
v><br>The issue I have is.. still some instances of interrupted responses a=
re not trapped.=C2=A0</div><div>Copying pasting my minimal implementation f=
or your convinence</div><div><br></div><div><div>def=C2=A0print_json_respon=
se(resp, request):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0request.writ=
e(json.dumps(resp))</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0request.clo=
se()</div></div><div><div>class FooResource(resource.Resource):</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0def render_GET(request):</div><div>=C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 future =3D asyncio.ensure_future(self.f=
etch_response(request)) // some async await functions</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 d =3D=C2=A0Deferred.fromFuture(futur=
e)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0d.addCal=
lback(print_json_response, request) // this is actually where the error is =
triggered.</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0=
d.addErrback(lambda failure: failure.trap(defer.CancelledError))</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0finished_errback =3D =
request.notifyFinish()</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 finished_errback.addErrback(self.handle_cancel, d) // simple logs an=
d cancels d by d.cancel()</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 return NOT_DONE_YET</div></div><div><br></div><div>=C2=A0 =C2=A0=
 =C2=A0 =C2=A0async def fetch_response(self, request):</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 future =3D {}</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 try:<=
/div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0future =3D await some ot=
her async def.. which returns future</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
except asyncio.CancelledError as e:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0print(&quot;Error..&quot;, e)</div><div>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 return future</div><div>=C2=A0 =C2=A0 def handle_cancel(failure, d):=
</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 d.cancel()</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print(failure.getTraceback())</div><div><br></d=
iv><div><br></div><div><br></div><div><br></div></div><br><div class=3D"gma=
il_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Aug 5, 2019 at 2:21=
 PM Jean-Paul Calderone &lt;<a href=3D"mailto:[email protected]" ta=
rget=3D"_blank">[email protected]</a>&gt; wrote:<br></div><blockquo=
te class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px =
solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div dir=3D"ltr">=
On Mon, Aug 5, 2019 at 2:15 PM Waqar Khan &lt;<a href=3D"mailto:wk80333@gma=
il.com" target=3D"_blank">[email protected]</a>&gt; wrote:<br></div><div cl=
ass=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0=
px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div =
dir=3D"ltr">False alarm. Seems like there are some sneaky conditions when I=
 get the error message.<div>Like before, I use to get notifyFinish error ev=
erytime. Now, it seems that 6/10 times=C2=A0 things are &quot;clean&quot; b=
ut then 4/10 times there are notifyFinish errors.</div><div>Wondering on yo=
ur suggestion. How do I ensure whether the notifyFinish error deferred has =
been fired or not.</div><div>Could I have like a vanilla &quot;HelloWorld&q=
uot; example?</div><div>Thanks</div></div><br></blockquote><div><br></div><=
div>You can find an example of this here -=C2=A0<a href=3D"https://twistedm=
atrix.com/documents/current/web/howto/web-in-60/interrupted.html" target=3D=
"_blank">https://twistedmatrix.com/documents/current/web/howto/web-in-60/in=
terrupted.html</a></div><div><br></div><div>Jean-Paul</div><div>=C2=A0</div=
><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border=
-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=3D"gmail_quot=
e"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Aug 5, 2019 at 2:24 AM Waq=
ar Khan &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">wk80333@=
gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding=
-left:1ex"><div dir=3D"ltr">Hi Glyph,<div>=C2=A0 =C2=A0 I am not sure I und=
erstand.=C2=A0</div><div><br></div><div>Is there a method/variable in reque=
st which keeps a track whether notifyFinish has been fired..</div><div>So, =
I can do something like.</div><div><br></div><div>if not request.hasFiredNo=
tifyFinish:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0request.finish()</div><div=
>??</div><div><br></div><div><br></div><div>I have sort of able to get arou=
nd this issue.. though I can&#39;t put a finger on what actually worked.=C2=
=A0</div><div>Here is what I did.. First change to 19.7rc01 version.. and j=
ust fix the channel issue.</div><div><br></div><div>Next=C2=A0 self.fetch_r=
esponse(request).. This is an async def.. So what I did was..</div><div><br=
></div><div>async def fetch_response(request):</div><div>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 future =3D {}</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 try:</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0future =3D await some other asy=
nc def.. which returns future</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 except =
asyncio.CancelledError as e:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0print(&quot;Error..&quot;, e)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 r=
eturn future</div><div><br></div><div>I basically ended up doing this every=
where where there is async/await.</div><div>Next, I added this:</div><div>d=
.addErrback(lambda failure: failure.trap(asyncio.CancelledError))=C2=A0</di=
v><div><br></div><div><br></div><div>So.. now.. I don&#39;t see the notifyF=
inish error anymore. And I am bit terrified not to touch anything.. :-D</di=
v><div><br></div><div>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 unders=
tand, how do I figure out whether notifyFinish has been called before calli=
ng finish?=C2=A0=C2=A0</div><div><br></div></div><br><div class=3D"gmail_qu=
ote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Aug 5, 2019 at 1:56 AM G=
lyph &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">glyph=
@twistedmatrix.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote=
" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);=
padding-left:1ex"><div><br><div><br><blockquote type=3D"cite"><div>On Aug 4=
, 2019, at 9:04 PM, Waqar Khan &lt;<a href=3D"mailto:[email protected]" tar=
get=3D"_blank">[email protected]</a>&gt; wrote:</div><br class=3D"gmail-m_3=
217915863636549902gmail-m_-3580166139866134158gmail-m_73587776653614432gmai=
l-m_1609281543949452197gmail-m_966327303200188702Apple-interchange-newline"=
><div><div dir=3D"ltr">Ah yes.. That is true..<div><br></div><div>If I comm=
ent out request.finish() (Here is the doc which I tried to followed:=C2=A0<=
a href=3D"https://twistedmatrix.com/documents/13.0.0/web/howto/web-in-60/in=
terrupted.html" target=3D"_blank">https://twistedmatrix.com/documents/13.0.=
0/web/howto/web-in-60/interrupted.html</a>)=C2=A0</div><div>Then actually..=
 when I try to test out the code... (via curl or like doing requests.get ..=
 to the URI)..=C2=A0</div><div>it is just stuck..</div></div></div></blockq=
uote><div><br></div><div>Yep!=C2=A0 This makes sense; if you don&#39;t call=
 `.finish()` <i>at all</i><span style=3D"font-style:normal">=C2=A0then your=
 code will never tell the client that it&#39;s done.</span></div><div><span=
 style=3D"font-style:normal"><br></span></div><div>But if you call `.finish=
()` <i>after notifyFinish() has fired</i><span style=3D"font-style:normal">=
=C2=A0then you get that error.</span></div><div><span style=3D"font-style:n=
ormal"><br></span></div><div><span style=3D"font-style:normal">If you chang=
e your code to only call `.finish()` if the deferred returned by notifyFini=
sh() has not fired or failed yet, do you get the error?=C2=A0 It&#39;s poss=
ible that you still do, in which case, there&#39;s a bug in Twisted that ne=
eds to be fixed.</span></div><br><blockquote type=3D"cite"><div><div dir=3D=
"ltr"><div>I do want to convey my thanks for the help. Really appreciate it=
.</div></div></div></blockquote><br></div><div>Thanks for using Twisted :)<=
/div><div>-g</div><div><br></div></div>____________________________________=
___________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Twist=
[email protected]</a><br>
<a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-pytho=
n" rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/m=
ailman/listinfo/twisted-python</a><br>
</blockquote></div>
</blockquote></div>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Twist=
[email protected]</a><br>
<a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-pytho=
n" rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/m=
ailman/listinfo/twisted-python</a><br>
</blockquote></div></div>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Twist=
[email protected]</a><br>
<a href=3D"https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-pytho=
n" rel=3D"noreferrer" target=3D"_blank">https://twistedmatrix.com/cgi-bin/m=
ailman/listinfo/twisted-python</a><br>
</blockquote></div>
</blockquote></div>

--000000000000102204058f62e912--

--===============4540812947757986767==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC1Q
eXRob24gbWFpbGluZyBsaXN0ClR3aXN0ZWQtUHl0aG9uQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBz
Oi8vdHdpc3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtcHl0
aG9uCg==

--===============4540812947757986767==--