def set_close_exec(fd):
    """
    exec functions (e.g. subprocess.Popen) by default force the child
    process to inherit all file handles which can result in stuck
    connections and unacknowledged messages. Setting FD_CLOEXEC forces
    the handles to be closed first.
    """
    try:
        import fcntl
        flags = fcntl.fcntl(fd, fcntl.F_GETFD)
        fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
    except ImportError:
        pass
