Skip to content

Commit 5e5fe76

Browse files
committed
fix(serialization): Rearrange pwrite retry logic
1 parent 258ca7c commit 5e5fe76

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

tensorizer/serialization.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,41 +3526,40 @@ def _pwrite_syscall(
35263526
verify if isinstance(verify, int) else self._buffer_size(data)
35273527
)
35283528
bytes_just_written: int = os.pwrite(self._fd, data, offset)
3529-
bytes_written += bytes_just_written
3529+
requested_write_size: int = self._buffer_size(data)
3530+
if bytes_just_written > 0:
3531+
bytes_written += bytes_just_written
35303532
attempts: int = 0
35313533
while bytes_written < expected_bytes_written and attempts < 3:
35323534
# Writes larger than ~2 GiB may not complete in a single pwrite call
3533-
offset += bytes_just_written
3534-
with self._mv_suffix(data, bytes_written) as mv:
3535-
mv_size: int = mv.nbytes
3536-
bytes_just_written = os.pwrite(self._fd, mv, offset)
35373535
if bytes_just_written > 0:
3538-
bytes_written += bytes_just_written
3536+
offset += bytes_just_written
35393537
else:
35403538
# In case pwrite returns something strange
35413539
(logger.error if bytes_just_written < 0 else logger.info)(
35423540
(
3543-
"pwrite: Supplementary write of %d bytes returned %d"
3541+
"pwrite: Write of %d bytes returned %d"
35443542
" with %d/%d bytes written (offset: %d)"
35453543
),
3546-
mv_size,
3544+
requested_write_size,
35473545
bytes_just_written,
35483546
bytes_written,
35493547
expected_bytes_written,
35503548
offset,
35513549
)
35523550
if bytes_just_written == 0:
3553-
if mv_size == 0:
3551+
if requested_write_size == 0:
35543552
logger.error("pwrite: Attempted to write 0 bytes")
35553553
break
35563554
attempts += 1
3557-
logger.debug(
3558-
"pwrite: %s (attempt %d/3)",
3559-
"Retrying" if attempts < 3 else "Not retrying",
3560-
attempts,
3561-
)
3555+
logger.debug("pwrite: Retrying (attempt %d/3)", attempts)
35623556
if attempts > 1:
35633557
time.sleep(0.2)
3558+
with self._mv_suffix(data, bytes_written) as mv:
3559+
requested_write_size = mv.nbytes
3560+
bytes_just_written = os.pwrite(self._fd, mv, offset)
3561+
if bytes_just_written > 0:
3562+
bytes_written += bytes_just_written
35643563
if isinstance(verify, int) or verify:
35653564
self._verify_bytes_written(bytes_written, expected_bytes_written)
35663565
return bytes_written

0 commit comments

Comments
 (0)