Skip to content

postgres-listener: Fix data race during initialisation

Pavlo Strokov requested to merge ps-data-race-on-listener into master

During connection establishment the 'listener' field is assigned the value the other goroutine tries to read. The simple lock around accessing 'listener' field solves the problem.

The failed job:

=== RUN   TestPostgresListener_Listen_storage_empty_notification
==================
WARNING: DATA RACE
Read at 0x00c000152ac0 by goroutine 54:
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.(*PostgresListener).handleNotifications()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:179 +0x7c
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.(*PostgresListener).handleNotifications-fm()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:174 +0x4a
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.(*PostgresListener).async.func1()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:216 +0x72

Previous write at 0x00c000152ac0 by goroutine 82:
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.(*PostgresListener).connect()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:125 +0x2a4
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.NewPostgresListener()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:78 +0x4ab
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.testListener()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres_test.go:516 +0x4dc
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.TestPostgresListener_Listen_storage_empty_notification()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres_test.go:453 +0xcc
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202

Goroutine 54 (running) created at:
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.(*PostgresListener).async()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:214 +0x73
  gitlab.com/gitlab-org/gitaly/v14/internal/praefect/datastore.(*PostgresListener).connect.func1()
      /builds/gitlab-org/gitaly/internal/praefect/datastore/listener_postgres.go:111 +0x364
  github.com/lib/pq.(*Listener).emitEvent()
      /root/go/pkg/mod/github.com/lib/pq@v1.10.1/notify.go:797 +0x575
  github.com/lib/pq.(*Listener).listenerConnLoop()
      /root/go/pkg/mod/github.com/lib/pq@v1.10.1/notify.go:827 +0x519
  github.com/lib/pq.(*Listener).listenerMain()
      /root/go/pkg/mod/github.com/lib/pq@v1.10.1/notify.go:856 +0x38

Goroutine 82 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:1239 +0x5d7
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:1512 +0xa6
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202
  testing.runTests()
      /usr/local/go/src/testing/testing.go:1510 +0x612
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:1418 +0x3b3
  main.main()
      _testmain.go:101 +0x236
==================
    testing.go:1093: race detected during execution of test
--- FAIL: TestPostgresListener_Listen_storage_empty_notification (1.15s)

Merge request reports