Skip to content
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

zaptest: test logger seems susceptible to data races (?) #687

Closed
bobheadxi opened this issue Mar 12, 2019 · 3 comments
Closed

zaptest: test logger seems susceptible to data races (?) #687

bobheadxi opened this issue Mar 12, 2019 · 3 comments

Comments

@bobheadxi
Copy link

bobheadxi commented Mar 12, 2019

I've run into this quite a bit - here's an example: https://travis-ci.com/ubclaunchpad/pinpoint/jobs/183635090#L564 (relevant PR: ubclaunchpad/pinpoint#152 - race condition only appeared after converting test loggers to zaptest)

and another one: https://travis-ci.com/RTradeLtd/Nexus/builds/103797950#L717

# snip
  testing.(*common).logDepth()
      /home/travis/.gimme/versions/go1.12.linux.amd64/src/testing/testing.go:621 +0x94
  testing.(*common).Log()
      /home/travis/.gimme/versions/go1.12.linux.amd64/src/testing/testing.go:614 +0x78
# snip

it seems to me that testing.T.Log() etc are not thread safe? while it kinda makes sense that the test logger isn't thread-safe (it's only meant to be used sequentially in a test after all), once it is passed to zaptest.NewLogger() it enters all sorts of potentially racey conditions, which makes this rather problematic

I think this can be resolved by just adding a mutex in testingWriter - I'd be happy to submit a patch! Let me know if this is a known/intended behaviour, or if there's something else i should be doing differently on my end

edit actually, i just saw this note:

	// Note: t.Log is safe for concurrent use.
	w.t.Logf("%s", p)
	if w.markFailed {
		w.t.Fail()
	}

am I doing something wrong here? 🤔

edit2 I added a crude test and under -race nothing seems to be off on its own:

func TestTestLoggerConcurrent(t *testing.T) {
	log := NewLogger(t).Sugar()
	go log.Info("received work order")
	go log.Debug("starting work")
	go log.Warn("work may fail")
	go log.Errorw("work failed", "error", errors.New("tremendous sadness"))
	time.Sleep(time.Millisecond)
}

any help would be appreciated!

@bobheadxi bobheadxi changed the title zaptest: test logger seems susceptible to data races zaptest: test logger seems susceptible to data races (?) Mar 12, 2019
@Sauraus
Copy link

Sauraus commented Mar 15, 2019

The issue that you are running into does it perhaps the side effect of opening 100+ /dev/pts/X sessions and thus at some point running out of file handles?

@prashantv
Copy link
Collaborator

The race detector has the following logs:

WARNING: DATA RACE
Read at 0x00c00018c943 by goroutine 44:
  testing.(*common).logDepth()
      /home/travis/.gimme/versions/go1.12.linux.amd64/src/testing/testing.go:629 +0x132
  testing.(*common).Logf()
      /home/travis/.gimme/versions/go1.12.linux.amd64/src/testing/testing.go:614 +0x90
  testing.(*T).Logf()
      <autogenerated>:1 +0x75

and

Previous write at 0x00c00018c943 by goroutine 19:
  testing.tRunner.func1()
      /home/travis/.gimme/versions/go1.12.linux.amd64/src/testing/testing.go:856 +0x33e
  testing.tRunner()
      /home/travis/.gimme/versions/go1.12.linux.amd64/src/testing/testing.go:869 +0x17f

If we look at the file:line of the write (testing.go:856) we see the following:

 854     // Do not lock t.done to allow race detector to detect race in case
 855     // the user does not appropriately synchronizes a goroutine.
 856     t.done = true

t.Log is safe for concurrent use, but it's not safe to call t.Log as the test is ending, which I think is causing the race detector to go off. You should make sure that any background goroutines are stopped before the test ends.

Closing the issue as this doesn't seem like a zap issue, please let me know if that's not the case.

@bobheadxi
Copy link
Author

bobheadxi commented Mar 15, 2019

ah thanks @prashantv , that definitely seems like a likely culprit 👍 appreciate the help!

update: fixed by making sure no goroutines log after a test ends as suggested

mx-psi added a commit to open-telemetry/opentelemetry-collector-contrib that referenced this issue Aug 4, 2023
…d data race (#24899)

**Description:** 

Fixes data race on `TestChain` that was caused by logging after the test
was ended (see
uber-go/zap#687 (comment) for
details).
To do this, we move the logging outside of the goroutines in the Chain
provider.
Goroutines can still be running after the function has returned (this is
intentional, their return value will just be ignored), but they will no
longer log anything.

I also made the `Source` method honor cancellation of the context when
waiting on providers.

**Link to tracking Issue:** Reported on Slack, see
https://cloud-native.slack.com/archives/C01N6P7KR6W/p1691074033049049?thread_ts=1690992010.123699&cid=C01N6P7KR6W
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 14, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112
 Close #3217

Signed-off-by: Ekaterina Pavlova <[email protected]>
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 14, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112
 Close #3217

Signed-off-by: Ekaterina Pavlova <[email protected]>
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 14, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112
 Close #3217

Signed-off-by: Ekaterina Pavlova <[email protected]>
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 14, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112
 Close #3217

Signed-off-by: Ekaterina Pavlova <[email protected]>
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 14, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112
 Close #3217

Signed-off-by: Ekaterina Pavlova <[email protected]>
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 15, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112

Signed-off-by: Ekaterina Pavlova <[email protected]>
AliceInHunterland added a commit to nspcc-dev/neo-go that referenced this issue Feb 15, 2024
Added sync logs for every service separately to provide the ability to
have a custom logger for each service. Data race in logging is fixed by
adding a sync log, this problem is related to uber-go/zap#687.

 Close #2973
 Close #2974
 Close #3112

Signed-off-by: Ekaterina Pavlova <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants