Release unused capacity after more short reads (#9089)

Call shrink_to_fit on the result buffer in os.pread, os.getrandom,
WindowsConsoleIO.read, _winapi.PeekNamedPipe, _winapi.ReadFile,
_multiprocessing.recv and the openssl MemoryBIO.read.

Assisted-by: Claude:claude-opus-5-5
Assisted-by: Grok:grok-4.7
This commit is contained in:
Jeong, YunWon authored and GitHub committed 2026-10-13 10:33:55 +09:00
1 parent 85d5956dbb
commit c3e1276229
5 files changed
+13 -4

No files matched your search

+5 -2
View File
@@ -408,8 +408,11 @@ mod _multiprocessing {
#[pyfunction]
fn recv(socket: usize, size: usize, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
host_multiprocessing::recv_socket(socket as host_multiprocessing::RawSocket, size)
.map_err(|_| vm.new_last_os_error())
let mut buf =
host_multiprocessing::recv_socket(socket as host_multiprocessing::RawSocket, size)
.map_err(|_| vm.new_last_os_error())?;
buf.shrink_to_fit();
Ok(buf)
}
#[pyfunction]
+1
View File
@@ -3706,6 +3706,7 @@ mod _ssl {
}
buf.truncate(nbytes as usize);
buf.shrink_to_fit();
Ok(buf)
}
}
+1
View File
@@ -6949,6 +6949,7 @@ mod winconsoleio {
}
buf.truncate(read_len);
buf.shrink_to_fit();
Ok(vm.ctx.new_bytes(buf).into())
}
+4 -2
View File
@@ -768,7 +768,8 @@ mod _winapi {
if size > 0 {
let result = host_winapi::peek_named_pipe(handle.0, Some(size as u32))
.map_err(|e| e.to_pyexception(vm))?;
let buf = result.data.unwrap_or_default();
let mut buf = result.data.unwrap_or_default();
buf.shrink_to_fit();
let bytes: PyObjectRef = vm.ctx.new_bytes(buf).into();
Ok(vm
.ctx
@@ -886,9 +887,10 @@ mod _winapi {
return Ok(result.into());
}
let result = vm
let mut result = vm
.allow_threads(|| host_winapi::read_file(handle.0, size))
.map_err(|e| e.to_pyexception(vm))?;
result.data.shrink_to_fit();
Ok(vm
.ctx
.new_tuple(vec![
+2
View File
@@ -1214,6 +1214,7 @@ pub mod module {
match vm.allow_threads(|| rustpython_host_env::posix::pread(fd, &mut buffer, offset)) {
Ok(n) => {
buffer.truncate(n);
buffer.shrink_to_fit();
return Ok(vm.ctx.new_bytes(buffer));
}
Err(err) if err.raw_os_error() == Some(libc::EINTR) => {
@@ -2976,6 +2977,7 @@ pub mod module {
.map_err(|_| vm.new_last_os_error())?;
buf.set_len(len);
}
buf.shrink_to_fit();
Ok(buf)
}