---
title: "fd_close()"
description: "The fd_close() function is used to close an open file descriptor. For sockets, this function will flush the data before closing the socket."
url: "https://wasix.org/docs/api-reference/wasi/fd_close/"
markdown: "https://wasix.org/docs/api-reference/wasi/fd_close.md"
---

# `fd_close()`

Close an open file descriptor.

## Description

The `fd_close()` function is used to close an open file descriptor. For sockets, this function will flush the data before closing the socket.

## Syntax

```ebnf
  ;;; Note: This is similar to `close` in POSIX.
  ;;; Close a file descriptor.
  (@interface func (export "fd_close")
    (param $fd $fd)
    (result $error (expected (error $errno)))
  )
```

## Parameters

- `ctx`: A mutable reference to the function environment.
- `fd`: The file descriptor mapping to an open file to close.

## Return Value

The function returns a `Result` containing an `Errno` value indicating the success or failure of the operation. An `Errno::Success` value indicates a successful closure.

## Errors

The function can return the following errors:

- `Errno::Isdir`: If the provided `fd` corresponds to a directory.
- `Errno::Badf`: If the provided `fd` is invalid or not open.

## Logging

This function has been instrumented with debug-level logging. It will log the following information:

- `pid`: The process ID of the current execution context.
- `%fd`: The file descriptor being closed.
- `err`: Any encountered error during the closing process.

## Note

The `fd_close()` function is used to close an open file descriptor. It releases any resources associated with the file descriptor. For sockets, it is best practise to run [`sock_shutdown()`](https://wasix.org/docs/api-reference/wasi/sock_shutdown.md) before closing, because otherwise it can have unexpected runtime and latency side affects in some wasm runtimes.

---

[Documentation index](https://wasix.org/llms.txt) · [HTML version](https://wasix.org/docs/api-reference/wasi/fd_close/)
