← index #7878Issue #206
Related · medium · value 0.286
QUERY · ISSUE

ussl.wrap_socket: option for pre-allocated buffer

openby fragmuffinopened 2021-10-04updated 2022-05-13

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)
CANDIDATE · ISSUE

urequests with ussl causes an OSError

closedby Ahmedkalnaggaropened 2017-08-19updated 2017-11-02

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.

4 comments
dpgeorge · 2017-09-06

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.

Ahmedkalnaggar · 2017-09-09

@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 ?

dpgeorge · 2017-09-10

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).

pfalcon · 2017-11-02

Closing then.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied