-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
properly close connections #128
Changes from all commits
bee7261
46442bc
64a3afb
8fd182b
c1470e4
e398e5c
f9e0e55
9c7f7f9
795f93b
590d4a8
8a0e54b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ../protocol, | |
|
||
type | ||
Secure* = ref object of LPProtocol # base type for secure managers | ||
|
||
SecureConn* = ref object of Connection | ||
|
||
method readMessage*(c: SecureConn): Future[seq[byte]] {.async, base.} = | ||
|
@@ -32,8 +33,9 @@ method handshake(s: Secure, | |
initiator: bool = false): Future[SecureConn] {.async, base.} = | ||
doAssert(false, "Not implemented!") | ||
|
||
proc readLoop(sconn: SecureConn, stream: BufferStream) {.async.} = | ||
proc readLoop(sconn: SecureConn, conn: Connection) {.async.} = | ||
try: | ||
let stream = BufferStream(conn.stream) | ||
while not sconn.closed: | ||
let msg = await sconn.readMessage() | ||
if msg.len == 0: | ||
|
@@ -44,24 +46,23 @@ proc readLoop(sconn: SecureConn, stream: BufferStream) {.async.} = | |
except CatchableError as exc: | ||
trace "Exception occurred Secure.readLoop", exc = exc.msg | ||
finally: | ||
trace "closing conn", closed = conn.closed() | ||
if not conn.closed: | ||
await conn.close() | ||
|
||
trace "closing sconn", closed = sconn.closed() | ||
if not sconn.closed: | ||
await sconn.close() | ||
trace "ending Secure readLoop", isclosed = sconn.closed() | ||
trace "ending Secure readLoop" | ||
|
||
proc handleConn*(s: Secure, conn: Connection, initiator: bool = false): Future[Connection] {.async, gcsafe.} = | ||
var sconn = await s.handshake(conn, initiator) | ||
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = | ||
trace "sending encrypted bytes", bytes = data.shortLog | ||
await sconn.writeMessage(data) | ||
|
||
var stream = newBufferStream(writeHandler) | ||
asyncCheck readLoop(sconn, stream) | ||
result = newConnection(stream) | ||
result.closeEvent.wait() | ||
.addCallback do (udata: pointer): | ||
trace "wrapped connection closed, closing upstream" | ||
if not isNil(sconn) and not sconn.closed: | ||
asyncCheck sconn.close() | ||
result = newConnection(newBufferStream(writeHandler)) | ||
asyncCheck readLoop(sconn, result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda wanna remove this too and track the future but I guess can be done in another PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to happen in a more focused refactor, we'll do that after we get the current implementation more stable |
||
|
||
if not isNil(sconn.peerInfo) and sconn.peerInfo.publicKey.isSome: | ||
result.peerInfo = PeerInfo.init(sconn.peerInfo.publicKey.get()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might help to merge this #125 but anyway can be the opposite way too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably more urgent since it fixes some pretty egregious mem leaks. I'd rather get this in first to see how it behaves in NBC.