status-im / nim-chronos

Chronos - An efficient library for asynchronous programming
https://status-im.github.io/nim-chronos/docs/chronos
Apache License 2.0
353 stars 51 forks source link

`write` hangs on a `Finished` `TlsStream` #329

Closed Menduist closed 1 year ago

Menduist commented 1 year ago

when I use write on a TlsStream with state == Finished, the write hangs forever instead of popping an error: https://github.com/status-im/nim-chronos/blob/6525f4ce1d1a7eba146e5f1a53f6f105077ae686/chronos/streams/asyncstream.nim#L783-L789

template checkStreamClosed*(t: untyped) =
  if t.state == AsyncStreamState.Closed: # "Finished" not handled
    raiseAsyncStreamUseClosedError()

Since wstream.writerLoop is not nil, it's added to the wstream.queue, except that the tlsLoop was exited once we reach the Finished state so it will never be completed

https://github.com/status-im/nim-chronos/blob/6525f4ce1d1a7eba146e5f1a53f6f105077ae686/chronos/streams/tlsstream.nim#L299-L301

EDIT: seems to be the same issue for state Error