def set_cloexec(fd):
    """Set the file descriptor `fd` to automatically close on
    :func:`os.execve`. This has no effect on file descriptors inherited across
    :func:`os.fork`, they must be explicitly closed through some other means,
    such as :func:`mitogen.fork.on_fork`."""
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    assert fd > 2
    fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)