mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: If a write is buffered in the `PendingWritesManager` and the high watermark is breached then a writability change will be fired as a result. The corresponding event when the channel becomes writable again comes when enough pending bytes have been written to the socket (i.e. as a result of a `flush()`) and we fall below the low watermark. However, if a promise associated with a write has a callback registered on its future which writes enough data to breach the high watermark (and also flushes) then we will re-entrantly enter `flushNow()`. This is okay, `flushNow()` protects against re-entrancy and any re-entrantly enqueud flushed writes will be picked up in the write-spin-loop. The issue here is that the `writeSpinLoop` does not monitor for changes in writability. Instead if checks the writability before it begins and after it completes, only signalling if there was a change. This is problematic: the re-entrant write-and-flush could have caused the channel to become unwritable yet no event will be fired to make it writable again! Modifications: - Store a local writability state in the pending writes manager and modify it when we emit writability changes Result: - Better protection against re-entrant writability changes.