Hacker News new | past | comments | ask | show | jobs | submit login

> Using that, I was able to discover that the Go runtime was actually spinning up two goroutines when I thought it was only using one (servicing the stdout and stderr of a child process), and the one buffer object I was writing to turned out to be written to from two separate threads

Wait, does it mean that go makes your code running in more threads without you knowing? When can that happen? Sounds scary if true.




It means that there's nothing in the API that tells you whether the function might spawn a goroutine. It has to document it.

I don't remember the precise details anymore, but my recollection is I was spawning a child process and passing Writers to use for stdout and stderr (looking at os.exec, I don't immediately see anything that matches my recollection, so I'm wondering if maybe I was using a different package). I expected that it would internally do something like select over stdout and stderr in one goroutine, therefore making it safe for me to pass the same buffer object for both (because I wanted to combine writes). But instead it was spawning 2 goroutines, not 1. And there was no evidence in the API that what I was doing was wrong, the only way to know was for the data race detector to flag it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: