Page 1 of 1

[OLD uasyncio] Problem with StreamWriter.awrite(). Very strange...

Posted: Fri Feb 07, 2020 7:17 pm
by mvincm
Hello,

First of all, I know that there is coming soon new async lib for micropython but I have some implementation with the old one (thanks to Konstantin Belyalov).

I think that there is some small bug (or I can't understand the normal way of working).

Simple case. HTTP async server. When I use StreamWrite.awrite() to send http headers and body (using await) and just after I'm doing await StreamWriter.aclose() to close writer there is situation that TCP connection is going down (RST, ACK) even if a stream with data is not ended. If I add "await asyncio.sleep(1)" before aclose everything is ok because there is a delay and stream have time to be ended.

Dear community and dear Peter (@pythoncoder) could you take a look. Migration to new version is, of course, an option but a lot of code to rewrite. Thanks in advance.

Code:
An exception to send error (HTTP 404 in my case):
https://github.com/belyalov/tinyweb/blo ... #L466-L470

And "finally" statement with "aclose":
https://github.com/belyalov/tinyweb/blo ... #L482-L489

Best regards,
MvincM

Re: [OLD uasyncio] Problem with StreamWriter.awrite(). Very strange...

Posted: Sun Feb 09, 2020 3:25 pm
by pythoncoder
If you think you've hit a bug in tinyweb I suggest you raise an issue. I haven't used or studied this (I have used picoweb) so I'm not well placed to debug it. If you're not using task cancellation I think it's unlikely that you've hit a bug in uasyncio V2.0. At a glance the finally clause does seem a little drastic, closing a socket which might still be busy, but I haven't time to investigate this right now.

Regarding the new uasyncio while it is a substantial improvement, as I understand it the old version won't go away. Until the new version is formally released it's hard to comment on porting issues. I have ported some substantial applications to an experimental release and it was extremely easy, but it will depend on the exact functionality in use.

Re: [OLD uasyncio] Problem with StreamWriter.awrite(). Very strange...

Posted: Mon Feb 10, 2020 7:21 pm
by mvincm
Hello,

Thanks for your interest!

Re: tinyweb bug
Let's forget for a while about the bug in tinyweb. Some theoretical discussion. I can totally agree that "finally" is not the best place to close a socket (what "StreamWriter.aclose()" exactly do). But isn't that true that if we use function StreamWrite.awrite() as a "await" in "try" it should first send what it has in buffer (whole message) end if it has done go to the "finally" and close a socket? Isn't it a proper and expected way? The code in tinyweb is simple and pointed by me code with "try" and "finally" exactly do what I describe. Maybe I'm missing something from async idea (it doesn't matter which version I use). It is important for me to understand the model. Is there a way how can I check that everything is sent by StreamWrite.awrite()? If StreamWrite.aclose() is closing socket even if there is some data to send and don't wait for the end of writing by awrite, there must be some way to get the info that is safe to close a socket. BTW... workaround with asyncio.sleep() is just a workaround because I don't have certainty that I sleep just enough.

Re: new version
I will try to learn it and use it and maybe port tinyweb to new version. But my doubts, describe below are still there ;)

Best regards,
MvincM

Web programming

Posted: Tue Feb 11, 2020 7:27 am
by pythoncoder
This is a question about web programming which is rather outside of my experience. The question seems to me to be this: if you send data to an internet server, how can you establish when it has been received? I think the answer depends on the protocol. If the website supports a protocol such as MQTT then you can expect an acknowledge packet and can time out if it is not received.

Alas I don't know the answer for HTTP/HTTPS. Anyone?

Re: [OLD uasyncio] Problem with StreamWriter.awrite(). Very strange...

Posted: Tue Feb 11, 2020 9:39 am
by mvincm
Hello,

Sorry for the lack of information. Of course, it is important.

First of all. I'm working with HTTP and in this particular case, I respond to GET request. "Server" raises an exception in the case where there is no file/path/url. Tipical 404 error (Not found).

So if there is no such a path/file there is an exception and 404 is raised. Then I should send at least HTTP response with 404 header. In a time when the response going to the client (await StreamWrite.awrite()), "finally" statement close socket without waiting for awrite job is finished.

Best regards,
MvincM