cmd.StdoutPipe在Go包文档中的示例在playground中无法运行。

huangapple go评论96阅读模式
英文:

cmd.StdoutPipe example in go pkg docs does not run in playground

问题

cmd.StdoutPipe的示例在Go文档中(https://pkg.go.dev/os/exec#example-Cmd.StdoutPipe)无法在playground中运行。

https://play.golang.org/p/ek7-_Xa_bN3

错误信息:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [IO wait]:
internal/poll.runtime_pollWait(0x7faec2ac4e88, 0x72)
	/usr/local/go-faketime/src/runtime/netpoll.go:234 +0x89
internal/poll.(*pollDesc).wait(0xc000074180, 0xc000100000, 0x1)
	/usr/local/go-faketime/src/internal/poll/fd_poll_runtime.go:84 +0x32
internal/poll.(*pollDesc).waitRead(...)
	/usr/local/go-faketime/src/internal/poll/fd_poll_runtime.go:89
internal/poll.(*FD).Read(0xc000074180, {0xc000100000, 0x200, 0x200})
	/usr/local/go-faketime/src/internal/poll/fd_unix.go:167 +0x25a
os.(*File).read(...)
	/usr/local/go-faketime/src/os/file_posix.go:32
os.(*File).Read(0xc00000e028, {0xc000100000, 0x28, 0xc000060e80})
	/usr/local/go-faketime/src/os/file.go:119 +0x5e
encoding/json.(*Decoder).refill(0xc00007e000)
	/usr/local/go-faketime/src/encoding/json/stream.go:165 +0x17f
encoding/json.(*Decoder).readValue(0xc00007e000)
	/usr/local/go-faketime/src/encoding/json/stream.go:140 +0xbb
encoding/json.(*Decoder).Decode(0xc00007e000, {0x4cf580, 0xc00000c048})
	/usr/local/go-faketime/src/encoding/json/stream.go:63 +0x78
main.main()
	/tmp/sandbox2294589397/prog.go:24 +0x185

程序已退出。

在本地运行时没有死锁问题。我无法理解为什么在Go Playground中会发生死锁。

英文:

cmd.StdoutPipe example at go documentation: https://pkg.go.dev/os/exec#example-Cmd.StdoutPipe does not run in playground.

https://play.golang.org/p/ek7-_Xa_bN3

Error:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [IO wait]:
internal/poll.runtime_pollWait(0x7faec2ac4e88, 0x72)
	/usr/local/go-faketime/src/runtime/netpoll.go:234 +0x89
internal/poll.(*pollDesc).wait(0xc000074180, 0xc000100000, 0x1)
	/usr/local/go-faketime/src/internal/poll/fd_poll_runtime.go:84 +0x32
internal/poll.(*pollDesc).waitRead(...)
	/usr/local/go-faketime/src/internal/poll/fd_poll_runtime.go:89
internal/poll.(*FD).Read(0xc000074180, {0xc000100000, 0x200, 0x200})
	/usr/local/go-faketime/src/internal/poll/fd_unix.go:167 +0x25a
os.(*File).read(...)
	/usr/local/go-faketime/src/os/file_posix.go:32
os.(*File).Read(0xc00000e028, {0xc000100000, 0x28, 0xc000060e80})
	/usr/local/go-faketime/src/os/file.go:119 +0x5e
encoding/json.(*Decoder).refill(0xc00007e000)
	/usr/local/go-faketime/src/encoding/json/stream.go:165 +0x17f
encoding/json.(*Decoder).readValue(0xc00007e000)
	/usr/local/go-faketime/src/encoding/json/stream.go:140 +0xbb
encoding/json.(*Decoder).Decode(0xc00007e000, {0x4cf580, 0xc00000c048})
	/usr/local/go-faketime/src/encoding/json/stream.go:63 +0x78
main.main()
	/tmp/sandbox2294589397/prog.go:24 +0x185

Program exited.

Locally it's running correctly, no deadlock. I am not able to understand why deadlock happens in go playground.

答案1

得分: 3

根据os.exec包的文档(这个例子就来自这里):

需要注意的是,这个包中的示例假设是在Unix系统上运行的。它们可能无法在Windows上运行,而且它们也无法在golang.org和godoc.org使用的Go Playground中运行。

这个说明没有提供具体原因,但可以推测的原因是允许用户提供的Unix命令运行会给恶意代码提供更广泛的攻击面。并不是说不可能以相对安全的方式允许这样做,但是有各种权衡考虑,所以禁止使用os.exec是一个自然的选择。

英文:

From the documentation on the os.exec package (which is where this example comes from):

> Note that the examples in this package assume a Unix system. They may
> not run on Windows, and they do not run in the Go Playground used by
> golang.org and godoc.org
.

That note doesn't provide a reason, but the reason is presumably that allowing user-provided unix commands to run would give a broader attack surface for malicious code. It's not that it's impossible to allow this in a relatively secure way, but there's various tradeoffs which make disallowing os.exec a natural choice.

huangapple
  • 本文由 发表于 2022年2月27日 18:38:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/71284040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定