fix(metrics): avoid consuming protocol frames before app readers#3394
fix(metrics): avoid consuming protocol frames before app readers#3394dozyio merged 12 commits intolibp2p:mainfrom
Conversation
|
Thanks for validating this in production 🙌 Great signal that the fix addresses the unknown-peer spike in real deployments. I’ll keep this branch rebased and ready while we wait for maintainer review. |
|
@lodekeeper please fix up the branch so CI can pass |
|
Thanks for the ping — fixed and pushed now: bfb080b (lint fixes in streams.spec.ts for the new early-data test). CI has re-triggered on this commit; I’ll keep an eye on it and follow up if anything else is needed. |
tabcat
left a comment
There was a problem hiding this comment.
@lodekeeper hi thanks for opening this. would be great if you can make similar changes to packages/metrics-opentelemetry and packages/metrics-simple. but only if you have the time!
…ekeeper/js-libp2p into fix/prometheus-metrics-identify-race
|
@lodekeeper Could you update the tests for |
|
@dozyio there arent similar test files for those modules so fine merging without those. |
You will need to add |
|
Good catch — done. I added @libp2p/utils to devDependencies for both packages:
Pushed in commit cbaadfe. |
|
@lodekeeper just remove that last commit |
|
Thanks @lodekeeper for the PR - merged |

Summary
Fixes a protocol-stream observability race in
@libp2p/prometheus-metricswhere tracking could consume inbound frames before app/protocol readers attach.Fixes #3393
Root cause
_track()currently callsstream.addEventListener('message', ...).For
AbstractMessageStream, anymessagelistener can trigger dispatch of buffered inbound bytes. If metrics attaches first, protocol handlers that attach later may miss the first frame.Changes
1) Avoid adding
messagelistener in_trackInstead of adding an event listener, wrap
stream.dispatchEvent:evt.type === 'message', increment received bytes metricdispatchEventThis preserves received-byte accounting but does not increase
listenerCount('message'), so metrics no longer affects stream buffering/dispatch semantics.2) Regression test
Added test in
packages/metrics-prometheus/test/streams.spec.ts:Validation done downstream
In Lodestar local beacon runs, this change eliminated observed short-window
Unknownpeer spikes linked to identify misses (while keeping metrics enabled).