Skip to content

gh-140795: Remove 'exc' field in SSLObject - #143491

Merged
colesbury merged 4 commits into
python:mainfrom
colesbury:gh-140795-ssl-exc
Jan 12, 2026
Merged

gh-140795: Remove 'exc' field in SSLObject#143491
colesbury merged 4 commits into
python:mainfrom
colesbury:gh-140795-ssl-exc

Conversation

@colesbury

@colesburycolesbury commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

The 'exc' field was used by our debug SSL callbacks. Keep the exception in the normal per-thread state to avoid shared mutable state between threads.

This also avoids a reference count leak if the Python callback raised an exception because it can be called multiple times per SSL operation.

The 'exc' field was used by our debug SSL callbacks. Keep the exception
in the normal per-thread state to avoid shared mutable state between
threads.
This also avoids a reference count leak if the Python callback raised an
exception because it can be called multiple times per SSL operation.
@colesbury

Copy link
Copy Markdown
ContributorAuthor

This is like:

But for the exc field.

@colesbury
colesbury marked this pull request as ready for review January 6, 2026 19:07
Comment threadModules/_ssl.c
Comment threadModules/_ssl.c Outdated
Comment threadModules/_ssl.c
Comment threadModules/_ssl.c
Comment threadModules/_ssl/debughelpers.c
@colesburycolesbury added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jan 8, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 8cd4d98 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F143491%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jan 8, 2026

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I would feel safer with proposed assertions to check that assumptions remain valid in the future if the code is modified.

Comment threadModules/_ssl.c
}
Py_RETURN_NONE;
error:
Py_XDECREF(sock);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might add: assert(exc == NULL);.

Comment threadModules/_ssl.c
}
return PyLong_FromSize_t(count);
error:
Py_XDECREF(sock);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might add: assert(exc == NULL);.

Comment threadModules/_ssl.c Outdated
int retval;
int sockstate;
_PySSLError err;
PyObject *exc;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other functions:

Suggested change
PyObject*exc;
PyObject*exc=NULL;

Comment threadModules/_ssl.c
@@ -3002,7 +3012,6 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
}

error:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might add: assert(exc == NULL); here and in the done: label.

Comment threadModules/_ssl.c
@@ -3126,7 +3144,6 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self)

error:
Py_XDECREF(sock);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might add: assert(exc == NULL);.

@vstinner

Copy link
Copy Markdown
Member

The 4 failed builbots are unrelated to this change.

Windows buildbots failed to download zlib external dependency:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1081)

Android buildbot: test_urllibnet failed with:

ERROR: test_getcode (test.test_urllibnet.urlopenNetworkTests.test_getcode)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.15/test/test_urllibnet.py", line 107, in test_getcode
self.assertEqual(e.exception.code, 404)
^^^^^^^^^^^^^^^^
AttributeError: 'URLError' object has no attribute 'code'

@colesbury
colesbury enabled auto-merge (squash) January 12, 2026 16:52
@colesbury
colesbury merged commit 7d155d7 into python:mainJan 12, 2026
44 checks passed
@colesbury
colesbury deleted the gh-140795-ssl-exc branch January 12, 2026 17:53
reidenong pushed a commit to reidenong/cpython that referenced this pull request Jan 12, 2026
The 'exc' field was used by our debug SSL callbacks. Keep the exception
in the normal per-thread state to avoid shared mutable state between
threads.
This also avoids a reference count leak if the Python callback raised an
exception because it can be called multiple times per SSL operation.
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
The 'exc' field was used by our debug SSL callbacks. Keep the exception
in the normal per-thread state to avoid shared mutable state between
threads.
This also avoids a reference count leak if the Python callback raised an
exception because it can be called multiple times per SSL operation.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@colesbury@bedevere-bot@vstinner@picnixz