Skip to content

Commit de385f5

Browse files
authored
Merge pull request #1182 from kestrelcjx/kestrel/process
fix(process): fix the bug that the program is hung when getting the f…
2 parents a00b8ea + a0b6077 commit de385f5

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

process/process_windows.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"reflect"
1313
"strings"
1414
"syscall"
15+
"time"
1516
"unicode/utf16"
1617
"unsafe"
1718

@@ -720,24 +721,38 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
720721
continue
721722
}
722723

723-
var buf [syscall.MAX_LONG_PATH]uint16
724-
n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0)
725-
if err != nil {
726-
continue
727-
}
724+
var fileName string
725+
ch := make(chan struct{})
726+
727+
go func() {
728+
var buf [syscall.MAX_LONG_PATH]uint16
729+
n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0)
730+
if err != nil {
731+
return
732+
}
733+
734+
fileName = string(utf16.Decode(buf[:n]))
735+
ch <- struct{}{}
736+
}()
728737

729-
fileName := string(utf16.Decode(buf[:n]))
730-
fileInfo, _ := os.Stat(fileName)
731-
if fileInfo.IsDir() {
738+
select {
739+
case <-time.NewTimer(100 * time.Millisecond).C:
732740
continue
733-
}
741+
case <-ch:
742+
fileInfo, _ := os.Stat(fileName)
743+
if fileInfo.IsDir() {
744+
continue
745+
}
734746

735-
if _, exists := fileExists[fileName]; !exists {
736-
files = append(files, OpenFilesStat{
737-
Path: fileName,
738-
Fd: uint64(file),
739-
})
740-
fileExists[fileName] = true
747+
if _, exists := fileExists[fileName]; !exists {
748+
files = append(files, OpenFilesStat{
749+
Path: fileName,
750+
Fd: uint64(file),
751+
})
752+
fileExists[fileName] = true
753+
}
754+
case <-ctx.Done():
755+
return files, ctx.Err()
741756
}
742757
}
743758

v3/process/process_windows.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"reflect"
1313
"strings"
1414
"syscall"
15+
"time"
1516
"unicode/utf16"
1617
"unsafe"
1718

@@ -707,24 +708,38 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
707708
continue
708709
}
709710

710-
var buf [syscall.MAX_LONG_PATH]uint16
711-
n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0)
712-
if err != nil {
713-
continue
714-
}
711+
var fileName string
712+
ch := make(chan struct{})
713+
714+
go func() {
715+
var buf [syscall.MAX_LONG_PATH]uint16
716+
n, err := windows.GetFinalPathNameByHandle(windows.Handle(file), &buf[0], syscall.MAX_LONG_PATH, 0)
717+
if err != nil {
718+
return
719+
}
720+
721+
fileName = string(utf16.Decode(buf[:n]))
722+
ch <- struct{}{}
723+
}()
715724

716-
fileName := string(utf16.Decode(buf[:n]))
717-
fileInfo, _ := os.Stat(fileName)
718-
if fileInfo.IsDir() {
725+
select {
726+
case <-time.NewTimer(100 * time.Millisecond).C:
719727
continue
720-
}
728+
case <-ch:
729+
fileInfo, _ := os.Stat(fileName)
730+
if fileInfo.IsDir() {
731+
continue
732+
}
721733

722-
if _, exists := fileExists[fileName]; !exists {
723-
files = append(files, OpenFilesStat{
724-
Path: fileName,
725-
Fd: uint64(file),
726-
})
727-
fileExists[fileName] = true
734+
if _, exists := fileExists[fileName]; !exists {
735+
files = append(files, OpenFilesStat{
736+
Path: fileName,
737+
Fd: uint64(file),
738+
})
739+
fileExists[fileName] = true
740+
}
741+
case <-ctx.Done():
742+
return files, ctx.Err()
728743
}
729744
}
730745

0 commit comments

Comments
 (0)