Skip to content

Ensure deflater/inflater end() is called in DeflateCompressor and GzipCompressor#1142

Open
pjfanning wants to merge 9 commits into
apache:mainfrom
pjfanning:copilot/ensure-end-called-on-inflaters-deflaters
Open

Ensure deflater/inflater end() is called in DeflateCompressor and GzipCompressor#1142
pjfanning wants to merge 9 commits into
apache:mainfrom
pjfanning:copilot/ensure-end-called-on-inflaters-deflaters

Conversation

@pjfanning

Copy link
Copy Markdown
Member

more changes related to #1133

  • DeflateCompressor: add deflaterEnded flag + idempotent endDeflater() method; call endDeflater() in finishWithBuffer() instead of deflater.end() directly
  • GzipDecompressor: add postStop() calling inflater.end() + add createInflater() factory method for testability
  • StreamUtils.byteStringTransformer: add optional cleanup callback, call it in postStop() when the stage is stopped before onUpstreamFinish()
  • Encoder.singleUseEncoderFlow: pass cleanup callback that calls endDeflater() on DeflateCompressor instances to handle stream cancellation/failure
  • DeflateSpec: add tests for deflater cleanup on normal finish and cancellation
  • GzipSpec: add tests for inflater cleanup on normal finish and cancellation, and deflater cleanup on normal finish and cancellation

@pjfanning pjfanning changed the title Ensure deflater/inflater end() is called in DeflateCompressor and GzipCompressor WIP: Ensure deflater/inflater end() is called in DeflateCompressor and GzipCompressor Jul 7, 2026
@pjfanning pjfanning marked this pull request as draft July 7, 2026 22:02
Copilot AI and others added 3 commits July 8, 2026 09:35
…pCompressor

- DeflateCompressor: add deflaterEnded flag + idempotent endDeflater() method;
  call endDeflater() in finishWithBuffer() instead of deflater.end() directly
- GzipDecompressor: add postStop() calling inflater.end() + add createInflater()
  factory method for testability
- StreamUtils.byteStringTransformer: add optional cleanup callback, call it in
  postStop() when the stage is stopped before onUpstreamFinish()
- Encoder.singleUseEncoderFlow: pass cleanup callback that calls endDeflater()
  on DeflateCompressor instances to handle stream cancellation/failure
- DeflateSpec: add tests for deflater cleanup on normal finish and cancellation
- GzipSpec: add tests for inflater cleanup on normal finish and cancellation,
  and deflater cleanup on normal finish and cancellation
@pjfanning pjfanning force-pushed the copilot/ensure-end-called-on-inflaters-deflaters branch from 7653520 to 8add717 Compare July 8, 2026 08:36

@He-Pin He-Pin 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.

Overall this looks like a solid cleanup. The idempotent endDeflater() pattern is clean, and the tests actually exercise the cancellation path properly (nice touch with awaitEnd to avoid the postStop race).

A couple of minor thoughts inline. Also — the PR title still has "WIP" in it, might want to update that before merging.

Comment thread http-core/src/main/scala/org/apache/pekko/http/impl/util/StreamUtils.scala Outdated
Comment thread http/src/main/scala/org/apache/pekko/http/scaladsl/coding/DeflateCompressor.scala Outdated
@pjfanning pjfanning changed the title WIP: Ensure deflater/inflater end() is called in DeflateCompressor and GzipCompressor Ensure deflater/inflater end() is called in DeflateCompressor and GzipCompressor Jul 8, 2026
@pjfanning pjfanning marked this pull request as ready for review July 8, 2026 09:24
if (data.nonEmpty) emit(out, data)
completeStage()
} finally {
finished = true

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.

Setting the finished flag in the finally block prevents postStop() from calling cleanup() if finish() throws. In that failure path the stage still stops, but the finished guard is already true, so the deflater leak this PR is trying to fix can still happen. Please move the flag assignment onto the success path, right before completeStage(). That keeps the happy path idempotent while still letting postStop() clean up on exceptions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I moved it before the if (data.nonEmpty) emit(out, data) - is that ok?

}
val data = finish()
finished = true
if (data.nonEmpty) emit(out, data)

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.

then we should add the finished = true to the emit(out, data, () => finished)` wdyt

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.

3 participants