darwin: use RLIMIT_STACK for fsevents pthread

This fixes `SIGBUS` crashes on macOS 10.15 due a new change in
`FSEvents.framework` that makes it allocate a large stack array for
event paths. (See the linked nodejs/node issue for details on the
`FSEvents.framework` memory requirements change itself.)

The existing size (`4 * PTHREAD_STACK_MIN` or 32KB) causes a stack
overflow when more than ~1000 events are received at once. Setting this
to `uv__thread_stack_size()` increases it to 8192KB (by default) on
64-bit machines. This value can be configured at runtime on macOS with
`ulimit -s <size-kb>`.

The 32KB limit was originally added to reduce virtual memory
fragmentation on 32-bit systems, which is not a concern on 64-bit
systems.

Fixes: nodejs/node#37697
Refs: joyent/libuv#964
PR-URL: https://github.com/libuv/libuv/pull/3132
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
This commit is contained in:
Brandon Cheng 2021-03-13 16:55:16 -05:00 committed by Santiago Gimeno
parent 054a495351
commit 608ac2e433
No known key found for this signature in database
GPG Key ID: F28C3C8DA33C03BE

View File

@ -642,15 +642,15 @@ static int uv__fsevents_loop_init(uv_loop_t* loop) {
}
/* In the unlikely event that pthread_attr_init() fails, create the thread
* with the default stack size. We'll use a little more address space but
* that in itself is not a fatal error.
* with the default stack size. We'll likely use less address space but that
* in itself is not a fatal error.
*/
attr = &attr_storage;
if (pthread_attr_init(attr))
attr = NULL;
if (attr != NULL)
if (pthread_attr_setstacksize(attr, 4 * PTHREAD_STACK_MIN))
if (pthread_attr_setstacksize(attr, uv__thread_stack_size()))
abort();
loop->cf_state = state;