Skip to content

Diagnostics channels get garbage collected even when used #42170

Description

@rochdev

Version

17.6.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

node --expose-gc test.js
const { channel } = require('diagnostics_channel')
const strongRef = channel('strong')

strongRef.subscribe(message => {
  console.log(message) // outputs because the subscriber is still available
})

channel('weak').subscribe(message => {
  console.log(message) // no output because the subscriber was garbage collected
})

setTimeout(() => {
  channel('weak').publish('weak output')
  strongRef.publish('strong output')
})

gc()

How often does it reproduce? Is there a required condition?

Whenever a channel is used multiple times without a strong reference to the returned object and with any garbage collection happening between uses.

What is the expected behavior?

It should be possible to reference channels by name and expect them to be retained without explicitly holding a reference to the returned value of the function.

What do you see instead?

Channels are seemingly randomly removed whenever garbage collection happens if there is no strong reference to the returned channel.

Additional information

Every single member in my team got bit by this at one point or another. This is also true for external users trying to contribute to our repository or to other libraries, for example DataDog/dd-trace-js#1642 (comment).

My understanding is this was done to allow dynamic channel names that would later be garbage collected. Unfortunately, this resulted in this bug. Since dynamic channel names is an edge case, I think the priority should be on fixing the bug, and if anyone actually needs dynamic channel names then there could be a method added to cleanup channels by name, or to explicitly make them weak references.

cc @Qard since he did the original implementation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnostics_channelIssues and PRs related to the diagnostics_channel module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions