Skip to content

Error condition when the connection will be closed by the server while reading the response #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amotl opened this issue Mar 24, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@amotl
Copy link

amotl commented Mar 24, 2020

Dear people of Adafruit,

thanks a bunch for taking over from urequests and giving that baseline implementation so much love within your Adafruit_CircuitPython_Requests module. We are about to switch over to your implementation within our Terkin Datalogger.

It came to our attention that the baseline implementation might suffer from a minor issue manifesting in a specific error condition. The user kjm reported on the Pycom user forum at [1] that when the server would close the connection while reading the socket, the module will croak like

OSError: [Errno -76] MBEDTLS_ERR_NET_RECV_FAILED
AttributeError: NoneType object has no attribute close

within the Response.content() method [2]. That will also apply to this implementation. So, I would like to suggest to replace the two lines [3] with just self.close().

With kind regards,
Andreas.

[1] https://forum.pycom.io/topic/5755/micropython-error-trail-confusing-me
[2] https://github.com/micropython/micropython-lib/blob/829f53dc9ea633f3a906cc078027fe7c5248b04e/urequests/urequests.py#L16-L24
[3] https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/v1.1.0/adafruit_requests.py#L115-L116

@amotl
Copy link
Author

amotl commented Mar 24, 2020

When looking at the implementation of the Response.close() method [1], we can see it deviates from the original implementation [2] by adding a gc.collect() just before returning.

To make the GC actually free heap memory, you probably also added del self.socket and del self._cached beforehand. However, these will also remove the instance attributes completely, so this will lead to other problems by methods still trying to use them even when the socket connection has been shut down already. Reproducing this condition should be easy and is obvious to spot - just imagine invoking the Response.close() method twice where even checking for if self.socket would then fail badly like

AttributeError: 'Response' object has no attribute 'socket'

That would even not be unlikely as I see Response.__exit__() will also invoke self.close() - no matter whether it has been called before.

So, I am humbly asking if just setting these two instance attributes to None by self.socket = None and self._cached = None will not be sufficient to make the GC pick up the orphaned Python objects and free them?

If nothing speaks against this, I would like to vote to change this in order to make the library more robust - even in error conditions and unforeseen bogus usage.

[1] https://github.com/adafruit/Adafruit_CircuitPython_Requests/blob/v1.1.0/adafruit_requests.py#L94-L100
[2] https://github.com/micropython/micropython-lib/blob/829f53dc/urequests/urequests.py#L10-L14

@amotl amotl changed the title Minor improvement for a specific error condition when the connection will be closed by the server while reading the response Error condition when the connection will be closed by the server while reading the response Mar 24, 2020
@tannewt
Copy link
Member

tannewt commented Mar 24, 2020

Hi @amotl! Please make a pull request with your suggested changes. This code is relatively new and evolving so we're happy to merge them in. Let us know if you need help getting it going.

@amotl
Copy link
Author

amotl commented Mar 25, 2020

Dear Scott,

thanks for your quick answer.

We are about to switch over to Adafruit_CircuitPython_Requests within our Terkin Datalogger.

We have not been able to make this switchover yet. In the meanwhile, we fixed the uurequests module of Pycopy to suite our needs, see daq-tools/pycopy-lib@89cad7d9 as a reference for that.

As soon as we might be going towards using Adafruit_CircuitPython_Requests, we will be happy to submit an appropriate pull request.

With kind regards,
Andreas.


P.S.: The background of our current work is that we are conceiving a powerful CPython- and pytest-based test harness for our Terkin Datalogger, which might also spark your interest [1,2].

Let us know if you need help getting it going.

From investigating the source code of this module, I definitively have a few remarks about compatibility with CPython and Genuine MicroPython. However, I will open another issue about that in order not to hijack this one.

[1] https://github.com/hiveeyes/terkin-datalogger/tree/master/test
[2] https://community.hiveeyes.org/t/developing-terkin-for-micropython/233/25

@kattni kattni added the enhancement New feature or request label May 4, 2020
@amotl
Copy link
Author

amotl commented Nov 7, 2020

Hi there,

thanks for putting more efforts into this implementation. I can now see that after f32426d and e372574 on behalf of #31 by @tannewt,

is only made at the end of the close() method and nowhere else. That will probably have resolved the condition I was describing here, so I feel it will be safe to close this issue.

Keep up the spirit and with kind regards,
Andreas.

@amotl amotl closed this as completed Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants