Skip to content

fix(server): do not crash when the interface list cannot be enumerated - #7270

Merged
mcollina merged 2 commits into
fastify:mainfrom
defaultuser233:fix/guard-network-interfaces-throw
Nov 28, 2026
Merged

mcollina merged 2 commits into
fastify:mainfrom
defaultuser233:fix/guard-network-interfaces-throw

Conversation

@defaultuser233

Copy link
Copy Markdown
Contributor

Problem

logServerAddress() calls getAddresses() when the server binds to the wildcard address, and getAddresses() calls os.networkInterfaces() without guarding it.

That call throws on platforms where the interface list cannot be read. On Android, for example, the glibc implementation of getifaddrs() fails with EACCES: it needs CAP_NET_ADMIN for its NETLINK_ROUTE (RTM_GETLINK) dump, and it also reads /proc/net/if_inet6, which Android does not expose to ordinary apps. bionic's getifaddrs() avoids both and works fine, which is why the failure is specific to glibc-linked builds.

The throw happens inside the server's listening event handler, so it escapes as an uncaught exception: the listen() promise never settles and the process dies.

Reproduction

import os from 'node:os'
import Fastify from 'fastify'

// Stand in for a platform where the interface list cannot be enumerated.
// (On Android this happens for real.)
os.networkInterfaces = () => {
  throw Object.assign(new Error('uv_interface_addresses returned Unknown system error 13'), {
    code: 'ERR_SYSTEM_ERROR'
  })
}

const app = Fastify()
app.get('/', async () => 'hello')

await app.listen({ host: '0.0.0.0', port: 0 })
console.log('listening')
Error: uv_interface_addresses returned Unknown system error 13
    at os.networkInterfaces (.../repro.mjs:7:23)
    at getAddresses (.../fastify/lib/server.js:365:29)
    at Object.logServerAddress (.../fastify/lib/server.js:382:19)
    at Server.listeningEventHandler (.../fastify/lib/server.js:290:32)

Fix

Guard the call and fall back to the address the server bound to:

function getAddresses (address) {
  if (address.address === '0.0.0.0') {
    let interfaces
    try {
      interfaces = os.networkInterfaces()
    } catch {
      return [address.address]
    }
    ...

The server then starts as usual and still logs the address it is bound to.

Test

test/listen.1.test.js gains a case that makes os.networkInterfaces() throw and asserts that listen() still completes:

✔ listen works when the interface list cannot be enumerated

Without the fix the case fails with code: 'ERR_SYSTEM_ERROR' and takes the rest of the test file down with it.

Fixes #7268

logServerAddress() calls getAddresses() when the server binds to the
wildcard address, and getAddresses() calls os.networkInterfaces() without
guarding it. That call throws on platforms where the interface list
cannot be read - on Android the glibc implementation of getifaddrs()
fails with EACCES because it needs CAP_NET_ADMIN for its NETLINK_ROUTE
dump and /proc/net is not readable.

The throw happens inside the server's 'listening' event handler, so it
escapes as an uncaught exception and takes the whole process down: the
listen() promise never settles and the server is gone.

Fall back to the address that was bound to, so the server still starts.

Fixes fastify#7268

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina
mcollina enabled auto-merge (squash) November 26, 2026 21:36
@Tony133 Tony133 added the backport 5.x Issue or pr that should be backported to Fastify v5 label Nov 28, 2026

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina
mcollina merged commit c38830f into fastify:main Nov 28, 2026
43 of 56 checks passed
Tony133 pushed a commit that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 5.x Issue or pr that should be backported to Fastify v5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os.networkInterfaces() can throw and crash the process when listening on 0.0.0.0

3 participants