Skip to content

fix: close FileIO source before completing its IOResult - #3808

Merged
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:file-source-close-before-io-result
Nov 26, 2026
Merged

He-Pin merged 1 commit into
apache:mainfrom
He-Pin:file-source-close-before-io-result

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 23, 2026

Copy link
Copy Markdown
Member

Motivation

The materialized FileIO source result can be used to decide when an input file is no longer in use. It could previously complete while the channel was still open.

Modification

Attempt to close an opened input channel before completing the materialized result. Preserve the earlier read and cancellation result shapes and add EOF and cancellation tests that directly observe a real open file channel when that result completes.

Result

Consumers can wait for the materialized result before taking the next step on the file. Both new tests observed an open channel before the fix and now pass.

Risk

This changes when the result is published, not when downstream receives data or completion. If close itself fails, the original failure still takes precedence. No Windows unlink behavior is claimed.

Tests

  • FileSourceSpec: 17 passed with the final changed Scala 2.13.18/JDK 17 classes over cached support classes. Both new cases failed on the test-only baseline.
  • Scoped scalafmt and git diff --check passed. No full build or MiMa claim.

References

Follows FileOutputStage's close-before-result convention; the comparison with Netty #10212 surfaced this independent lifecycle issue.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall. I checked the open failure, read failure, EOF, cancellation, downstream failure and abrupt termination paths, and the result keeps its shape in each. One nit inline.

Motivation: Applications can use the materialized FileIO source result
to coordinate later access to the file. The result could be completed
before an opened channel had been closed.

Modification: Attempt to close the channel before publishing the
result. Log a non-fatal close error with its cause, while keeping any
earlier read, open, or downstream error as the primary result.

Result: Consumers can observe that the close attempt has finished.
A close-only failure remains visible in the materialized IOResult
status without changing the downstream terminal signal.

Tests: On JDK 17, both Scala 2.13.18 and 3.3.9 passed FileSourceSpec
and FileSourceCloseFailureSpec in a focused run. The new cases observe
the close error log after EOF and after a previous read failure.

References: FileOutputStage's close-before-result convention, PJ's
review on apache#3808, and the IOResult deprecation cleanup in apache#2054.
@He-Pin
He-Pin force-pushed the file-source-close-before-io-result branch from 10174ca to bf8b46e Compare November 26, 2026 13:00
He-Pin added a commit to He-Pin/incubator-pekko that referenced this pull request Nov 26, 2026
Motivation:
A positional file-channel read can return fewer bytes than requested
without reaching EOF. Treating that as EOF could truncate the source.

Modification:
Keep filling the existing buffer after positive progress. Emit a full
chunk, or the remaining bytes on a terminal read, without changing the
read-ahead limit or materialized result and close ordering.

Result:
A sequence of short positive reads yields full chunks until the final
chunk. The existing zero-read termination policy is unchanged.

Tests:
FileSourceSpec: 18 passed on Scala 2.13.18 and 3.3.9 (JDK 17), compiling the exact changed blobs against cached support classes on the apache#3808 branch. Native sbt confirmed the old implementation fails the new test. Scoped style and diff checks passed.

References:
Depends on apache#3808 for the channel-injection test seam.
@He-Pin
He-Pin requested a review from pjfanning November 26, 2026 13:02

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for logging the close failure and adding FileSourceCloseFailureSpec. LGTM, assuming CI passes on the new commit.

@He-Pin
He-Pin merged commit fbbae39 into apache:main Nov 26, 2026
11 checks passed
He-Pin added a commit to He-Pin/incubator-pekko that referenced this pull request Nov 27, 2026
Motivation:
A positional file-channel read can return fewer bytes than requested
without reaching EOF. Treating that as EOF could truncate the source.

Modification:
Keep filling the existing buffer after positive progress. Emit a full
chunk, or the remaining bytes on a terminal read, without changing the
read-ahead limit or materialized result and close ordering.

Result:
Short positive reads yield full chunks until the final chunk. The
existing zero-read termination policy is unchanged.

Tests:
- Native FileSourceSpec and FileSourceCloseFailureSpec: 20/20 each on
  Scala 2.13.18 and 3.3.9, JDK 17.
- The exact pre-fix FileSource blob failed the new case, as expected,
  with the same Scala 2.13 test classes.
- Scoped Scalafmt, license header checks and git diff --check passed.

References:
Refs apache#3808 and apache#3815.
He-Pin added a commit that referenced this pull request Nov 29, 2026
Motivation:
A positional file-channel read can return fewer bytes than requested
without reaching EOF. Treating that as EOF could truncate the source.

Modification:
Keep filling the existing buffer after positive progress. Emit a full
chunk, or the remaining bytes on a terminal read, without changing the
read-ahead limit or materialized result and close ordering.

Result:
Short positive reads yield full chunks until the final chunk. The
existing zero-read termination policy is unchanged.

Tests:
- Native FileSourceSpec and FileSourceCloseFailureSpec: 20/20 each on
  Scala 2.13.18 and 3.3.9, JDK 17.
- The exact pre-fix FileSource blob failed the new case, as expected,
  with the same Scala 2.13 test classes.
- Scoped Scalafmt, license header checks and git diff --check passed.

References:
Refs #3808 and #3815.
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.

2 participants