Si Beaumont 3df009d563 Fix crash in HTTPResumableUploadChannel when parent channel is closed (#304)
### Motivation:

Servers using `HTTPResumableUploadHandler` can currently crash during
channel creation if the parent channel is closed.

During channel creation, the handler will inherit the `autoRead` option
from the parent channel:

```
try! parent.syncOptions!.getOption(ChannelOptions.autoRead)
```

The force-unwrap of `syncOptions` is justified because this handler can
only be used with channels that support sync options, but the force-try
can trigger a crash with supported channels when the connection is
interrupted.

NIO's `BaseSocketChannel` will throw an error if the channel is closed,
and this presents a race window and we've observed crashes with the
following output:

```
HTTPResumableUploadChannel.swift:81: Fatal error: 'try!' expression unexpectedly raised an error: I/O on closed channel
```

### Modifications:

Replace the force-try with `try?` to prevent the crash. Given it looks
like the only reason this would throw is if the connection is closed,
the channel is not long for this world, and so the default value used is
moot. However, I've defaulted this to `false` as it's the most
conservative option.

I also wrote a test that uses new `EmbeddedChannel` API that was added
to support this fix. The PR has been structured to add the test first,
showing it failing, and then add the fix commit, which then shows the
test passing.

### Result:

The server will no longer crash when parent channels close during upload
operations.
2026-01-28 11:08:37 +00:00
2018-05-15 17:09:13 +01: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
2025-10-16 15:29:09 +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 ... 6.0

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%