From db054ec106f2660a4cb0fdb66012af0327dbe097 Mon Sep 17 00:00:00 2001 From: kestrelcjx Date: Sat, 20 Nov 2021 17:34:10 +0800 Subject: [PATCH 1/3] fix(process): fix the bug that the program is hung when getting the file name --- process/process_windows.go | 43 +++++++++++++++++++++++------------ v3/process/process_windows.go | 43 +++++++++++++++++++++++------------ 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/process/process_windows.go b/process/process_windows.go index 9a1f54869..cd86646f8 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -12,6 +12,7 @@ import ( "reflect" "strings" "syscall" + "time" "unicode/utf16" "unsafe" @@ -720,24 +721,36 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er continue } - var buf [syscall.MAX_LONG_PATH]uint16 - n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) - if err != nil { - continue - } + var fileName string + ch := make(chan struct{}) + + go func() { + var buf [syscall.MAX_LONG_PATH]uint16 + n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) + if err != nil { + return + } - fileName := string(utf16.Decode(buf[:n])) - fileInfo, _ := os.Stat(fileName) - if fileInfo.IsDir() { + fileName = string(utf16.Decode(buf[:n])) + ch <- struct{}{} + }() + + select { + case <-time.NewTimer(100 * time.Millisecond).C: continue - } + case <-ch: + fileInfo, _ := os.Stat(fileName) + if fileInfo.IsDir() { + continue + } - if _, exists := fileExists[fileName]; !exists { - files = append(files, OpenFilesStat{ - Path: fileName, - Fd: uint64(file), - }) - fileExists[fileName] = true + if _, exists := fileExists[fileName]; !exists { + files = append(files, OpenFilesStat{ + Path: fileName, + Fd: uint64(file), + }) + fileExists[fileName] = true + } } } diff --git a/v3/process/process_windows.go b/v3/process/process_windows.go index 2aaedcd1e..373bb3f28 100644 --- a/v3/process/process_windows.go +++ b/v3/process/process_windows.go @@ -12,6 +12,7 @@ import ( "reflect" "strings" "syscall" + "time" "unicode/utf16" "unsafe" @@ -707,24 +708,36 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er continue } - var buf [syscall.MAX_LONG_PATH]uint16 - n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) - if err != nil { - continue - } + var fileName string + ch := make(chan struct{}) + + go func() { + var buf [syscall.MAX_LONG_PATH]uint16 + n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) + if err != nil { + return + } - fileName := string(utf16.Decode(buf[:n])) - fileInfo, _ := os.Stat(fileName) - if fileInfo.IsDir() { + fileName = string(utf16.Decode(buf[:n])) + ch <- struct{}{} + }() + + select { + case <-time.NewTimer(100 * time.Millisecond).C: continue - } + case <-ch: + fileInfo, _ := os.Stat(fileName) + if fileInfo.IsDir() { + continue + } - if _, exists := fileExists[fileName]; !exists { - files = append(files, OpenFilesStat{ - Path: fileName, - Fd: uint64(file), - }) - fileExists[fileName] = true + if _, exists := fileExists[fileName]; !exists { + files = append(files, OpenFilesStat{ + Path: fileName, + Fd: uint64(file), + }) + fileExists[fileName] = true + } } } From 3b61bb28330224a73bbf396e6247d5aa96ded765 Mon Sep 17 00:00:00 2001 From: kestrelcjx Date: Sat, 20 Nov 2021 21:12:31 +0800 Subject: [PATCH 2/3] chore(process): add ctx.Done() to break select --- process/process_windows.go | 2 ++ v3/process/process_windows.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/process/process_windows.go b/process/process_windows.go index cd86646f8..aa9dbc41f 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -751,6 +751,8 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er }) fileExists[fileName] = true } + case <-ctx.Done(): + return files, nil } } diff --git a/v3/process/process_windows.go b/v3/process/process_windows.go index 373bb3f28..5194c082f 100644 --- a/v3/process/process_windows.go +++ b/v3/process/process_windows.go @@ -738,6 +738,8 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er }) fileExists[fileName] = true } + case <-ctx.Done(): + return files, nil } } From a0b60775028d837b0a7d7b0a7a4ae294a9d18361 Mon Sep 17 00:00:00 2001 From: kestrelcjx Date: Sat, 20 Nov 2021 21:25:08 +0800 Subject: [PATCH 3/3] chore(process): change the return value --- process/process_windows.go | 2 +- v3/process/process_windows.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/process/process_windows.go b/process/process_windows.go index aa9dbc41f..e410a0eb1 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -752,7 +752,7 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er fileExists[fileName] = true } case <-ctx.Done(): - return files, nil + return files, ctx.Err() } } diff --git a/v3/process/process_windows.go b/v3/process/process_windows.go index 5194c082f..4f004ff9f 100644 --- a/v3/process/process_windows.go +++ b/v3/process/process_windows.go @@ -739,7 +739,7 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er fileExists[fileName] = true } case <-ctx.Done(): - return files, nil + return files, ctx.Err() } }