Skip to content

Conversation

@mcollina
Copy link
Member

Summary

Fixes #4615 - Prevents duplicate debug logs when both Node.js built-in undici and undici as a dependency are present.

Problem

When using NODE_DEBUG=undici, debug logs were being duplicated 2-3 times because both Node.js's built-in undici and undici installed as a dependency would subscribe to the same diagnostics channels. Each module instance would independently subscribe, causing the same event to be logged multiple times.

Solution

Added hasSubscribers checks in the diagnostics channel tracking functions before subscribing:

  • trackClientEvents() - checks client-related channels
  • trackRequestEvents() - checks request-related channels
  • trackWebSocketEvents() - checks websocket-related channels

This ensures that even when multiple module instances exist, only the first instance subscribes to the diagnostics channels, preventing duplicate logs.

Test Plan

  • Added regression test debug#undici no duplicates that reproduces the issue with multiple module loads
  • All existing debug tests pass (4/4)
  • All diagnostics channel tests pass (6/6)
  • All websocket diagnostics tests pass (3/3)
  • Linting passes

🤖 Generated with Claude Code

When both Node.js built-in undici and undici as a dependency are present,
both module instances would subscribe to the same diagnostics channels,
causing debug logs to be duplicated 2-3 times.

This fix adds checks for existing channel subscribers before subscribing
to prevent duplicate subscriptions across module instances.

Fixes #4615

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina force-pushed the fix/debug-logs-duplication branch from ba793f4 to f4ff3a5 Compare October 18, 2025 16:28
@mcollina mcollina marked this pull request as ready for review October 18, 2025 16:29
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 0% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.85%. Comparing base (9adb5e6) to head (f4ff3a5).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/core/diagnostics.js 0.00% 25 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4630      +/-   ##
==========================================
- Coverage   92.94%   92.85%   -0.09%     
==========================================
  Files         106      106              
  Lines       33092    33117      +25     
==========================================
- Hits        30756    30752       -4     
- Misses       2336     2365      +29     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont like this solution, as it is not possible to determine if the listener is actually a debug listener or some other listener.

I already thought about the problem multiple times I think the actual solution has to be something like using a Symbol.for('undici, -diagnostic-channel-debug') or what i prefer even more finegrained Symbol.for('diagnostic-channel-debug-websocket') and using it as a key on globalThis and persist if we already activated specified channels.

@mcollina mcollina merged commit b7f5fca into main Oct 19, 2025
32 of 35 checks passed
@Uzlopak Uzlopak deleted the fix/debug-logs-duplication branch October 19, 2025 09:52
@github-actions github-actions bot mentioned this pull request Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: debug logs duplicated

5 participants