denoland/deno · error · FsError

sockets cannot be copied

Error message

sockets cannot be copied

What it means

In Deno's copy implementation, entries are classified by file type before copying; on Unix, an entry whose type is a socket (S_ISSOCK) is refused with io::ErrorKind::InvalidInput 'sockets cannot be copied' (ext/fs/std_fs.rs:820). An AF_UNIX socket is a live connection endpoint, not file content, so duplicating it has no meaning.

Source

Thrown at ext/fs/std_fs.rs:820

        .collect::<Result<Vec<_>, _>>()?;

      return Ok(());
    } else if ty.is_symlink() {
      let from = std::fs::read_link(from)?;

      #[cfg(unix)]
      std::os::unix::fs::symlink(from, to)?;
      #[cfg(windows)]
      std::os::windows::fs::symlink_file(from, to)?;

      return Ok(());
    }
    #[cfg(unix)]
    {
      use std::os::unix::fs::FileTypeExt;
      if ty.is_socket() {
        return Err(
          io::Error::new(
            io::ErrorKind::InvalidInput,
            "sockets cannot be copied",
          )
          .into(),
        );
      }
    }

    // Ensure parent destination directory exists
    if let Some(parent) = to.parent() {
      fs::create_dir_all(parent)?;
    }

    copy_file(from, to)
  }

  #[cfg(target_os = "macos")]
  {

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Copy entries individually and skip those failing with 'sockets cannot be copied'
  2. Exclude well-known socket names (*.sock) before copying
  3. Use tar with --exclude for runtime directories instead of Deno.copy

Example fix

// before
await Deno.copy('/var/run/myapp', '/backup/myapp');

// after
await Deno.mkdir('/backup/myapp', { recursive: true });
for (const entry of Deno.readDirSync('/var/run/myapp')) {
  if (entry.name.endsWith('.sock')) continue;
  await Deno.copy(`/var/run/myapp/${entry.name}`, `/backup/myapp/${entry.name}`);
}
Defensive patterns

Strategy: try-catch

Validate before calling

const looksLikeSocket = (name: string): boolean =>
  name.endsWith('.sock') || name.endsWith('.socket');

for (const entry of Deno.readDirSync(srcDir)) {
  if (looksLikeSocket(entry.name)) continue; // known runtime sockets
  await Deno.copy(`${srcDir}/${entry.name}`, `${dstDir}/${entry.name}`);
}

Try / catch

for (const entry of Deno.readDirSync(srcDir)) {
  try {
    await Deno.copy(`${srcDir}/${entry.name}`, `${dstDir}/${entry.name}`);
  } catch (e) {
    if (e instanceof Error && /sockets cannot be copied/.test(e.message)) continue;
    throw e;
  }
}

Prevention

When it happens

Trigger: Deno.copy() (recursive) over a directory containing socket files: /var/run or /run with docker.sock, postgres/.s.PGSQL.sock, redis.sock, systemd sockets; a project tree containing a dev-server socket.

Common situations: Backing up /run or /var/run; container image build scripts copying runtime directories; local dev trees with database or IPC sockets in them.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/b4ff19d022013bf3. Report an issue: GitHub.