You're writing a network client or server on Windows, and boom—your socket call fails with WSAENOTEMPTY (0X00002752). The message says "Cannot remove a directory that is not empty." But you're not even touching directories. You're sending or receiving data. This error tripped me up the first time I saw it too, because the message is misleading. It usually appears when you're trying to bind a socket to a Unix domain socket path, or when you're cleaning up after a failed socket operation and your code tries to remove() a socket file that still has connections or leftover files in its directory.
What's actually happening
On Windows, Unix domain sockets are implemented as files. When you create a Unix socket, Windows creates a placeholder file in the filesystem. If your program crashes or you forget to close the socket before deleting that file, the file stays. When you try to delete the directory containing that file—or when your socket code tries to unlink the socket file while it's still in use—Windows throws WSAENOTEMPTY. It's Windows's way of saying "I can't clean up because this directory still has stuff in it."
The most common trigger: you're using AF_UNIX sockets in a cross-platform app. On Linux, you can unlink() a socket file even if it's active. On Windows, you can't. So your cleanup code that worked on Linux suddenly breaks on Windows.
Root cause, in plain English
Windows doesn't let you delete a file that's still open or a directory that isn't empty. Unix domain sockets are treated as files, so if your socket is still bound or connected, the file is locked. When your code tries to remove() the socket file or its parent directory, Windows refuses, and the Winsock layer reports 0X00002752.
Another scenario: you're using a temporary directory for sockets, and a previous run left stale socket files behind. Your new run tries to reuse that directory, but it's not empty because of the leftovers.
How to fix it
Here's the fix that works, and it's not just about catching the error—it's about preventing it.
- Close the socket before cleanup. Always call
closesocket()(orclose()if you're using the CRT) on the socket before attempting to remove the file. This releases the lock. Don't skip this step, even if you think the socket is already dead. - Remove the socket file, not the directory. If you're trying to remove a directory that contains the socket file, first unlink the socket file specifically. On Windows, you need to use
DeleteFile()orremove()on the socket path, but only after the socket is closed. - Use a unique directory per run. If your app creates a temp directory for Unix sockets, include a process ID or a random string in the directory name. This avoids stale files from previous runs.
- If you still hit the error, list the directory contents. Check for leftover files. You can do this in a command prompt:
dir /s /b C:\path\to\socket\directory
If you see a file likesocket.tmp, delete it manually or in code before retrying. - Use
SetHandleInformation()to mark the socket handle as non-inheritable. This prevents child processes from inheriting the handle and keeping the file locked. It's a rare edge case, but I've seen it happen with forked worker processes.
Here's a code snippet in C that does it right:
// After socket is closed
closesocket(sock);
// Now safe to remove the socket file
const char* path = "C:/temp/mysocket.sock";
if (remove(path) != 0) {
// Check errno - if it's EACCES, the file is still locked
// If it's ENOENT, the file doesn't exist - that's fine
}
Still failing? Check these
- Antivirus or backup software might be holding the file open. Temporarily disable it to test.
- Windows Defender's controlled folder access can block file deletion. Add your app to the allowed list.
- If you're using a network share, the directory might be on a mapped drive with permissions issues. Move the socket to a local path.
- Check if the socket is bound to a different process. Use
netstat -ano | findstr :portto see which PID owns the socket, then close that process.
The real fix is to treat Unix domain socket files as first-class files on Windows. Close them before you clean up, and don't assume the directory is empty until you've verified it. Once you get this pattern down, you won't see 0X00002752 again.