Repository: kubernetes/kubernetes (language: go)

Issue:
kubelet fail to start on Windows since v1.31.0
### What happened?

Since I upgraded my kubernetes cluster from `v1.30.4` to `v1.31.0`, kubelet fails to restart on Windows.

The error messages in the logs are:
```
E0828 03:15:28.934935    5404 server.go:102] "Failed to listen to socket while starting device plugin registry" err="listen unix C:\\var\\lib\\kubelet\\device-plugins\\kubelet.sock: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."
E0828 03:15:28.934935    5404 kubelet.go:1566] "Failed to start ContainerManager" err="listen unix C:\\var\\lib\\kubelet\\device-plugins\\kubelet.sock: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."
```

### What did you expect to happen?

I expected kubelet to start even if the `kubelet.sock` file exists, this was the behavior in the previous versions.

### How can we reproduce it (as minimally and precisely as possible)?

1. Setup a Windows Kubernetes Node
2. Start kubelet.exe
3. Stop kubelet.exe
4. Start kubelet.exe again and watch it fail due to the kubelet.sock file already existing.

### Anything else we need to know?

I think this Issue has to do with the commit 4060ee6 where socket files are not removed anymore causing the start to fail because the file already exists.

### Kubernetes version

<details>

```console
$ kubectl version
Client Version: v1.31.0
Kustomize Version: v5.4.2
Server Version: v1.31.0
```

</details>

### Cloud provider

On Premise / No Cloud Provider

### OS version

<details>

```console
C:\> wmic os get Caption,Version,BuildNumber,OSArchitecture
BuildNumber  Caption                                              OSArchitecture  Version
20348        Microsoft Windows Server 2022 Datacenter Evaluation  64-bit          10.0.20348
```

</details>

### Install tools

_No response_

### Container runtime (CRI) and version (if applicable)

<details>

Containerd Version: 1.7.20

</details>

### Related plugins (CNI, CSI, ...) and versions (if applicable)

_No response_


Discussion / hints:
/sig windows
/area kubelet
/cc
/sig node
/triage accepted
/priority critical-urgent
I was able to reproduce this
/assign @jsturtevant 

Given the severity, assigning to you.

Fix patch:
diff --git a/pkg/kubelet/cm/devicemanager/manager.go b/pkg/kubelet/cm/devicemanager/manager.go
index 6264cb12dd9ae..bca6b516be7d9 100644
--- a/pkg/kubelet/cm/devicemanager/manager.go
+++ b/pkg/kubelet/cm/devicemanager/manager.go
@@ -203,12 +203,15 @@ func (m *ManagerImpl) CleanupPluginDirectory(dir string) error {
 		if filePath == m.checkpointFile() {
 			continue
 		}
-		stat, err := os.Stat(filePath)
+		// TODO: Until the bug - https://github.com/golang/go/issues/33357 is fixed, os.stat wouldn't return the
+		// right mode(socket) on windows. Hence deleting the file, without checking whether
+		// its a socket, on windows.
+		stat, err := os.Lstat(filePath)
 		if err != nil {
 			klog.ErrorS(err, "Failed to stat file", "path", filePath)
 			continue
 		}
-		if stat.IsDir() || stat.Mode()&os.ModeSocket == 0 {
+		if stat.IsDir() {
 			continue
 		}
 		err = os.RemoveAll(filePath)


Test patch: