Dialog about overrun of a stack-based buffer on Windows Server 2022 (caused by `asyncio` / `socket`)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Crash report
What happened?
On Windows Server 2022, when exiting a Python process, we sporadically get a dialog with the title "python.exe - System Error", which reads "The system detected an overrun of a stack-based buffer in this application. This overrun could potentially allow a malicious user to gain control of this application."
We do not get this error with Windows Server 2019.
The issue is very sporadically. Even with the following reproducers, we only experience the issue every 1,000 to 100,000 runs on our systems.
Our initial reproducer was:
import asyncio
async def f():
pass
asyncio.run(f())
We could narrow it down further to:
import socket
socket.socketpair()
(The issue does also occur if I close the sockets returned from socketpair.)
The issue can also be reproduced with the following snippet extracted from socket.socketpair():
import socket
l = socket.socket()
l.bind(("127.0.0.1",0))
l.listen()
c = socket.socket()
c.setblocking(False)
try:
c.connect(l.getsockname())
except BlockingIOError:
pass
Setting the socket to unblocking seems to be essential. I could not reproduce the issue without setting it to unblocking.
Further, calling os._exit() prevents the issue. I digged a little further and found that not calling WSACleanup() in os_cleanup() in socketmodule.c prevents the issue.
CPython versions tested on:
3.11, 3.12, 3.13, 3.14
Operating systems tested on:
Windows
Output from running 'python -VV' on the command line:
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in socketmodule.c at os_cleanup(), focusing on the WSACleanup() call. Run the minimal socketpair() or nonblocking socket reproducer repeatedly on Windows Server 2022 and compare behavior with Windows Server 2019. Done means identifying and correcting the shutdown path so the stack-buffer-overrun dialog no longer occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking, operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100