MichaelDRZandGeorge Barnett 88a51340f5 Make the HTTP decompressors safe against synchronous channelRead re-entry (#317)
### Motivation:

`NIOHTTPRequestDecompressor` / `NIOHTTPResponseDecompressor` keep their
state in a
stored `var decompressor` (a `struct`) and forwarded downstream
(`fireChannelRead` /
`fireErrorCaught`) from inside that struct's `mutating` `channelRead`,
i.e. while an
exclusive (modify) access to the stored property was still open. If
firing downstream
synchronously re-enters the handler's `channelRead`, a second exclusive
access is
opened and the program traps with `Fatal access conflict detected`
(SIGABRT).

This reproduces in production with AsyncHTTPClient over HTTP/2 and
`decompression = .enabled`: a compressed response is mid-decompress when
a
decompression error closes the stream, and the buffered read
re-delivered on the
stream-close path re-enters `channelRead` on the same decompressor.

### Modifications:

- The body decompression is now expressed as a
`NIOSingleStepByteToMessageDecoder`,
driven by a `NIOSingleStepByteToMessageProcessor`. The handler pulls one
decompressed
buffer at a time and forwards it only after `decode` has returned, so
the decoder
  relinquishes its exclusive access before anything is fired downstream.
- The shared driver is now a `class`, so the head/end forwarding paths
are re-entrancy
  safe for the same reason.
- The zlib stream is torn down in the decoder's `deinit`, which
additionally fixes a
  leak when a message ends without a final `.end`.
- No public API changes.

### Result:

Synchronous re-entry of `channelRead` no longer traps. Adds regression
tests covering
the forward and error re-entry paths on both decompressors; all existing
decompression
tests (limits, trailing data, truncation, multi-message reuse) continue
to pass.

Fixes #314

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-06 08:59:16 +00:00
2024-11-15 09:51:09 +00:00
2023-06-06 09:10:59 -07:00
2018-05-15 15:36:56 +01:00
2026-04-07 12:22:26 +00:00
2021-03-09 11:10:43 +00:00

NIOExtras

NIOExtras is a good place for code that is related to NIO but not core. It can also be used to incubate APIs for tasks that are possible with core-NIO but are cumbersome today.

What makes a good contribution to NIOExtras?

  • a protocol encoder/decoder pair (also called "codec") that is often used but is small enough so it doesn't need its own repository
  • a helper to achieve a task that is harder-than-necessary to achieve with core-NIO

Code Quality / Stability

All code will go through code review like in the other repositories related to the SwiftNIO project.

swift-nio-extras part of the SwiftNIO 2 family of repositories and depends on the following:

  • swift-nio, version 2.30.0 or better.
  • Swift 5.7.1
  • zlib and its development headers installed on the system. But don't worry, you'll find zlib on pretty much any UNIX system that can compile any sort of code.

To depend on swift-nio-extras, put the following in the dependencies of your Package.swift:

.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.0.0"),

Support for older Swift versions

The most recent versions of SwiftNIO Extras support Swift 5.7.1 and newer. The minimum Swift version supported by SwiftNIO Extras releases are detailed below:

SwiftNIO Extras Minimum Swift Version
1.0.0 ..< 1.10.0 5.0
1.10.0 ..< 1.11.0 5.2
1.11.0 ..< 1.14.0 5.4
1.14.0 ..< 1.19.0 5.5.2
1.19.0 ..< 1.20.0 5.6
1.20.0 ..< 1.23.0 5.7.1
1.23.0 ..< 1.27.0 5.8
1.27.0 ..< 1.30.0 5.10
1.30.0 ..< 1.34.0 6.0
1.34.0 ..< 6.1

On the nio-extras-0.1 branch, you can find the swift-nio-extras version for the SwiftNIO 1 family. It requires Swift 4.1 or better.

Current Contents

S
Description
Mirror of github.com/apple/swift-nio-extras
Readme Apache-2.0
2.5 MiB
0 Stars 1 Watchers 0 Forks
Languages
Swift 52.7%
C 47%
Shell 0.3%