Are os. Chdir
, os.Setuid
and os.Setgid
thread-safe in Go?
In otherwords, if I do a os.Chdir
(or the other two functions) in two different goroutines running in parallel, is it going change directory for the working goroutine or the whole process (which can cause problems)?
I couldn't find any information in the documentation.
Under the hood, os.Chdir
just calls the chdir()
system call:
211 func Chdir(dir string) error {
212 if e := syscall.Chdir(dir); e != nil {
213 return &PathError{"chdir", dir, e}
214 }
215 return nil
216 }
So it affects the entire process. The same is true for os.Setuid
and os.Setgid
.
It is probably safe to call these from multiple goroutines at the same time, because executing a system call halts the scheduler; but beware of race conditions.