Skip to content

Data race when calling acceptor.Stop() immediately after acceptor.Start() #681

Open
@vihaanthora

Description

@vihaanthora

Description

When calling acceptor.Stop() immediately after acceptor.Start(), there appears to be a data race for the sync.Once variable in session stopOnce when these two functions are called in succession. This issue was not present in v0.7.0.

Steps To Reproduce

func TestQuickFIXAcceptor(t *testing.T) {
	// Create a QuickFIX configuration file in memory for the acceptor
	cfg := `
[DEFAULT]
ConnectionType=acceptor
ReconnectInterval=5
StartTime=00:00:00
EndTime=23:59:59
HeartBtInt=30
SocketAcceptPort=5000

[SESSION]
BeginString=FIX.4.2
SenderCompID=SERVER
TargetCompID=CLIENT
`

	// Parse the settings
	settings, err := quickfix.ParseSettings(strings.NewReader(cfg))
	require.NoError(t, err)

	// Create a minimal application that implements the Application interface
	app := Application{}

	// Create the store and log factories
	storeFactory := quickfix.NewMemoryStoreFactory()
	logFactory := quickfix.NewScreenLogFactory()

	// Create the acceptor
	acceptor, err := quickfix.NewAcceptor(&app, storeFactory, settings, logFactory)
	require.NoError(t, err)

	// Start the acceptor
	err = acceptor.Start()
	require.NoError(t, err)

	// Stop the acceptor
	acceptor.Stop()
}

Race Detector Error Stack

=== RUN   TestQuickFIXAcceptor
<2024-11-26 12:56:00.737010131 +0000 UTC, FIX.4.2:SERVER->CLIENT, event>
  (Created session)
==================
WARNING: DATA RACE
Read at 0x00c0001e0134 by goroutine 25:
  sync/atomic.LoadInt32()
      /usr/local/go/src/runtime/race_amd64.s:202 +0xb
  sync/atomic.LoadUint32()
      <autogenerated>:1 +0x10
  sync.(*Once).Do()
      /usr/local/go/src/sync/once.go:63 +0x30
  github.com/quickfixgo/quickfix.(*session).stop()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/session.go:96 +0x2d1
  github.com/quickfixgo/quickfix.(*Acceptor).Stop()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:148 +0x27a
  github.com/quickfix-test/pkg/fix.TestQuickFIXAcceptor()
      ~/Projects/quickfix-test/go/pkg/fix/engine_test.go:90 +0x1b7
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1742 +0x44

Previous write at 0x00c0001e0134 by goroutine 26:
  github.com/quickfixgo/quickfix.(*session).run()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/session.go:866 +0x4f
  github.com/quickfixgo/quickfix.(*Acceptor).Start.func1()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:115 +0x2f
  github.com/quickfixgo/quickfix.(*Acceptor).Start.gowrap1()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:117 +0x41

Goroutine 25 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:1742 +0x825
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:2161 +0x85
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1689 +0x21e
  testing.runTests()
      /usr/local/go/src/testing/testing.go:2159 +0x8be
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:2027 +0xf17
  main.main()
      _testmain.go:165 +0x2bd

Goroutine 26 (running) created at:
  github.com/quickfixgo/quickfix.(*Acceptor).Start()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:114 +0x19d8
  github.com/quickfix-test/pkg/fix.TestQuickFIXAcceptor()
      ~/Projects/quickfix-test/go/pkg/fix/engine_test.go:86 +0x18e
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1742 +0x44
==================
==================
WARNING: DATA RACE
Write at 0x00c0001e0138 by goroutine 25:
  sync/atomic.CompareAndSwapInt32()
      /usr/local/go/src/runtime/race_amd64.s:310 +0xb
  sync/atomic.CompareAndSwapInt32()
      <autogenerated>:1 +0x18
  sync.(*Once).doSlow()
      /usr/local/go/src/sync/once.go:70 +0x51
  sync.(*Once).Do()
      /usr/local/go/src/sync/once.go:65 +0x44
  github.com/quickfixgo/quickfix.(*session).stop()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/session.go:96 +0x2d1
  github.com/quickfixgo/quickfix.(*Acceptor).Stop()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:148 +0x27a
  github.com/quickfix-test/pkg/fix.TestQuickFIXAcceptor()
      ~/Projects/quickfix-test/go/pkg/fix/engine_test.go:90 +0x1b7
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1742 +0x44

Previous write at 0x00c0001e0138 by goroutine 26:
  github.com/quickfixgo/quickfix.(*session).run()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/session.go:866 +0x4f
  github.com/quickfixgo/quickfix.(*Acceptor).Start.func1()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:115 +0x2f
  github.com/quickfixgo/quickfix.(*Acceptor).Start.gowrap1()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:117 +0x41

Goroutine 25 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:1742 +0x825
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:2161 +0x85
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1689 +0x21e
  testing.runTests()
      /usr/local/go/src/testing/testing.go:2159 +0x8be
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:2027 +0xf17
  main.main()
      _testmain.go:165 +0x2bd

Goroutine 26 (running) created at:
  github.com/quickfixgo/quickfix.(*Acceptor).Start()
      ~/go/pkg/mod/github.com/quickfixgo/[email protected]/acceptor.go:114 +0x19d8
  github.com/quickfix-test/pkg/fix.TestQuickFIXAcceptor()
      ~/Projects/quickfix-test/go/pkg/fix/engine_test.go:86 +0x18e
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/local/go/src/testing/testing.go:1742 +0x44
==================
    ~/Projects/quickfix-test/go/pkg/fix/testing.go:1398: race detected during execution of test
--- FAIL: TestQuickFIXAcceptor (0.27s)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions