Skip to content

[kyo-system] fail a write that reports no progress instead of retrying - #1966

Merged
fwbrasil merged 3 commits into
mainfrom
fix/kyo-system-write-progress
Sep 20, 2026
Merged

fwbrasil merged 3 commits into
mainfrom
fix/kyo-system-write-progress

Conversation

@fwbrasil

Copy link
Copy Markdown
Collaborator

Fixes #1848

Problem

A short write is ordinary: a platform write call may take fewer bytes than offered, and the remainder is re-offered from where it stopped. Zero bytes taken is not a short write. The retry offers the same bytes at the same offset, gets the same answer, and the loop cannot end, so the calling thread hangs with no diagnostic.

Four loops had no progress guard:

  • NioWriteHandle.writeBytes, while buf.hasRemaining do discard(channel.write(buf))
  • NioRawChannel.writeAt, while buf.hasRemaining do written += channel.write(buf, pos + written)
  • NodeWriteHandle.writeBytes, tail-recursive loop(offset + writeSync(...))
  • NodeRawChannel.writeAt, written += writeSync(..., pos + written)

The issue names the two write handles. The two positioned raw channels beside them run the same loop against the same contract, so guarding only two of them would be a divergence of its own.

Solution

Each loop stops at the first write that reports no progress and fails with a new leaf of the filesystem error hierarchy:

case class FileWriteStalledException(path: Path, remaining: ByteSize)(using Frame)
    extends FileSystemException(s"Write made no progress on $path with ${remaining.show} unwritten")
    with FileWriteException derives CanEqual

A named leaf rather than FileIOException: the module's guide reserves FileIOException for a failure no more precise leaf describes, and this one is precise. It also has no diagnostic cause to carry, since no platform exception is thrown, so reporting it as an I/O error meant inventing an IOException to fill that field. A caller can now match the condition rather than parse a message, and remaining gives it the unwritten count, as a ByteSize for the same reason FileLockTimeoutException takes a Duration.

Not a panic: nothing the caller did produces this, it is already inside the declared FileWriteException of every write, so no signature and no caller changes, and the caller's ordinary write-failure handling is where it belongs.

Bounding the retry count, the issue's other suggestion, only moves the stall: a bounded loop that gives up still has to report something, and the something is this failure. Guarding at the first zero-byte return is the same report without the spin.

The four loops keep their own shapes, the buffer-cursor form on JVM and Native and the offset form on JavaScript and Wasm, positioned and unpositioned.

Notes

Four leaves, two per platform source set, each asserting both the failure and its remaining, and that the platform write was attempted exactly once, which is what proves there is no retry:

  • jvm-native/src/test/scala/kyo/internal/PathPlatformSpecificTest.scala, a FileChannel subclass whose write overloads count their calls, return 0, and throw past a small bound so a regression cannot hang the suite. It sits in jvm-native, so JVM and Native run the same file.
  • js/src/test/scala/kyo/internal/PathPlatformSpecificJsTest.scala, patching fs.writeSync for the duration of the leaf and restoring it in finally. JS only, because Wasm links as an ES module whose namespace is sealed and cannot be patched; the file records that.

FileSystemExceptionTest carries the new leaf in its field-retention list and its category assertions, and in the exhaustive FileWriteException match, which has no wildcard precisely so that a new subtype cannot be added without being accounted for there.

The kyo-net loops that also test hasRemaining are untouched and are not defects: those are non-blocking socket writes where a zero-byte return is the documented want-write signal, and each already routes it to a WriteResult rather than retrying.

Green on JVM, JS, Wasm and Native (584 kyo-system tests on Native, 574 on Wasm, 608 on JS), plus the kyo-system README doctest.

## Problem

A write that consumed zero bytes was retried with the same bytes at the same
offset, so the loop could not end and the calling thread hung with no
diagnostic. Four loops had no guard: both write handles and both positioned raw
channels, across the NIO and Node leaves. The issue names the two handles; the
raw channels beside them run the same loop against the same contract.

## Solution

Each loop stops at the first write that makes no progress and fails with a new
leaf, `FileWriteStalledException(path, remaining)`, which is a
`FileWriteException` and so already inside every write's declared error type.

A named leaf rather than `FileIOException`, because the module's guide reserves
that one for failures no precise leaf describes, and because there is no
platform exception here to carry as its cause. Not a panic: nothing the caller
did produces it. Bounding the retry count instead, the issue's other
suggestion, only moves the stall, since a bounded loop that gives up still has
to report this.

## Notes

Each reproduction drives a write that takes no bytes and asserts it was
attempted exactly once, which is what proves the retry is gone. The stubs throw
past a small call bound so a regression fails on the count rather than hanging
the suite.

The new leaf is public surface under `FileWriteException`, so a wildcard-free
exhaustive match on that trait now warns. `FileSystemExceptionTest` keeps such a
match deliberately, as the alarm for exactly this.

The `kyo-net` loops that also test `hasRemaining` are untouched and are not
defects: a zero-byte return there is the documented want-write signal, already
routed to a `WriteResult`.

Green on JVM, JS, Wasm and Native.

Fixes #1848
@fwbrasil
fwbrasil force-pushed the fix/kyo-system-write-progress branch from 28b9829 to 4bc6864 Compare September 20, 2026 13:54
@fwbrasil
fwbrasil merged commit cbce9e5 into main Sep 20, 2026
2 of 14 checks passed
@fwbrasil
fwbrasil deleted the fix/kyo-system-write-progress branch September 20, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write handle retry loops spin forever if a write returns zero bytes

1 participant