[issue40720] accessing mmap of file that is overwritten causes bus error

2021-10-18 Thread Irit Katriel


Irit Katriel  added the comment:

Reproduced on 3.11.

--
nosy: +iritkatriel
versions: +Python 3.11 -Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40720] accessing mmap of file that is overwritten causes bus error

2021-03-09 Thread Ronald Oussoren


Ronald Oussoren  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40720] accessing mmap of file that is overwritten causes bus error

2021-03-09 Thread Thomas Grainger


Thomas Grainger  added the comment:

I can confirm this happens on py3.5-3.10

```
import mmap
import pathlib
import tempfile


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("rb") as rf:
mm = mmap.mmap(rf.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
path.write_bytes(b"")
bytes(mm)

if __name__ == "__main__":
main()
```

--
nosy: +graingert
versions: +Python 3.10, Python 3.6, Python 3.7, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40720] accessing mmap of file that is overwritten causes bus error

2020-05-22 Thread Marten H. van Kerkwijk


Marten H. van Kerkwijk  added the comment:

I should probably have added that the bus error happens on linux. On Windows, 
the opening of the file for writing leads to an error, as the file is still 
opened for reading inside the mmap.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40720] accessing mmap of file that is overwritten causes bus error

2020-05-21 Thread Marten H. van Kerkwijk


New submission from Marten H. van Kerkwijk :

While debugging a strange failure with tests and np.memmap, I realized that the 
following direct use of mmap reliably leads to a bus error. Here, obviously 
mmap'ing a file, closing it, opening the file for writing but not writing 
anything, and then again accessing the mmap is not something one should do (but 
a test case did it anyway), but it would nevertheless be nice to avoid a crash!
```
import mmap


with open('test.dat', 'wb') as fh:
fh.write(b'abcdefghijklmnopqrstuvwxyz')


with open('test.dat', 'rb') as fh:
mm = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)


with open('test.dat', 'wb') as fh:
pass  # Note: if something is written, then I get no bus error.


mm[2]
```

--
components: Library (Lib)
messages: 369543
nosy: mhvk
priority: normal
severity: normal
status: open
title: accessing mmap of file that is overwritten causes bus error
type: crash
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com