Ronald Oussoren <ronaldousso...@mac.com> added the comment:

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()

----------
nosy: +ronaldoussoren

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40720>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to