Author davidsarah
Recipients amaury.forgeotdarc, casevh, davidsarah, neologix, pitrou, python-dev, santoso.wijaya, terry.reedy, vstinner
Date 2011-03-27.01:31:40
SpamBayes Score 1.47992e-08
Marked as misclassified No
Message-id <1301189501.83.0.258365398111.issue11395@psf.upfronthosting.co.za>
In-reply-to
Content
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)]
History
Date User Action Args
2011-03-27 01:31:42davidsarahsetrecipients: + davidsarah, terry.reedy, amaury.forgeotdarc, pitrou, vstinner, casevh, neologix, santoso.wijaya, python-dev
2011-03-27 01:31:41davidsarahsetmessageid: <1301189501.83.0.258365398111.issue11395@psf.upfronthosting.co.za>
2011-03-27 01:31:40davidsarahlinkissue11395 messages
2011-03-27 01:31:40davidsarahcreate