Skip to content

Commit a641016

Browse files
committed
Fix mmap closing
1 parent 569bb1f commit a641016

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Src/IronPython.Modules/mmap.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ public void resize(long newsize) {
812812
if (_handle.IsInvalid) {
813813
throw PythonNT.GetOsError(PythonErrno.EBADF);
814814
}
815+
if (_view.Capacity == newsize) {
816+
// resizing to the same size
817+
return;
818+
}
815819
if (newsize == 0) {
816820
// resizing to an empty mapped region is not allowed
817821
throw PythonNT.GetOsError(PythonErrno.EINVAL);
@@ -844,14 +848,6 @@ public void resize(long newsize) {
844848
throw WindowsError(PythonExceptions._OSError.ERROR_INVALID_PARAMETER);
845849
}
846850

847-
if (newsize == 0) {
848-
// resizing to an empty mapped region is not allowed
849-
throw WindowsError(_offset != 0 && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
850-
? PythonExceptions._OSError.ERROR_ACCESS_DENIED
851-
: PythonExceptions._OSError.ERROR_FILE_INVALID
852-
);
853-
}
854-
855851
if (_view.Capacity == newsize) {
856852
// resizing to the same size
857853
return;
@@ -860,6 +856,14 @@ public void resize(long newsize) {
860856
long capacity = checked(_offset + newsize);
861857

862858
try {
859+
if (newsize == 0) {
860+
// resizing to an empty mapped region is not allowed
861+
throw WindowsError(_offset != 0 && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
862+
? PythonExceptions._OSError.ERROR_ACCESS_DENIED
863+
: PythonExceptions._OSError.ERROR_FILE_INVALID
864+
);
865+
}
866+
863867
_view.Flush();
864868
_view.Dispose();
865869
_file.Dispose();
@@ -1143,13 +1147,13 @@ private void EnsureOpen() {
11431147
}
11441148
}
11451149

1146-
private struct MmapLocker : IDisposable {
1150+
private readonly struct MmapLocker : IDisposable {
11471151
private readonly MmapDefault _mmap;
11481152

11491153
public MmapLocker(MmapDefault mmap) {
11501154
_mmap = mmap;
1151-
Interlocked.Increment(ref _mmap._refCount);
11521155
_mmap.EnsureOpen();
1156+
Interlocked.Increment(ref _mmap._refCount);
11531157
}
11541158

11551159
#region IDisposable Members

Tests/modules/io_related/test_mmap.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MmapTest(IronPythonTestCase):
1818
def setUp(self):
1919
super(MmapTest, self).setUp()
2020

21-
self.temp_file = os.path.join(self.temporary_dir, "temp_%d.dat" % os.getpid())
21+
self.temp_file = os.path.join(self.temporary_dir, "temp_mmap_%d.dat" % os.getpid())
2222

2323
def tearDown(self):
2424
self.delete_files(self.temp_file)
@@ -60,6 +60,8 @@ def test_resize_errors(self):
6060
else:
6161
self.assertEqual(cm.exception.strerror, "Invalid argument")
6262

63+
self.assertTrue(m.closed)
64+
6365

6466
def test_resize_errors_negative(self):
6567
with open(self.temp_file, "wb+") as f:
@@ -69,8 +71,10 @@ def test_resize_errors_negative(self):
6971
m = mmap.mmap(f.fileno(), 0, offset=0)
7072
if is_cli or sys.version_info >= (3, 5):
7173
self.assertRaises(ValueError, m.resize, -1)
74+
self.assertFalse(m.closed)
7275
else:
7376
self.assertRaises(OSError, m.resize, -1)
77+
self.assertTrue(m.closed)
7478

7579
m.close()
7680

@@ -93,7 +97,8 @@ def test_resize_errors_offset(self):
9397
m.resize(0)
9498
self.assertEqual(cm.exception.errno, errno.EINVAL) # 22
9599
self.assertEqual(cm.exception.strerror, "Invalid argument")
96-
m.close()
100+
101+
self.assertTrue(m.closed)
97102

98103

99104
run_test(__name__)

0 commit comments

Comments
 (0)