From cade5f779373c3574782cfe514961f0b19fd4038 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 21 Apr 2022 15:12:58 -0700 Subject: [PATCH] Upgrade Tapir - Fix 2 small memory leaks around outbound connection handling --- go.mod | 2 +- go.sum | 2 ++ protocol/connections/engine.go | 23 +++++++++++++++++++-- protocol/connections/tokenboardclientapp.go | 1 + 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index fdcbdf0..292a191 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module cwtch.im/cwtch go 1.14 require ( - git.openprivacy.ca/cwtch.im/tapir v0.5.3 + git.openprivacy.ca/cwtch.im/tapir v0.5.4 git.openprivacy.ca/openprivacy/connectivity v1.8.3 git.openprivacy.ca/openprivacy/log v1.0.3 github.com/gtank/ristretto255 v0.1.2 diff --git a/go.sum b/go.sum index 6793e25..734add5 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ git.openprivacy.ca/cwtch.im/tapir v0.5.2 h1:7qJlUGY8RZbI7905BzVEEVB0hxP2GzI3lGiP git.openprivacy.ca/cwtch.im/tapir v0.5.2/go.mod h1:vVEu3CbXdyeMI5s1RkbROe1TBboNroGyJfODw8Ujlo8= git.openprivacy.ca/cwtch.im/tapir v0.5.3 h1:ei01JCQTXV15IOP00o5hygQCKb3+yCibWGih7yQ5t94= git.openprivacy.ca/cwtch.im/tapir v0.5.3/go.mod h1:VJitTBzerc+WO53c5XY30P2JD2Nx9mgxuII1FBVwW8E= +git.openprivacy.ca/cwtch.im/tapir v0.5.4 h1:CUcRVsM82Zx/pfcGqIviycavZcC50wXm67TiQ3mx6WY= +git.openprivacy.ca/cwtch.im/tapir v0.5.4/go.mod h1:VJitTBzerc+WO53c5XY30P2JD2Nx9mgxuII1FBVwW8E= git.openprivacy.ca/openprivacy/bine v0.0.4 h1:CO7EkGyz+jegZ4ap8g5NWRuDHA/56KKvGySR6OBPW+c= git.openprivacy.ca/openprivacy/bine v0.0.4/go.mod h1:13ZqhKyqakDsN/ZkQkIGNULsmLyqtXc46XBcnuXm/mU= git.openprivacy.ca/openprivacy/connectivity v1.8.3 h1:bWM8aQHqHIpobYQcLQ9OsNPoIl+H+4JFWbYGdG0nHlg= diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 9bf4f1f..76481a9 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "strconv" + "strings" "sync" "time" @@ -293,14 +294,18 @@ func (e *engine) Shutdown() { defer e.ephemeralServicesLock.Unlock() for _, connection := range e.ephemeralServices { log.Infof("shutting down ephemeral service") + connection.connectingLock.Lock() connection.service.Shutdown() + connection.connectingLock.Unlock() } + e.queue.Shutdown() } // peerWithOnion is the entry point for cwtchPeer relationships // needs to be run in a goroutine as will block on Open. func (e *engine) peerWithOnion(onion string) { + log.Debugf("Called PeerWithOnion for %v", onion) if !e.isBlocked(onion) { e.ignoreOnShutdown(e.peerConnecting)(onion) @@ -473,6 +478,15 @@ func (e *engine) peerAck(onion string, eventID string) { func (e *engine) peerDisconnected(onion string) { + // Clean up any existing get value requests... + e.getValRequests.Range(func(key, value interface{}) bool { + keyString := key.(string) + if strings.HasPrefix(keyString, onion) { + e.getValRequests.Delete(keyString) + } + return true + }) + // Purge circuit information... e.eventManager.Publish(event.NewEvent(event.ACNInfo, map[event.Field]string{ event.Handle: onion, @@ -494,8 +508,13 @@ func (e *engine) sendGetValToPeer(eventID, onion, scope, path string) error { return err } - e.getValRequests.Store(onion+eventID, message) - return e.sendPeerMessage(onion, pmodel.PeerMessage{ID: eventID, Context: event.ContextGetVal, Data: message}) + key := onion + eventID + e.getValRequests.Store(key, message) + err = e.sendPeerMessage(onion, pmodel.PeerMessage{ID: eventID, Context: event.ContextGetVal, Data: message}) + if err != nil { + e.getValRequests.Delete(key) + } + return err } func (e *engine) sendRetValToPeer(eventID, onion, val, existsStr string) error { diff --git a/protocol/connections/tokenboardclientapp.go b/protocol/connections/tokenboardclientapp.go index 9a69e60..b1849e7 100644 --- a/protocol/connections/tokenboardclientapp.go +++ b/protocol/connections/tokenboardclientapp.go @@ -178,6 +178,7 @@ func (ta *TokenBoardClient) MakePayment() error { id, sk := primitives.InitializeEphemeralIdentity() client := new(tor.BaseOnionService) client.Init(ta.acn, sk, &id) + defer client.Shutdown() tokenApplication := new(applications.TokenApplication) tokenApplication.TokenService = ta.tokenService