ussl.wrap_socket: option for pre-allocated buffer
Problem Description
Wrapping a socket requires ~16kB of RAM.
When memory is heavily fragmented, wrapping a socket will fail:
>>> secure_socket = ussl.wrap_socket(...)
MemoryError: memory allocation failed, allocating 16725 bytes
Proposed Solution
Instead, it could be possible to pre-allocate the memory (early in runtime), and share that with the wrapped socket... for example:
buf = bytearray(16725)
secure_socket = ussl.wrap_socket(..., buf=buf)
urequests with ussl causes an OSError
When trying to get or post (to https links) every n amount of seconds using the urequests library it works fine and make around 10-15 successful requests then suddenly it raises an OSError -1 . And The Error points me to line 56 in the urequests code which is:
s = ussl.wrap_socket(s, server_hostname=host)
Normally I would catch any exception that occurs and try to POST it again or POST another packet.
However, if an OSError occurs all the next Requests fails too which forces me to reset the board.
Sorry for the slow response on this... which board and version of MicroPython are you using? It may be that the device is running out of memory and can't create an SSL socket.
@dpgeorge I am working on WiPy 2.0 Board, I think you are right. Friend advised me to use
gc.collect()but I don't know when should I use it. Could you confirm that might help ? or there is another better way ?If you're using a WiPy 2.0 then I suggest you report the issue at https://forum.pycom.io/ because the bug could be in their implementation of the ussl module (urequests in know to work well on other MicroPython platforms).
Closing then.