Skip to content

Commit

Permalink
Merge pull request #1182 from kestrelcjx/kestrel/process
Browse files Browse the repository at this point in the history
fix(process): fix the bug that the program is hung when getting the f…
  • Loading branch information
shirou committed Nov 20, 2021
2 parents a00b8ea + a0b6077 commit de385f5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
45 changes: 30 additions & 15 deletions process/process_windows.go
Expand Up @@ -12,6 +12,7 @@ import (
"reflect"
"strings"
"syscall"
"time"
"unicode/utf16"
"unsafe"

Expand Down Expand Up @@ -720,24 +721,38 @@ 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]))
ch <- struct{}{}
}()

fileName := string(utf16.Decode(buf[:n]))
fileInfo, _ := os.Stat(fileName)
if fileInfo.IsDir() {
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
}
case <-ctx.Done():
return files, ctx.Err()
}
}

Expand Down
45 changes: 30 additions & 15 deletions v3/process/process_windows.go
Expand Up @@ -12,6 +12,7 @@ import (
"reflect"
"strings"
"syscall"
"time"
"unicode/utf16"
"unsafe"

Expand Down Expand Up @@ -707,24 +708,38 @@ 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]))
ch <- struct{}{}
}()

fileName := string(utf16.Decode(buf[:n]))
fileInfo, _ := os.Stat(fileName)
if fileInfo.IsDir() {
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
}
case <-ctx.Done():
return files, ctx.Err()
}
}

Expand Down

0 comments on commit de385f5

Please sign in to comment.