classification
Title: accessing mmap of file that is overwritten causes bus error
Type: crash Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mhvk
Priority: normal Keywords:

Created on 2020-05-21 21:32 by mhvk, last changed 2020-05-22 20:08 by mhvk.

Messages (2)
msg369543 - (view) Author: Marten H. van Kerkwijk (mhvk) Date: 2020-05-21 21:32
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]
```
msg369635 - (view) Author: Marten H. van Kerkwijk (mhvk) Date: 2020-05-22 20:08
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.
History
Date User Action Args
2020-05-22 20:08:56mhvksetmessages: + msg369635
2020-05-21 21:32:53mhvkcreate