From c463582528150150ae5571901e1005f5475d89ff Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sat, 10 Oct 2020 08:33:06 -0700 Subject: [PATCH] fix a race and optimize hello messages LifecycleCtx can _only_ be called during startup as it appends an fx hook. Worse, this was causing us to append an fx hook on every single hello message, leaking memory (and probably causing other shutdown issues...). --- node/modules/services.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/node/modules/services.go b/node/modules/services.go index 4ee0abacc..e0a7c2eda 100644 --- a/node/modules/services.go +++ b/node/modules/services.go @@ -42,11 +42,13 @@ func RunHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello. return xerrors.Errorf("failed to subscribe to event bus: %w", err) } + ctx := helpers.LifecycleCtx(mctx, lc) + go func() { for evt := range sub.Out() { pic := evt.(event.EvtPeerIdentificationCompleted) go func() { - if err := svc.SayHello(helpers.LifecycleCtx(mctx, lc), pic.Peer); err != nil { + if err := svc.SayHello(ctx, pic.Peer); err != nil { protos, _ := h.Peerstore().GetProtocols(pic.Peer) agent, _ := h.Peerstore().Get(pic.Peer, "AgentVersion") if protosContains(protos, hello.ProtocolID) {