Closed bcmills closed 7 years ago
One possible fix would be to have something in the runtime make an explicit call (in cgo-enabled builds) to some no-op libc function (e.g. sleep(0)
) to trigger signal delivery. (runtime.findrunnable
seems like a likely candidate.)
However, it isn't obvious to me that any call to runtime.futexsleep
with a negative ns
parameter can be made safely in the presence of TSAN, since the signal could be delivered at any point between the last libc call and the futex
syscall. Since futexsleep
allows spurious wakeups, perhaps we also need to inject an arbitrary timeout to futexsleep
when TSAN is enabled to bound the delay on signal delivery.
(@ianlancetaylor @dvyukov )
https://go-review.googlesource.com/#/c/35494/ has a potential fix.
Tsan has a notion of "blocking regions" of code (e.g. pthread_cond_wait). It delivers signals synchronously in such regions as it knows that no activity happens on the thread. We could mark whole Go world as "blocking region", then tsan will deliver signals synchronously while in Go code. However, we call some tsan annotations (__tsan_acquire/release) from Go code, so we will need to somehow mark them as well. This will require new hooks in tsan runtime, so we will need to handle versioning somehow as old tsan runtime won't have the hooks. What release is this for?
We could mark whole Go world as "blocking region", then tsan will deliver signals synchronously while in Go code. … This will require new hooks in tsan runtime
That seems much more complicated than making periodic libc calls on idle M
s — we would also need to unblock the regions during cgo calls.
What release is this for?
I was trying to fix it in 1.8, but the changes are invasive enough that Ian suggested we delay until 1.9.
CL https://golang.org/cl/35494 mentions this issue.
I've been thinking more about @dvyukov's comments, and I'm starting to see the logic to it.
I agree that marking the whole Go runtime as a "blocking region" for TSAN purposes is the right first-principles solution. It seems that as far as TSAN is concerned, the important property of a "blocking call" is that it does not acquire any libc locks, and Go only acquires libc locks when executing cgo function calls (including the x_cgo
hooks in place for other runtime calls).
That said, the "periodic yielding" strategy does seem to improve responsiveness significantly in practice, so I think that's probably the right fallback for versions of TSAN which do not support the proposed new hooks.
CL https://golang.org/cl/37867 mentions this issue.
The fix for #17753 causes signal delivery in Go programs running under TSAN to pass through the TSAN interceptors.
Unfortunately, it appears that the TSAN interceptors defer delivery of asynchronous signals until the next blocking libc call on the same thread. This interacts poorly with the Go runtime's use of direct 'futex' syscalls to park idle threads: if the thread receiving the signal happens to be one that the runtime is about to park, it may never make a libc call and the signal will be lost.
A simple program illustrating this behavior:
src/tsansig/tsansig.go:
It should exit with status 0 almost immediately. Instead, it hangs forever.
One might expect that injecting a blocking libc call would help:
But that attempt at a workaround does not succeed: the thread that receives the signal happens not to be the one the runtime uses for the subsequent cgo calls.
Impact on Go 1.8
This is (unfortunately) a significant regression over Go 1.7. In 1.7, Go programs without significant C signal behavior would exhibit correct Go signal behavior. In the current 1.8 RC, the C signal behavior in the presence of the Go runtime is improved, but the Go signal behavior is broken even for programs with no interesting C signal activity.