doukundong9652 2017-12-10 13:00
浏览 87
已采纳

将exec.CommandContext与sg_inq sys comand和超时一起使用永远不会返回

I'm trying to call sg_inq on a multipath device (/dev/mapper/mpatha). sg_inq sends a iScsi inquiry to the specific device and extracts some data. I'm using go's own exec.commandContext for the timeout, like this:

ctx, cancel := context.WithTimeout(context.Background(), 
time.Duration(1000)*time.Millisecond)
defer cancel()

cmd := exec.CommandContext(ctx, "sg_inq", "/dev/mapper/mpatha")
err := cmd.Run()
cmd.Stdout = &stdout
cmd.Stderr = &stderr
stdErr := stderr.Bytes()
stdOut := stdout.Bytes()

code reference in the official guide - https://golang.org/pkg/os/exec/#CommandContext

This code never returns. When I run it as a standalone script, it works fine. But what I run it as a part of a whole interface, it just gets stuck. I can see all of the calls "out", but none of them return.

Is there any reason why the same code snippet works in a standalone script but not in a whole go class? Or maybe I'm way off here and the problem is something else entirely? I'm compiling it on go 1.9.2 (latest) but I've tried on 1.9.1 as well.

The build works fine, the tests pass, so everything should be working.

  • 写回答

1条回答 默认 最新

报告相同问题?