Skip to content

destroyAgent option defaults to true instead of false when undefined #444

@Yukio0315

Description

@Yukio0315

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.6.2

Plugin version

12.4.0

Node.js version

24.11

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

15.1

Description

The destroyAgent option defaults to true when it is not explicitly provided in the options, contradicting the documentation.

According to the README, this option is listed as By Default: false.

However, the logic in lib/request.js (around line 107) currently treats undefined as a signal to destroy the agent. Consequently, the agent is destroyed unless destroyAgent: false is manually specified.

Reproduction Code

Running the following script reproduces the issue (exits with error):

const Fastify = require('fastify')
const From = require('@fastify/reply-from')
const undici = require('undici')

async function run() {
  const mockAgent = new undici.Agent()
  
  // If destroy is called, we know the bug is present
  mockAgent.destroy = () => {
    console.error('❌ FAILED: Agent was destroyed! (Bug reproduced)')
    process.exit(1)
  }

  const instance = Fastify()

  // Register plugin without specifying destroyAgent (undefined)
  await instance.register(From, {
    base: 'http://localhost:4242',
    undici: mockAgent
  })

  await instance.ready()
  
  // Trigger the onClose hook
  await instance.close()

  console.log('✅ PASSED: Agent was preserved.')
}

run()

PR

I have already prepared a fix for this issue and will open a Pull Request shortly.

Link to code that reproduces the bug

See the "Reproduction Code" section in the Description above.

Expected Behavior

The destroyAgent option should default to false when omitted. The agent should be preserved for reuse unless destroyAgent: true is explicitly set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions