David-Sarah Hopwood <david-sa...@jacaranda.org> added the comment:

If I understand the bug in the Windows console functions correctly, a limit of 
32767 bytes might not always be small enough. The problem is that if two or 
more threads are concurrently using any console functions (which all use the 
same 64 KiB heap), they could try to allocate up to 32767 bytes plus overhead 
at the same time, which will fail.

I wasn't able to provoke this by writing to sys.stdout.buffer (maybe there is 
locking that prevents concurrent writes), but the following code that calls 
WriteFile directly, does provoke it. GetLastError() returns 8 
(ERROR_NOT_ENOUGH_MEMORY; see 
http://msdn.microsoft.com/en-us/library/ms681382%28v=vs.85%29.aspx), indicating 
that it's the same bug.


# Warning: this test may DoS your system.

from threading import Thread
import sys
from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int
from ctypes.wintypes import BOOL, HANDLE, DWORD, LPVOID, LPCVOID

GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32))
WriteFile = WINFUNCTYPE(BOOL, HANDLE, LPCVOID, DWORD, POINTER(DWORD), LPVOID) \
                        (("WriteFile", windll.kernel32))
GetLastError = WINFUNCTYPE(DWORD)(("GetLastError", windll.kernel32))
STD_OUTPUT_HANDLE = DWORD(-11)
INVALID_HANDLE_VALUE = DWORD(-1).value

hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
assert hStdout is not None and hStdout != INVALID_HANDLE_VALUE

L = 32760
data = b'a'*L

def run():
    n = DWORD(0)
    while True:
        ret = WriteFile(hStdout, data, L, byref(n), None)
        if ret == 0 or n.value != L:
            print(ret, n.value, GetLastError())
            sys.exit(1)

[Thread(target=run).start() for i in range(10)]

----------
nosy: +davidsarah

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11395>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to