-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
io.Copy blocked after closing #88
Comments
This seems like expected behavior. Could you describe your use case and expected result more in detail?
|
Usually src or dest can exit after calling Close method io.Copy. Scenario: websocket reads and writes a bash terminal. The NewWebscoketConn function will convert ws.Conn into an rwc object, and then io.Copy uses rwc and ptmx to exchange read and write data streams. The object returned by the NewWebscoketConn function implements the io.ReadWriteCloser interface, which encapsulates the reading and writing of websocket data. import (
"github.com/creack/pty"
"github.com/gobwas/ws"
)
func handlebash(w http.ResponseWriter, r *http.Request) {
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return
}
cmd := exec.Command("bash")
ptmx, err := pty.Start(cmd)
if err != nil {
return
}
log.Println("start bash")
wconn := NewWebscoketConn(conn)
go func() {
log.Println(io.Copy(ptmx, wconn))
log.Println("stop wconn", wconn.Close())
ptmx.Write([]byte(" "))
ptmx.Close()
}()
go func() {
log.Println(io.Copy(wconn, ptmx))
log.Println("stop ptmx", ptmx.Close())
// wconn.Write([]byte("\r\n"))
wconn.Close()
}()
} Initially, after wconn.Close (), Write triggers err to exit io.Copy. With your prompt, I modified an implementation that uses ctx to close cmd to close ptmx. ctx, cannel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, "bash")
go func() {
log.Println(io.Copy(ptmx, wconn))
log.Println("stop wconn", wconn.Close())
cannel()
}()
go func() {
log.Println(io.Copy(wconn, ptmx))
log.Println("stop ptmx")
}() |
When compiled with go older than 1.12 creack/pty will not include a fix for blocking Read() and will be prone to data races - but at least it will work For more information see issues: creack#88 creack#114 creack#156 creack#162
When compiled with go older than 1.12 creack/pty will not include a fix for blocking Read() and will be prone to data races - but at least it will work For more information see issues: creack#88 creack#114 creack#156 creack#162
When compiled with go older than 1.12 creack/pty will not include a fix for blocking Read() and will be prone to data races - but at least it will work For more information see issues: creack#88 creack#114 creack#156 creack#162
After ptmx is closed, io.Copy will not read and return an error.
example:
example2:
The text was updated successfully, but these errors were encountered: