Title: multiprocessing.shared_memory: MacOS crashes by running attached Python code
Type: crash Stage:
Components: C API Versions: Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: davin, vinay0410
Priority: normal Keywords:

Created on 2020-02-08 14:18 by vinay0410, last changed 2020-02-12 23:06 by davin.

Messages (2)
msg361629 - (view) Author: Vinay Sharma (vinay0410) * Date: 2020-02-08 14:18
Consider the following python Code.

from multiprocessing.shared_memory import SharedMemory
shm = SharedMemory(name='test-crash', create=True, size=1000000000000000000)

This causes macOS Catalina, Mojave to freeze and then crash. Although, this works fine on ubuntu.

After, debugging I realised that this is due to the ftruncate call. I could replicate the same by calling os.ftruncate and also using ftruncate in C code.

Following C++ code also crashes, which confirms that ftruncate in macOS is broken:

#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>

int main() {

    int shm_fd = shm_open("/test-shm2", O_CREAT | O_RDWR, 0666);

    if (shm_fd == -1) {
        throw "Shared Memory Object couldn't be created or opened";

    int rv = ftruncate(shm_fd, (long long)1000000000000000000);


Should python, in any way handle this, so as to prevent any crashes using python code.
msg361924 - (view) Author: Davin Potts (davin) * (Python committer) Date: 2020-02-12 23:06
My sense is that it would be nice if we can catch this before ftruncate does something nasty.

Where else is ftruncate used in CPython that this could similarly trigger a problem?  How is it handled there (or not)?
Date User Action Args
2020-02-12 23:06:19davinsetmessages: + msg361924
2020-02-10 10:58:45vstinnersettitle: MacOS crashes by running attached Python code -> multiprocessing.shared_memory: MacOS crashes by running attached Python code
2020-02-10 10:58:26vstinnersetnosy: - vstinner
2020-02-08 14:51:44SilentGhostsetnosy: + vstinner, davin
2020-02-08 14:18:41vinay0410settype: crash
2020-02-08 14:18:24vinay0410create