Repository navigation
python3 resource.setrlimit strange behaviour under macOS #78783
Copy link
Copy link
Closed
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifeOS-macstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = <Date 2019-07-02.07:50:14.073>
created_at = <Date 2018-09-07.10:34:46.904>
labels = ['OS-mac', 'type-bug', '3.9', '3.10', '3.11', 'library']
title = 'python3 resource.setrlimit strange behaviour under macOS'
updated_at = <Date 2022-01-09.01:08:28.643>
user = 'https://github.com/marche147'bugs.python.org fields:
activity = <Date 2022-01-09.01:08:28.643>
actor = 'ned.deily'
assignee = 'none'
closed = True
closed_date = <Date 2019-07-02.07:50:14.073>
closer = 'ned.deily'
components = ['Library (Lib)', 'macOS']
creation = <Date 2018-09-07.10:34:46.904>
creator = 'marche147'
dependencies = []
files = []
hgrepos = []
issue_num = 34602
keywords = ['patch']
message_count = 22.0
messages = ['324731', '324737', '324818', '324824', '338868', '339197', '341112', '341114', '341118', '341120', '347110', '347113', '347115', '347117', '347119', '347167', '400984', '401879', '409803', '409879', '410032', '410126']
nosy_count = 9.0
nosy_names = ['ronaldoussoren', 'exarkun', 'ned.deily', 'methane', 'wiz', 'lukasz.langa', 'miss-islington', 'v2m', 'marche147']
pr_nums = ['13011', '13013', '13014', '14546', '14547', '14548', '14549', '27309', '28280', '30490', '30491']
priority = None
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue34602'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifeOS-macstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
marche147 commentedon Sep 7, 2018
Consider the following code:
Running this under macOS with python 3.6.5 gives the following exception:
Nevertheless, when using python 2.7.10 under the same environment, this code works perfectly without exceptions being thrown. Additionally, neither of these operations fail under the same circumstances :
I have also tried to run the above-mentioned python script on linux, also it does not generate exceptions both on python2 (2.7.10) & python3 (3.6.5).
ronaldoussoren commentedon Sep 7, 2018
I get the same error, also with python3.7. Both for homebrew and a python.org installer.
vladima commentedon Sep 8, 2018
I can repro it with a given sample file
Similar settings were added to Python in 335ab5b66f4
marche147 commentedon Sep 8, 2018
Thanks for the repro! It did help for pinpointing the issue.
So I took a little spare time and dived into xnu kernel code, here is my assumption based on what I found (N.B. : My assumption comes from a simple experiment and a brief skim of the source code within 15 minutes or less, it could be seriously wrong since I'm not an expert of XNU kernel, and I currently don't have the time to build and debug it.) :
In bsd/kern/kern_resource.c, there's a function
dosetrlimitwhich handles thesetrlimitrequest, and here's part of it:As we can see, the kernel will try to
mprotectthe memory preceding the stack toVM_PROT_DEFAULT(presumably read & write). I then usedvmmapto see the difference between two binaries compiled with different commands. And the results are :As we can see, it seems that the kernel tried to
mprotect(or we can say, allocate) from the "STACK GUARD" region. So where does this "STACK GUARD" region comes from? Let's see this:bsd/kern/kern_exec.c, in
create_unix_stackfunction (where the kernel creates the stack for a new task, I assume) :So that comes my conclusion: if the binary has a specified default stack size, this
load_result->user_stack_sizewould not be zero (this should be set somewhere inside the mach-o parser/loader, I guess), so the kernel will only map a small page for the "STACK GUARD" region, otherwise the kernel will use the current stack size soft limit (inherited from the parent) as theuser_stack_sizeand calculates aprot_size, which should be (rlim_max - rlim_cur). And of course, the python3 binary was built with default stack size, so the kernel does not provide a huge enough "STACK GUARD" region for thesetrlimitsyscall to allot more stack space than the default stack size.methane commentedon Mar 26, 2019
https://bugs.python.org/issue34602 may be relating to this.
yan12125 commentedon Mar 30, 2019
I guess Inada Naoki was to say https://bugs.python.org/issue36432 in the last comment.
ned-deily commentedon Apr 29, 2019
New changeset 883dfc6 by Ned Deily in branch 'master':
bpo-34602: Avoid failures setting macOS stack resource limit (GH-13011)
883dfc6
miss-islington commentedon Apr 29, 2019
New changeset 52a5b71 by Miss Islington (bot) in branch '3.7':
bpo-34602: Avoid failures setting macOS stack resource limit (GH-13011)
52a5b71
ned-deily commentedon Apr 29, 2019
New changeset fbe2a13 by Ned Deily (Miss Islington (bot)) in branch '3.6':
bpo-34602: Avoid failures setting macOS stack resource limit (GH-13011) (GH-13014)
fbe2a13
ned-deily commentedon Apr 29, 2019
Thanks for the analyses everyone! Also see the discussion in duplicate bpo-36432. I was never able to reproduce the failure on earlier versions of macOS but then it seemed to become a hard failure with the release of 10.14.4. I haven't gone back and tried running the tests on all supported older versions with this reversion in place but those I have did not exhibit any new failures. So we should keep an eye open for reports of segfaults running tests as originally reported in bpo-18075. But better that then not being able to run any tests. "Fixed" in 3.8.0a4, 3.7.4, and 3.6.9 (to allow tests to be run on macOS) by reverting the change for bpo-18075.
18 remaining items
ned-deily commentedon Jan 7, 2022
Since there are so many iterations on this issue already, I think a new issue would be better.
ned-deily commentedon Jan 9, 2022
New changeset b962544 by Miss Islington (bot) in branch '3.10':
bpo-34602: Fix unportable test(1) operator in configure script (GH-30490) (GH-30491)
b962544
ChannyClaus commentedon Jan 3, 2024
hi! sorry if i'm not being blind but is the new issue linked somewhere? i just ran into the same issue on python 3.12 😭
bzamecnik commentedon Apr 11, 2025
Similar problem with setting
resource.RLIMIT_AS:Python 3.11.5 on MacOS 14.7.5 in Conda:
ronaldoussoren commentedon Apr 13, 2025
This is a platform limitation the following program prints
setrlimit: Invalid argumentfor me (macOS 15.4). Theerrnovalue isEINVALwhich the resource module translates to aValueErrorwith the message "current limit exceeds maximum limit".skirpichev commentedon Oct 27, 2025
This seems not working for me also on macOS 14.8 and 15.7. Is MacOS still POSIX-compliant?
[ASan] Disable test that sets call stack on Darwin
boomanaiden154 commentedon Dec 5, 2025
The above example from @ronaldoussoren is failing because the limit being set in the program is
RLIMIT_AS, notRLIMIT_STACK(which the issue is actually about). The following works for me while the above version withRLIMIT_ASdoes not:[ASan] Disable test that sets call stack on Darwin (#170786)
Automerge: [ASan] Disable test that sets call stack on Darwin (#170786)
[ASan] Disable test that sets call stack on Darwin (llvm#170786)
[ASan] Disable test that sets call stack on Darwin (#170786)