He-Pin opened a new pull request, #3157: URL: https://github.com/apache/pekko/pull/3157
### Motivation `Source.unfoldResourceAsync` could call `close(resource)` from `postStop()` while a `readData(resource)` `Future` was still running. That allowed `close` and `readData` to operate on the same resource concurrently after downstream cancellation. ### Modification Track the current read `Future` with a null-sentinel field. On `postStop`, defer `close(resource)` until that pending read completes; clear the field on synchronous and asynchronous read completion to avoid retaining completed reads and keep the hot path GC/JIT friendly. Add a regression test that cancels after a read has started, waits for stream termination, asserts `close` has not run while the read is still pending, then completes the read and verifies `close` runs exactly after that point. ### Result Cancellation no longer closes the resource concurrently with an in-flight `readData` call. Existing `unfoldResourceAsync` completion, restart, resume, and close behavior remains covered by the full spec. ### Tests - `scalafmt --mode diff-ref=origin/main` - passed (JDK 25 Unsafe deprecation warning only) - `scalafmt --list --mode diff-ref=origin/main` - passed (JDK 25 Unsafe deprecation warning only) - `sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.UnfoldResourceAsyncSourceSpec -- -z \"wait for pending read before closing resource when stream is cancelled\""` - passed - Same focused test against old `postStop` behavior - failed as expected with `close` observing `readCompleted=false` - `sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.UnfoldResourceAsyncSourceSpec"` - passed, 21 tests - `sbt checkCodeStyle` - passed - `sbt +headerCheckAll` - passed - `git diff --check` - passed - `sbt validatePullRequest` - Not run - focused stream test, style, and header validation run locally ### References Fixes #3134 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
