Don't use `defer` in eth2_network to avoid https://github.com/nim-lang/Nim/issues/13899

This commit is contained in:
Zahary Karadjov 2020-04-14 19:49:46 +03:00 committed by zah
parent f34fd46e3a
commit 22e5f8165e
1 changed files with 48 additions and 48 deletions

View File

@ -392,14 +392,14 @@ proc sendNotificationMsg(peer: Peer, protocolId: string, requestBytes: Bytes) {.
raise newException(TransmissionError, "Failed to open LibP2P stream")
let stream = streamFut.read
defer:
await safeClose(stream)
try:
var s = memoryOutput()
s.appendVarint requestBytes.len.uint64
s.append requestBytes
let bytes = s.getOutput
await stream.write(bytes)
finally:
await safeClose(stream)
# TODO There is too much duplication in the responder functions, but
# I hope to reduce this when I increse the reliance on output streams.
@ -442,9 +442,7 @@ proc makeEth2Request(peer: Peer, protocolId: string, requestBytes: Bytes,
return none(ResponseMsg)
let stream = streamFut.read
defer:
await safeClose(stream)
try:
# Send the request
var s = memoryOutput()
s.appendVarint requestBytes.len.uint64
@ -454,6 +452,8 @@ proc makeEth2Request(peer: Peer, protocolId: string, requestBytes: Bytes,
# Read the response
return await stream.readResponse(ResponseMsg, deadline)
finally:
await safeClose(stream)
proc init*[MsgType](T: type Responder[MsgType],
peer: Peer, conn: Connection): T =
@ -546,9 +546,7 @@ proc handleIncomingStream(network: Eth2Node, conn: Connection,
handleIncomingPeer(peer)
defer:
await safeClose(conn)
try:
let
deadline = sleepAsync RESP_TIMEOUT
msgBytes = await readMsgBytes(conn, false, deadline)
@ -577,6 +575,8 @@ proc handleIncomingStream(network: Eth2Node, conn: Connection,
await callUserHandler(peer, conn, msg)
except CatchableError as err:
await sendErrorResponse(peer, conn, ServerError, err.msg)
finally:
await safeClose(conn)
proc handleOutgoingPeer*(peer: Peer): Future[void] {.async.} =
let network = peer.network