bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
619 stars 189 forks source link

Read crashes on closed handle, Windows #57

Closed stas-makutin closed 4 years ago

stas-makutin commented 5 years ago

Read() methods crashes on Windows in the (https://github.com/bugst/go-serial/issues/13) scenario:

Port opened
goroutine: reading
Port closing
Exception 0xc0000008 0x0 0x0 0x7ffb7b06e78a
PC=0x7ffb7b06e78a

syscall.Syscall(0x7ffb7a5c0a20, 0x1, 0x130, 0x0, 0x0, 0x0, 0x0, 0x0)
    .../golang/src/runtime/syscall_windows.go:172 +0xc6
Port closed
syscall.CloseHandle(0x130, 0x0, 0x0)
    .../golang/src/syscall/zsyscall_windows.go:353 +0x71
go.bug.st/serial%2ev1.(*windowsPort).Close(0xc00004e200, 0x0, 0x0)
    .../go/src/go.bug.st/serial.v1/serial_windows.go:58 +0x3f
go.bug.st/serial%2ev1.(*windowsPort).Read(0xc00004e200, 0xc0000b6000, 0x3e8, 0x3e8, 0x0, 0x0, 0x0)
    .../go/src/go.bug.st/serial.v1/serial_windows.go:97 +0x3f2
main.main.func1(0x54b180, 0xc00004e200)
    .../src/test.go:90 +0x147
created by main.main
    .../src/test.go:87 +0xc96

goroutine 1 [syscall, locked to thread]:
syscall.Syscall(0x7ffb7a5c0a20, 0x4490ab, 0xc000075a08, 0x4490ab, 0xc000000008, 0x53df70, 0x608520, 0x0)
    .../golang/src/runtime/syscall_windows.go:172 +0xc6
runtime: unexpected return pc for syscall.CloseHandle called from 0x608520
stack: frame={sp:0xc0000759a8, fp:0xc000075a20} stack=[0xc00006e000,0xc000076000)
000000c0000758a8:  0000000000000001  0000000000000000 
000000c0000758b8:  0000000000000000  0000000000515600 
000000c0000758c8:  000000c00008e101  000102000043c495 
000000c0000758d8:  0000000000000008  0000000000000008 
000000c0000758e8:  000000c0000c4000  00000000006079a0 
000000c0000758f8:  0000000000000000  0000000000000010 
000000c000075908:  0000000000608100  0000000000140d40 
000000c000075918:  0000000000000000  000000000000000c 
000000c000075928:  000000c00008e100  000000c000075960 
000000c000075938:  00000000004b5820 <fmt.(*pp).free+192>  000000c0000759b8 
000000c000075948:  00000000004403d7 <runtime.growslice+567>  000000c0000c4000 
000000c000075958:  0000000000000000  0000000000000000 
000000c000075968:  0000000000437adc <runtime.newproc+140>  000000c000075988 
000000c000075978:  0000000000000008  000000c0000759d8 
000000c000075988:  0000000000455610 <runtime.newproc.func1+0>  000000c0000759d0 
000000c000075998:  000000c0000759d8  0000000000000008 
000000c0000759a8: <000000c000002180  00000000004490ab <runtime.(*timersBucket).addtimerLocked+283> 
000000c0000759b8:  000000c000075a08  00000000004490ab <runtime.(*timersBucket).addtimerLocked+283> 
000000c0000759c8:  000000c000000008  000000000053df70 
000000c0000759d8:  0000000000608520  0000000000000000 
000000c0000759e8:  0000000000000001  000000c0000c4000 
000000c0000759f8:  0000000000000000  0000000000000001 
000000c000075a08:  000000c000075a48  0000000000448e1a <time.Sleep+266> 
000000c000075a18: !0000000000608520 >000000c00004a200 
000000c000075a28:  0000000000000000  0000000000608520 
000000c000075a38:  000000c00004a200  000000c000002180 
000000c000075a48:  000000c000075f88  00000000004f840d <main.main+3773> 
000000c000075a58:  000000012a05f200  0000000000000001 
000000c000075a68:  0000000000000001  000000000000000c 
000000c000075a78:  0000000000000000  0000000000000000 
000000c000075a88:  0000000000000000  0000000000000000 
000000c000075a98:  0000000000000002  0000000000000002 
000000c000075aa8:  0000000000000002  0000000000000002 
000000c000075ab8:  0000000000000002  0000000000000002 
000000c000075ac8:  0000000000000000  0000000000000000 
000000c000075ad8:  0000000000000000  0000000000000000 
000000c000075ae8:  000000c0000821e0  000000c000075c90 
000000c000075af8:  0000000000030000  0000000002030000 
000000c000075b08:  000000c000075cb0  000000c000075cc0 
000000c000075b18:  0203000000000000 
syscall.CloseHandle(0xc00004a200, 0x0, 0x608520)
    ...golang/src/syscall/zsyscall_windows.go:353 +0x71
rax     0x7ffb78369a9a
rbx     0x130
rcx     0x201f840
rdi     0x2e7000
rsi     0xc0000afcf8
rbp     0xc0000afca0
rsp     0x201fd50
r8      0x1
r9      0x2e7000
r10     0x7ffb7afd83d0
r11     0x201f830
r12     0x2030000
r13     0x0
r14     0x20
r15     0xc000032ad8
rip     0x7ffb7b06e78a
rflags  0x200
cs      0x33
fs      0x53
gs      0x2b

Seems like all what have to be done to fix it is to remove port.Close() from line 97 in go.bug.st\serial.v1\serial_windows.go:

        getCommState(port.handle, params)
        if err := setCommState(port.handle, params); err != nil {
            port.Close() // <--
            return 0, err
        }
cmaglie commented 4 years ago

Should be fixed with https://github.com/bugst/go-serial/pull/42