@@ -601,6 +601,11 @@ private bool TryAddRef(bool exclusive, out int reason) {
601601 reason = StateBits . Exporting ;
602602 return false ;
603603 }
604+ if ( exclusive && ( ( oldState & StateBits . RefCount ) > StateBits . RefCountOne ) ) {
605+ // mmap in non-exclusive use, temporarily no exclusive use allowed
606+ reason = StateBits . Exclusive ;
607+ return false ;
608+ }
604609 Debug . Assert ( ( oldState & StateBits . RefCount ) > 0 , "resurrecting disposed mmap object (disposed without being closed)" ) ;
605610
606611 newState = oldState + StateBits . RefCountOne ;
@@ -915,6 +920,8 @@ public void resize(long newsize) {
915920 throw PythonOps . ValueError ( "new size out of range" ) ;
916921 }
917922
923+ long capacity = checked ( _offset + newsize ) ;
924+
918925 if ( _handle is not null
919926 && ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) || RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ) ) {
920927 // resize on Posix platforms
@@ -926,6 +933,7 @@ public void resize(long newsize) {
926933 // resizing to the same size
927934 return ;
928935 }
936+
929937 if ( newsize == 0 ) {
930938 // resizing to an empty mapped region is not allowed
931939 throw PythonNT . GetOsError ( PythonErrno . EINVAL ) ;
@@ -936,14 +944,14 @@ public void resize(long newsize) {
936944
937945 // Resize the underlying file as needed.
938946 int fd = unchecked ( ( int ) _handle . DangerousGetHandle ( ) ) ;
939- PythonNT . ftruncateUnix ( fd , newsize ) ;
947+ PythonNT . ftruncateUnix ( fd , capacity ) ;
940948
941949#if NET8_0_OR_GREATER
942- _file = MemoryMappedFile . CreateFromFile ( _handle , _mapName , newsize , _fileAccess , HandleInheritability . None , leaveOpen : true ) ;
950+ _file = MemoryMappedFile . CreateFromFile ( _handle , _mapName , capacity , _fileAccess , HandleInheritability . None , leaveOpen : true ) ;
943951#else
944952 _sourceStream ? . Dispose ( ) ;
945953 _sourceStream = new FileStream ( new SafeFileHandle ( ( IntPtr ) fd , ownsHandle : false ) , FileAccess . ReadWrite ) ;
946- _file = CreateFromFile ( _sourceStream , _mapName , newsize , _fileAccess , HandleInheritability . None , leaveOpen : true ) ;
954+ _file = CreateFromFile ( _sourceStream , _mapName , capacity , _fileAccess , HandleInheritability . None , leaveOpen : true ) ;
947955#endif
948956 _view = _file . CreateViewAccessor ( _offset , newsize , _fileAccess ) ;
949957 return ;
@@ -963,8 +971,6 @@ public void resize(long newsize) {
963971 return ;
964972 }
965973
966- long capacity = checked ( _offset + newsize ) ;
967-
968974 try {
969975 if ( newsize == 0 ) {
970976 // resizing to an empty mapped region is not allowed
0 commit comments