This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ronaldoussoren
Recipients graingert, mhvk, ronaldoussoren
Date 2021-03-09.09:58:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1615283914.16.0.22214508995.issue40720@roundup.psfhosted.org>
In-reply-to
Content
What happens here is that the file is truncated, which (more or less) truncates the memory mapping. Accessing a memory mapping beyond the length of the file results in a SIGBUS signal.

I'm not sure if there is much Python can do about this other than shrinking the window for crashes like this by aggressively checking if the file size has changed (but even then a crash will happen if another proces truncates the file between the time the check is done and the memory is actually accessed).

---

Variant of the script that explicitly truncates the file:

def main():
    with tempfile.TemporaryDirectory() as tmp:
        tmp_path = pathlib.Path(tmp)
        path = tmp_path / "eg"

        path.write_bytes(b"Hello, World!")

        with path.open("r+b") as rf:
            mm = mmap.mmap(rf.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
            rf.truncate(0)
            bytes(mm)

if __name__ == "__main__":
    main()
History
Date User Action Args
2021-03-09 09:58:34ronaldoussorensetrecipients: + ronaldoussoren, graingert, mhvk
2021-03-09 09:58:34ronaldoussorensetmessageid: <1615283914.16.0.22214508995.issue40720@roundup.psfhosted.org>
2021-03-09 09:58:34ronaldoussorenlinkissue40720 messages
2021-03-09 09:58:33ronaldoussorencreate