Skip to content

Conversation

@colesbury
Copy link
Contributor

@colesburycolesbury commented Jan 6, 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.

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
ContributorAuthor

This is like:

But for the exc field.

@colesburycolesbury marked this pull request as ready for review January 6, 2026 19:07
@colesburycolesbury added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jan 8, 2026
@bedevere-bot
Copy link

🤖 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
Copy link
Member

@vstinnervstinner left a comment

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.

}
Py_RETURN_NONE;
error:
Py_XDECREF(sock);
Copy link
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);.

}
return PyLong_FromSize_t(count);
error:
Py_XDECREF(sock);
Copy link
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);.

Modules/_ssl.c Outdated
int retval;
int sockstate;
_PySSLError err;
PyObject *exc;
Copy link
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;

@@ -3002,7 +3012,6 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
}

error:
Copy link
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.

@@ -3126,7 +3144,6 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self)

error:
Py_XDECREF(sock);
Copy link
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
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' 

@colesburycolesbury enabled auto-merge (squash) January 12, 2026 16:52
@colesburycolesbury merged commit 7d155d7 into python:mainJan 12, 2026
44 checks passed
@colesburycolesbury 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.
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