Skip to content

Ensure sufficient stack size for worker threads #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions qasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ class _QThreadWorker(QtCore.QThread):
For use by the QThreadExecutor
"""

def __init__(self, queue, num):
def __init__(self, queue, num, stackSize=None):
self.__queue = queue
self.__stop = False
self.__num = num
super().__init__()
if stackSize is not None:
self.setStackSize(stackSize)

def run(self):
queue = self.__queue
Expand Down Expand Up @@ -156,12 +158,20 @@ class QThreadExecutor:
... assert r == 4
"""

def __init__(self, max_workers=10):
def __init__(self, max_workers=10, stack_size=None):
super().__init__()
self.__max_workers = max_workers
self.__queue = Queue()
if stack_size is None:
# Match cpython/Python/thread_pthread.h
if sys.platform.startswith("darwin"):
stack_size = 16 * 2 ** 20
elif sys.platform.startswith("freebsd"):
stack_size = 4 * 2 ** 20
elif sys.platform.startswith("aix"):
stack_size = 2 * 2 ** 20
self.__workers = [
_QThreadWorker(self.__queue, i + 1) for i in range(max_workers)
_QThreadWorker(self.__queue, i + 1, stack_size) for i in range(max_workers)
]
self.__been_shutdown = False

Expand Down
12 changes: 12 additions & 0 deletions tests/test_qthreadexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ def test_ctx_after_shutdown(shutdown_executor):
def test_submit_after_shutdown(shutdown_executor):
with pytest.raises(RuntimeError):
shutdown_executor.submit(None)


def test_stack_recursion_limit(executor):
# Test that worker threads have sufficient stack size for the default
# sys.getrecursionlimit. If not this should fail with SIGSEGV or SIGBUS
# (or event SIGILL?)
def rec(a, *args, **kwargs):
rec(a, *args, **kwargs)
fs = [executor.submit(rec, 1) for _ in range(10)]
for f in fs:
with pytest.raises(RecursionError):
f.result()