Skip to content

Commit 74ec52d

Browse files
MrSurlydpgeorge
authored andcommitted
extmod/modussl: Add finaliser support for ussl objects.
Per the comment found here micropython#209 (comment), this patch adds finaliser code to prevent memory leaks from ussl objects, which is especially useful when memory for a ussl context is allocated outside the uPy heap. This patch is in-line with the finaliser code found in many modsocket implementations for various ports. This feature is configured via MICROPY_PY_USSL_FINALISER and is disabled by default because there may be issues using it when the ussl state *is* allocated on the uPy heap, rather than externally.
1 parent 05a2bb8 commit 74ec52d

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

extmod/modussl_axtls.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ struct ssl_args {
5151
STATIC const mp_obj_type_t ussl_socket_type;
5252

5353
STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
54+
#if MICROPY_PY_USSL_FINALISER
55+
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
56+
#else
5457
mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
58+
#endif
5559
o->base.type = &ussl_socket_type;
5660
o->buf = NULL;
5761
o->bytes_left = 0;
@@ -178,6 +182,9 @@ STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {
178182
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
179183
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
180184
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) },
185+
#if MICROPY_PY_USSL_FINALISER
186+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) },
187+
#endif
181188
};
182189

183190
STATIC MP_DEFINE_CONST_DICT(ussl_socket_locals_dict, ussl_socket_locals_dict_table);

extmod/modussl_mbedtls.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
111111

112112

113113
STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
114+
#if MICROPY_PY_USSL_FINALISER
115+
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
116+
#else
114117
mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
118+
#endif
115119
o->base.type = &ussl_socket_type;
116120

117121
int ret;
@@ -272,6 +276,9 @@ STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {
272276
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
273277
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
274278
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) },
279+
#if MICROPY_PY_USSL_FINALISER
280+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) },
281+
#endif
275282
{ MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) },
276283
};
277284

py/mpconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ typedef double mp_float_t;
11091109

11101110
#ifndef MICROPY_PY_USSL
11111111
#define MICROPY_PY_USSL (0)
1112+
// Whether to add finaliser code to ussl objects
1113+
#define MICROPY_PY_USSL_FINALISER (0)
11121114
#endif
11131115

11141116
#ifndef MICROPY_PY_WEBSOCKET

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy