Merge pull request #1182 from kestrelcjx/kestrel/process

fix(process): fix the bug that the program is hung when getting the f…
pull/1186/head
shirou 4 years ago committed by GitHub
commit de385f50a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"syscall" "syscall"
"time"
"unicode/utf16" "unicode/utf16"
"unsafe" "unsafe"
@ -720,13 +721,24 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
continue continue
} }
var fileName string
ch := make(chan struct{})
go func() {
var buf [syscall.MAX_LONG_PATH]uint16 var buf [syscall.MAX_LONG_PATH]uint16
n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0)
if err != nil { if err != nil {
continue return
} }
fileName := string(utf16.Decode(buf[:n])) fileName = string(utf16.Decode(buf[:n]))
ch <- struct{}{}
}()
select {
case <-time.NewTimer(100 * time.Millisecond).C:
continue
case <-ch:
fileInfo, _ := os.Stat(fileName) fileInfo, _ := os.Stat(fileName)
if fileInfo.IsDir() { if fileInfo.IsDir() {
continue continue
@ -739,6 +751,9 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
}) })
fileExists[fileName] = true fileExists[fileName] = true
} }
case <-ctx.Done():
return files, ctx.Err()
}
} }
return files, nil return files, nil

@ -12,6 +12,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"syscall" "syscall"
"time"
"unicode/utf16" "unicode/utf16"
"unsafe" "unsafe"
@ -707,13 +708,24 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
continue continue
} }
var fileName string
ch := make(chan struct{})
go func() {
var buf [syscall.MAX_LONG_PATH]uint16 var buf [syscall.MAX_LONG_PATH]uint16
n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0) n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0)
if err != nil { if err != nil {
continue return
} }
fileName := string(utf16.Decode(buf[:n])) fileName = string(utf16.Decode(buf[:n]))
ch <- struct{}{}
}()
select {
case <-time.NewTimer(100 * time.Millisecond).C:
continue
case <-ch:
fileInfo, _ := os.Stat(fileName) fileInfo, _ := os.Stat(fileName)
if fileInfo.IsDir() { if fileInfo.IsDir() {
continue continue
@ -726,6 +738,9 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
}) })
fileExists[fileName] = true fileExists[fileName] = true
} }
case <-ctx.Done():
return files, ctx.Err()
}
} }
return files, nil return files, nil

Loading…
Cancel
Save