From 32623b930ed4beef42096386cac17dae538a5c54 Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Mon, 19 Oct 2020 14:13:14 +0900 Subject: [PATCH] handle secure errors in readOnce (secure) (#397) * handle secure errors in readOnce(secure) * small synthax fix * fix mistake in readOnce's isNil --- libp2p/protocols/secure/secure.nim | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index c7c9b9035..287e6b934 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -120,10 +120,23 @@ method readOnce*(s: SecureConn, doAssert(nbytes > 0, "nbytes must be positive integer") if s.buf.data().len() == 0: - let buf = await s.readMessage() + let (buf, err) = try: + (await s.readMessage(), nil) + except CatchableError as exc: + (@[], exc) + + if not isNil(err): + warn "error while reading message from secure connection, closing.", error=err.name, + message=err.msg, + connection=s + await s.close() + raise err + s.activity = true + if buf.len == 0: raise newLPStreamIncompleteError() + s.buf.add(buf) var p = cast[ptr UncheckedArray[byte]](pbytes)