Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-142516: fix reference leaks in ssl.SSLContext objects
  • Loading branch information
picnixz committed Jan 11, 2026
commit cfca05fae0ef5d83c011f33599d5d57e59af729a
18 changes: 12 additions & 6 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ typedef struct {
int post_handshake_auth;
#endif
PyObject *msg_cb;
PyObject *keylog_filename;
PyObject *keylog_filename; // can be anything accepted by Py_fopen()
BIO *keylog_bio;
/* Cached module state, also used in SSLSocket and SSLSession code. */
_sslmodulestate *state;
Expand Down Expand Up @@ -358,7 +358,7 @@ typedef struct {
PySSLContext *ctx; /* weakref to SSL context */
char shutdown_seen_zero;
enum py_ssl_server_or_client socket_type;
PyObject *owner; /* Python level "owner" passed to servername callback */
PyObject *owner; /* weakref to Python level "owner" passed to servername callback */
PyObject *server_hostname;
/* Some SSL callbacks don't have error reporting. Callback wrappers
* store exception information on the socket. The handshake, read, write,
Expand Down Expand Up @@ -2444,6 +2444,10 @@ static int
PySSL_clear(PyObject *op)
{
PySSLSocket *self = PySSLSocket_CAST(op);
Py_CLEAR(self->Socket);
Py_CLEAR(self->ctx);
Py_CLEAR(self->owner);
Py_CLEAR(self->server_hostname);
Py_CLEAR(self->exc);
return 0;
}
Expand All @@ -2468,10 +2472,7 @@ PySSL_dealloc(PyObject *op)
SSL_set_shutdown(self->ssl, SSL_SENT_SHUTDOWN | SSL_get_shutdown(self->ssl));
SSL_free(self->ssl);
}
Py_XDECREF(self->Socket);
Py_XDECREF(self->ctx);
Py_XDECREF(self->server_hostname);
Py_XDECREF(self->owner);
(void)PySSL_clear(op);
PyObject_GC_Del(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -3594,6 +3595,11 @@ context_traverse(PyObject *op, visitproc visit, void *arg)
PySSLContext *self = PySSLContext_CAST(op);
Py_VISIT(self->set_sni_cb);
Py_VISIT(self->msg_cb);
Py_VISIT(self->keylog_filename);
#ifndef OPENSSL_NO_PSK
Py_VISIT(self->psk_client_callback);
Py_VISIT(self->psk_server_callback);
#endif
Py_VISIT(Py_TYPE(self));
return 0;
}
Expand Down