-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: runtime/race: add const Enabled #36477
Comments
I’ve done this in the past to skip tests that were only useful with the -race flag set. |
It seems to me a more natural place for this would be runtime/race.Enabled. |
Note that the standard library has this already in internal/race. |
@josharian, I can send a CL with something like: package race
import "internal/race"
// Enabled tells whether the race detector was enabled when building.
const Enabled = enabled
const enabled = race.Enabled If the proposal is accepted. |
I too, have used this method manually in the past. The main counterargument here could be that one shouldn't make code behave differently when running in race mode - but that's already possible today, and I think there are plenty of legitimate use cases such as:
|
I also think that, long term, we could replace |
Here's another valid use case, I think: #37681 If the proposal is accepted, |
The “tests that should only run in race mode” are precisely (and only) those tests that verify run-time checks that are implemented using the race detector itself and do not build a new binary using Propagating the |
The set of “tests that can't run in race mode” should be empty. A variable whose value is set at compile-time is too late for guarding |
So, I agree that this would be useful in some cases, but those cases seem pretty rare — I'm not sure that they're worth the moral hazard of using the boolean to paper over actual races. |
Another use case: In the compiler, when race mode is enabled, we randomize the order in which we compile functions. This is because the optimal ordering for performance was the least likely to flush out race conditions. |
Provide race.Enabled for programs to know whether they were built with race detector or not. Code originates from https://lab.nexedi.com/kirr/wendelin.core/tree/25c3184d/wcfs/internal/race See also: golang/go#36477.
Another use case: Reducing the running time of a test in race mode. I encountered a test recently that experienced a drastic slowdown when run with -race, because of the heavy use of synchronization. Reducing the iterations with -race was the only reasonable fix I saw. |
@josharian, maybe? But you can also reduce the running time of a test using (Plus, if one of your tests fails due to memory corruption in an ordinary run, it seems unpleasant for that test to change its behavior — perhaps dramatically! — when you run it under the race detector to try to diagnose the problem.) |
You can, but as you point out, testing.Short is orthogonal. It seems better to tie the solution to the actual problem; I'd rather
It doesn't. It just runs less long; it's a stress test that runs for fewer iterations. If I were investigating memory corruption, I'd run it in the loop. Or read the test and notice the line that says |
Every argument in favor of this seems to be primarily for tests, not for normal code. It seems that everyone agrees that normal code should basically never do something different depending on the presence of the race detector, and that the very rare exceptions to that can be handled just fine by the build tags, as they provide enough annoyance in so implementing something that it at least discourages you from doing so. I personally have written a manual I'm personally somewhat ambivalent on the presence of a boolean on top of the build tags, but, all of that being said, why not put it in something more test specific, like |
@DeedleFake sometimes you test without package testing. And sometimes the place you want to add the hook is not in a test file. See my comment: #36477 (comment). |
Another use for this flag: When the race detector is enabled, profiling could emit warnings. (Where and how to emit those warnings will vary; package testing might print them to stderr, third party packages might return an error from some API, etc. In an ideal world, we'd be able to write a message to the pprof output itself, which cmd/pprof would then display.) |
It would be great to have a flag that specifies whether race detector is enabled or not. Plenty of projects at github use the "build flag" approach https://github.com/search?p=1&q=%22Package+israce+reports+if+the+Go+race+detector+is+enabled.%22&type=Code And in my case I need to propagate "-race" to "go build" command I invoke from tests. See anatol/booster@35b1dda But it would be convenient to have something cleaner (like runtime.race.Enabled variable). |
Another use case (observed long ago, finally remembered to record here): Skipping AllocsPerRun tests in race mode, because -race can cause extra allocs. |
Another use case that I seem to not have brought up sooner are debug outputs, like |
It seems like we have a variety of use cases, including some abuse cases. All the cases are already handled, with some complexity, by using build tags. Adding race.Enabled would make all the cases easier. The question is whether we want to encourage people to change test behavior based on the race detector. Today it's clear that's not something you're supposed to do, because it's so difficult to do it. Adding race.Enabled would make it seem like something you're expected to do, at least some of the time. |
This proposal has been added to the active column of the proposals project |
Another non-test-related use case I've encountered since my last comment is tweaking resource usage. Since programs compiled with |
As I noted two weeks ago, the lack of race.Enabled today implies that most people shouldn't be using it in most contexts. Do I have that right? |
This limitation at least has been lifted on Linux and MacOS (and we're working on lifting it on Windows). |
@rsc, I do not agree that it's currently “difficult”. It is merely annoying. If your goal is to discourage any difference between race and non-race code, you could remove the @mvdan, @josharian, and I provided valid examples of non-test use cases, including propagation of the Leaving only the annoying approach would be approximately the same as not having I have no strong feelings either way, but the decision against this proposal will merely mean that this boilerplate code will propagate by copy-paste, because nobody would want to add an external dependency for a single boolean. (At least in the Go realm, heh.) |
Another use case is working around #37233, which seems to have been around for a while now. This fits into the “Work Around Race Detector's Issues” category of use cases, along with the goroutine limit (soon to be gone) and RAM/CPU usage. |
Based on the discussion above, this proposal seems like a likely decline. |
I still mildly support this proposal because I've had to resort to |
No change in consensus, so declined. |
For anyone else who wants an importable package, I just tossed one up at https://github.com/josharian/race (because I had to add it to yet another project, to work around #69995). |
I've seen a lot of code like this:
I think it would be better to have one boolean for that, and the most logical place for it seems to be either
runtime/debug
orruntime/race
.The text was updated successfully, but these errors were encountered: