Files
swift-nio-ssl/Sources
Felix Schlegel 678b6412f5 NIOSSLHandler: behave sensibly on close(mode: .output) (#428)
* NIOSSLHandler.close(mode: .output) new behaviour

Modifications:

* flush outstanding messages on NIOSSLHandler.close(mode: .output)
* change state once NIOSSLHandler.close(mode: .output) is invoked, so
  that subsequent writes to the channel fail
* write integration tests to verify the behaviour

* * replace ConnectionState.closedOutput with ConnectionState.active(.output)

* Revert "* replace ConnectionState.closedOutput with ConnectionState.active(.output)"

This reverts commit 59cf0dfa147dcb794639b6a2dcd5331b7250c39f.

* close(mode: .output): perform TLS shutdown

* Insight comments from Cory about why test fail

Reason:

* atm our `NIOSSLServerHandler` does not support half-closure, thus it
  closes TCP connection entirely on reception of close
  -> our tests race

* Add temporary test demonstrating how half closure works for vanilla TCP connections

* testSubsequentWritesFailAfterCloseModeOutput: configure test server with allowRemoteHalfClosure

* First implementation of half-closure for NIOSSLServerHandlers

* NIOSSLHandler: throw ChannelError.outputClosed on NIOSSLHandler.bufferWrite

* Refactor

* Replace inputClosed: Bool with ConnectionState.inputClosed

* Add NIOSSLIntegrationTest.testMultipleCLoseOutput()

* Trigger TCP close when NIOSSLHandler.state == .inputClose

* testCloseModeOutputTriggersFlush: add allowHalfClosure to serverChannel

* * don't cache value of ChannelOptions.allowRemoteHalfClosure in NIOSSLHandler

* Refactor

* Refactor TCP input close handling

Modifications:

* send user event "input closed" onwards when in state .closed or .idle
* trigger closure of TCP input once we enter the .inputClosed state

* Refactor closingOutput procedure

Modifications:

* in `closeOuput` trigger the shutdown step regardless of the existence
  of a promise
* remove redundant state transition to `.outputClosed`

* * closeOutput: flush before we doShutdown

* Refactor closeOutput state management

Modifications:

* remove `.closingOutput` state

* * closeOutput: remove closeOutputPromise

* * remove TCP half close example

* * add comment about not closing the entire channel after a half-closure

* * rework .inputClosed and .outputClosed state

* * add test for closing server output and client output

* Review Cory

Modifications:

* pass on user inbound event "input closed" to the next `ChannelHandler`
  event in the event of an error
* cache value of `ChannelOptions.allowRemoteHalfClosure` across
  `doFlushReadData` to avoid races in case the option's value
  changes while `doFlushReadData` is being executed
* change behaviour: pass `closeOutputPromise` on in `context.close(mode:
  .output, promise: closeOutputPromise`

* Review Cory

Modifications:

* `NIOSSLHandler.userInboundInputClosedTriggered`: execute
  `fireErrorCaught` before `userInboundEventTriggered(.inputClosed)`
* `func completedAdditionalPeerCertificateVerification` fails with
  invalid state when in states `{.input,.ouput}closed`

* Support close(.output) when handshaking

Modifications:

* create `enum BufferedAction` that allows `NIOSSLHandler` to buffer
  actions instead of just writes
* rename `bufferedWrites` -> `bufferdActions`
* add `BufferedAction.closeOutput`, which allows us to buffer
  invocations of `close(mode: .output)` when we are still in non-active
  modes such as `.handshaking` or `.additionalVerification` -> this
  means we'll invoke the closing of the output after our handshaking
  step is completed and all our buffered writes have been flushed

* Review Cory

Modifications:

* Refactoring
* fixed wrong comment
* remove `MarkedCircularBuffer.forEachElementUntilMark()`
* discard all buffered actions that occured after the `.closeOutput`
  buffered actions in `NIOSSLHandler.doUnbufferActions()`

* Fix race caused by setting .inputClosed

Motivation:

Previously we did not check the current state when setting the
`NIOSSLHandler`'s state to `.inputClosed`. This resulted in an issue
where when we were in state `.closing`, then set the state to
`.inputClosed`, we would end up with a fatalError in our shutdown /
scheduledShutdown as state `.inputClosed` is not a shutting-down state.

Modifications:

* check current state before setting `state = .inputClosed`,
  specifically not setting the state to `.inputClosed` when we are
  already in the process of doing a full closure

* NIOSSLHandler: full close if input & output closed

Modifications:

* `NIOSSLHandler`: escalate to full closure when both input and output
  are closed
* `NIOSSLIntegrationTest.testCloseModeOutputServerAndClient`: assert
  that having input and output closed in `NIOSSLHandler` results in full
  closure
* `NIOSSLIntegrationTest.testCloseModeOutputServerAndClient`: assert
  that if one side closes its output, the other side of the connection
  closes its input

* Review Cory

Modifications:

* update comment on why we don't do a full channel close when we execute
  `doShutDownStep` for state `.outputClosed`
* do not suppress error in `NIOSSLHandler.doUnbufferActions` when in
  state `.outputClosed`
2023-06-26 14:36:35 +01:00
..