Skip to content

Commit c400f72

Browse files
authored
GH-51229: [Python] Raise instead of crashing on unopened resize (#51246)
### Rationale for this change Calling resize on a memory-mapped file that has never been opened takes down the Python process with a SIGSEGV, so an application cannot catch or recover from it. ### What changes are included in this PR? The resize path now applies the same open-state check the other file operations already use, before it reaches the native resize call. A directly constructed object raises ValueError. ### Are these changes tested? The regression runs in-process and checks ValueError and its message with pytest.raises. It does not spawn a subprocess. Separate, 20-second-bounded processes reproduced the original crash and the exception after the fix. Both used native Arrow 26.0.0-SNAPSHOT; the reused patched extension has byte-identical resize source to this PR. <details> <summary>Raw logs</summary> ```console $ python -c 'import pyarrow as pa; pa.MemoryMappedFile().resize(0)' Before: returncode=-11 After: ValueError: I/O operation on closed file returncode=1 $ cd python $ python -m pytest pyarrow/tests/test_io.py -k memory_map_resize -q .. [100%] 2 passed, 161 deselected, 1 warning in 1.02s ``` </details> ### Are there any user-facing changes? Yes. Misuse of a directly constructed memory-mapped file now raises a Python exception instead of terminating the process. **This PR contains a "Critical Fix".** It fixes a process crash reachable from ordinary Python-level object state. * GitHub Issue: #51229 Lead-authored-by: 1fanwang <1fannnw@gmail.com> Co-authored-by: Stefan Wang <1fannnw@gmail.com> Signed-off-by: AlenkaF <frim.alenka@gmail.com>
1 parent 70f5c26 commit c400f72

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

‎python/pyarrow/io.pxi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ cdef class MemoryMappedFile(NativeFile):
11031103
----------
11041104
new_size : new size in bytes
11051105
"""
1106+
self._assert_open()
11061107
check_status(self.handle.get().Resize(new_size))
11071108

11081109
def fileno(self):

‎python/pyarrow/tests/test_io.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ def test_memory_map_resize(tmpdir):
12231223
assert f.read() == bytes(arr[:SIZE])
12241224

12251225

1226+
def test_memory_map_resize_uninitialized():
1227+
with pytest.raises(ValueError, match="I/O operation on closed file"):
1228+
pa.MemoryMappedFile().resize(0)
1229+
1230+
12261231
def test_memory_zero_length(tmpdir):
12271232
path = os.path.join(str(tmpdir), guid())
12281233
f = open(path, 'wb')

0 commit comments

Comments
 (0)