Safely close connection's streams. (#199)

This commit is contained in:
Eugene Kabanov 2021-06-14 15:16:33 +03:00 committed by GitHub
parent e6fd38fd49
commit f7dd6b76c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -534,7 +534,15 @@ proc closeWait(conn: HttpClientConnectionRef) {.async.} =
if conn.state notin {HttpClientConnectionState.Closing,
HttpClientConnectionState.Closed}:
conn.state = HttpClientConnectionState.Closing
await allFutures(conn.reader.closeWait(), conn.writer.closeWait())
let pending =
block:
var res: seq[Future[void]]
if not(isNil(conn.reader)) and not(conn.reader.closed()):
res.add(conn.reader.closeWait())
if not(isNil(conn.writer)) and not(conn.writer.closed()):
res.add(conn.writer.closeWait())
res
if len(pending) > 0: await allFutures(pending)
case conn.kind
of HttpClientScheme.Secure:
await allFutures(conn.treader.closeWait(), conn.twriter.closeWait())