mirror of
https://github.com/apple/swift-nio-extras
synced 2026-11-24 04:37:58 +00:00
### 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.